File: writePNG.c

package info (click to toggle)
xpaint 2.5.1-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,488 kB
  • ctags: 2,478
  • sloc: ansic: 25,980; makefile: 36; sh: 23
file content (251 lines) | stat: -rw-r--r-- 7,340 bytes parent folder | download | duplicates (3)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* +-------------------------------------------------------------------+ */
/* | Copyright 1996, Greg Roelofs (newt@pobox.com)                     | */
/* | Last revised:  21 August 1996                                     | */
/* +-------------------------------------------------------------------+ */

/* $Id: writePNG.c,v 1.7 1996/08/29 05:24:57 torsten Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include "png.h"
#include "image.h"
#include "rwTable.h"

#ifndef Trace
#  ifdef PNG_DEBUG
#    define Trace(x)   fprintf x ; fflush(stderr)
#  else
#    define Trace(x)
#  endif
#endif

extern void compressColormap(Image *image);


static int
WritePNG(char *file, Image *image, int interlace_type)
{
    FILE  *fp = fopen(file, "wb");
    char software_text[40];
    int i;
    png_structp  png_ptr;
    png_infop  info_ptr;
    png_textp  software;


    Trace((stderr, "\nGRR WritePNG:  %d x %d, scale = %d\n",
      image->width, image->height, image->scale));
    if (!fp)
        return 1;

    png_ptr = (png_structp)malloc(sizeof (png_struct));
    if (!png_ptr)
        return 1;

    info_ptr = (png_infop)malloc(sizeof (png_info));
    if (!info_ptr) {
        free(png_ptr);
        return 1;
    }

    if (setjmp(png_ptr->jmpbuf)) {    
        png_write_destroy(png_ptr);
        free(info_ptr);
        free(png_ptr);
        fclose(fp);
        return 1;
    }

    png_info_init(info_ptr);
    png_write_init(png_ptr);
    png_init_io(png_ptr, fp);

    info_ptr->width = image->width;
    info_ptr->height = image->height;

    if (image->isBW) {
        if (image->maskData) {
            info_ptr->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
            info_ptr->bit_depth = 8;   /* promote to full grayscale */
        } else {
            info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
            info_ptr->bit_depth = 1;
        }
        Trace((stderr, "GRR WritePNG:  B/W, bit_depth = %d\n",
          info_ptr->bit_depth));

    } else if (image->isGrey) {
        info_ptr->color_type = image->maskData? PNG_COLOR_TYPE_GRAY_ALPHA :
                                                PNG_COLOR_TYPE_GRAY;
        if (image->cmapPacked)
            info_ptr->bit_depth = 8;
        else {
            Trace((stderr,
              "GRR WritePNG:  isGrey: cmapSize = %d (before compressing), ",
              image->cmapSize));
            compressColormap(image);
            Trace((stderr, "%d (after)\n", image->cmapSize));
            if (image->cmapSize > 16)
                info_ptr->bit_depth = 8;
            else if (image->cmapSize > 4)
                info_ptr->bit_depth = 4;
            else if (image->cmapSize > 2)
                info_ptr->bit_depth = 2;
            else
                info_ptr->bit_depth = 1;
            Trace((stderr, "GRR WritePNG:  isGrey: picked bit_depth = %d\n",
              info_ptr->bit_depth));
        }

    } else if (image->scale == 3) {
        Image *cmapImage;

        /* try compressing image to palette mode, but don't force if too big */
        if (!image->maskData)   /* can't store alpha mask with palette image */
            cmapImage = ImageCompress(image, 256, 1);

        if (cmapImage) {
            image = cmapImage;  /* original was deleted in ImageCompress() */
        } else {
            info_ptr->color_type = image->maskData? PNG_COLOR_TYPE_RGB_ALPHA :
                                                    PNG_COLOR_TYPE_RGB;
            info_ptr->bit_depth = 8;
            Trace((stderr, "GRR WritePNG:  RGB, bit_depth = 8\n"));
        }
    }

/*
	GRR ReadPNG:  reading file /home1/xpaint-images/cdrom.png
	GRR ReadPNG:  width = 34, height = 33
	GRR ReadPNG:  PNG_COLOR_TYPE_PALETTE
	GRR ReadPNG:  34 x 33, scale = 1
	GRR ReadPNG end:  34 x 33, scale = 1

	GRR WritePNG:  34 x 33, scale = 3
	GRR WritePNG:  palette, bit_depth = 4, num_palette = 12
 */

    /* either we're on an 8-bit or smaller display, or image->scale was 3 and
     * ImageCompress() worked
     */
    if (image->scale == 1) {
        info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
        if (image->maskData) {
            fprintf(stderr,
              "WritePNG:  can't use alpha mask with colormapped image\n");
            fflush(stderr);
        }
        Trace((stderr, "GRR WritePNG:  palette, bit_depth = "));
        if (!image->cmapPacked)
            compressColormap(image);
        if (image->cmapSize > 16)
            info_ptr->bit_depth = 8;
        else if (image->cmapSize > 4)
            info_ptr->bit_depth = 4;
        else if (image->cmapSize > 2)
            info_ptr->bit_depth = 2;
        else
            info_ptr->bit_depth = 1;
        info_ptr->valid |= PNG_INFO_PLTE;
        info_ptr->num_palette = image->cmapSize;
        info_ptr->palette = (png_colorp)image->cmapData;  /* seems to work... */
        Trace((stderr, "%d, num_palette = %d\n", info_ptr->bit_depth,
          info_ptr->num_palette));
#if 0
        for (i = 0;  i < image->cmapSize;  ++i) {
            info_ptr->palette[i].red = 
            info_ptr->palette[i].green = 
            info_ptr->palette[i].blue = 
        }
#endif
    }

    info_ptr->interlace_type = interlace_type;

    /* set the file gamma */
    info_ptr->valid |= PNG_INFO_gAMA;
#ifdef DISPLAY_GAMMA
    info_ptr->gamma = 1.0 / DISPLAY_GAMMA;
#else
    info_ptr->gamma = 0.45;   /* default: assume PC-like system */
#endif

    /* info_ptr->valid |= PNG_INFO_tEXt;               DOES NOT EXIST */
    /* only one text comment:  Software */
    software = (png_textp)malloc(sizeof(png_text));
    if (software) {
        /* room for up to 40 characters: */
        sprintf(software_text, "XPaint %s", XPAINT_VERSION);
        software->compression = -1;
        software->key = "Software";
        software->text = software_text;
        software->text_length = strlen(software->text);
        info_ptr->num_text = 1;
        info_ptr->text = software;
    } else {
        /* couldn't malloc:  oh well */
        info_ptr->num_text = 0;
        info_ptr->text = NULL;
    }

    info_ptr->valid |= PNG_INFO_tIME;
    png_convert_from_time_t(&info_ptr->mod_time, time(NULL));

    png_write_flush(png_ptr);
    png_write_info(png_ptr, info_ptr);

    png_set_packing(png_ptr);

    if (image->maskData) {
        /* alpha channel version */
        fprintf(stderr, "WritePNG:  sorry, can't write alpha images yet\n");
        fflush(stderr);
        png_write_destroy(png_ptr);
        free(info_ptr);
        free(png_ptr);
        fclose(fp);
        return 1;
    } else {
        int rowbytes = image->scale * image->width;
        png_bytep *row_pointers;

	if ((row_pointers = (png_bytep *)
	     malloc(image->height * sizeof(png_bytep))) == 0) {
	    RWSetMsg("Out of memory in WritePNG()");
	    return 1;
	}

        for (i = 0;  i < image->height;  ++i)
            row_pointers[i] = (png_bytep)image->data + i*rowbytes;

        png_write_image(png_ptr, row_pointers);
    }

    png_write_end(png_ptr, NULL);
    png_write_destroy(png_ptr);

    if (software)
        free(software);   /* we LOVE free software!! */

    free(info_ptr);
    free(png_ptr);   /* necessary?? */
    fclose(fp);

    return 0;
}



int
WritePNGn(char *file, Image *image)
{
    return WritePNG(file, image, 0);
}



int
WritePNGi(char *file, Image *image)
{
    return WritePNG(file, image, 1);
}