File: writeimage.c

package info (click to toggle)
metapixel 0.11-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 356 kB
  • ctags: 575
  • sloc: ansic: 5,096; xml: 219; perl: 150; makefile: 98
file content (87 lines) | stat: -rw-r--r-- 2,190 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* -*- c -*- */

/*
 * writeimage.c
 *
 * metapixel
 *
 * Copyright (C) 2000 Mark Probst
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <assert.h>
#include <stdlib.h>

#include "rwpng.h"

#include "writeimage.h"

image_writer_t*
open_image_writing (char *filename, int width, int height, int format)
{
    image_writer_t *writer;
    void *data = 0;
    image_write_func_t write_func = 0;
    image_writer_free_func_t free_func = 0;

    if (format == IMAGE_FORMAT_PNG)
    {
	data = open_png_file_writing(filename, width, height);
	write_func = png_write_lines;
	free_func = png_free_writer_data;
    }
    else
	assert(0);

    if (data == 0)
	return 0;

    writer = (image_writer_t*)malloc(sizeof(image_writer_t));
    writer->width = width;
    writer->height = height;
    writer->num_lines_written = 0;
    writer->data = data;
    writer->write_func = write_func;
    writer->free_func = free_func;

    return writer;
}

void
write_lines (image_writer_t *writer, unsigned char *lines, int num_lines)
{
    assert(writer->num_lines_written + num_lines <= writer->height);

    writer->write_func(writer->data, lines, num_lines);
}

void
free_image_writer (image_writer_t *writer)
{
    writer->free_func(writer->data);
    free(writer);
}

void
write_image (char *filename, int width, int height, unsigned char *lines, int format)
{
    image_writer_t *writer = open_image_writing(filename, width, height, format);

    assert(writer != 0);

    write_lines(writer, lines, height);
    free_image_writer(writer);
}