File: img_png.c

package info (click to toggle)
fenix 0.92a.dfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,492 kB
  • sloc: ansic: 42,357; sh: 3,474; perl: 527; makefile: 180; cpp: 19
file content (454 lines) | stat: -rw-r--r-- 13,052 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 *  Fenix - Videogame compiler/interpreter
 *  Current release       : FENIX - PROJECT 1.0 - R 0.84
 *  Last stable release   :
 *  Project documentation : http://fenix.divsite.net
 *
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *  Copyright � 1999 Jos� Luis Cebri�n Pag�e
 *  Copyright � 2002 Fenix Team
 *
 */

/*
 * FILE        : img_png.c
 * DESCRIPTION : PNG Loading functions
 *
 * HISTORY:     0.83 - Added gr_save_png
 *
 */

/*
 *  INCLUDES
 */

#ifdef TARGET_BEOS
#include <posix/assert.h>
#else
#include <assert.h>
#endif

#include <string.h>
#include <stdlib.h>

#ifdef USE_GETTEXT
#include <libintl.h>
#define _(String) gettext (String)
#else
#define _(String) String
#endif

#include <png.h>
#include "fxi.h"

/*
 *  GLOBAL VARIABLES
 */

extern SDL_Surface * screen ;   /* DEFINED in g_main.c */
static file * png ;


static void user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
{
    file_read (png, data, length) ;
}

GRAPH * gr_read_png (const char * filename)
{
    GRAPH * bitmap ;
    unsigned int n, x ;
    int shift;
    Uint16 * ptr ;
    Uint32 * orig ;
    Uint32 * row ;
    Uint32 Rshift, Gshift, Bshift ;
    Uint32 Rmask, Gmask, Bmask ;

    png_bytep   * rowpointers ;

    png_structp png_ptr ;
    png_infop   info_ptr, end_info ;
    png_uint_32 width, height, rowbytes;
    int         depth, color ;

    /* Abre el fichero y se asegura de que screen est� inicializada */

    png = file_open (filename, "rb") ;
    if (!png) gr_error (_("'%s': file does not exist\n"), filename) ;

    /* Prepara las estructuras internas */

    png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0) ;
    if (!png_ptr) gr_error (_("Error loading PNG file")) ;
    info_ptr = png_create_info_struct (png_ptr) ;
    end_info = png_create_info_struct (png_ptr) ;
    if (!info_ptr || !end_info) gr_error (_("Error loading PNG file")) ;

    /* Rutina de error */

#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
    if (setjmp(png_jmpbuf(png_ptr)))
#else
    if (setjmp (png_ptr->jmpbuf))
#endif
    {
        png_destroy_read_struct (&png_ptr, &info_ptr, &end_info) ;
        file_close (png) ;
        return 0 ;
    }

    /* Recupera informaci�n sobre el PNG */

    png_set_read_fn (png_ptr, 0, user_read_data) ;
    png_read_info (png_ptr, info_ptr) ;
    png_get_IHDR (png_ptr, info_ptr, &width, &height, &depth, &color, 0, 0 , 0) ;

    row = malloc( sizeof(Uint32) * width );
    if ( !row ) {
		file_close (png) ;
        return 0 ;
    }

    rowpointers = malloc( sizeof(png_bytep) * height );
    if ( !rowpointers ) {
		file_close (png) ;
        free ( row ) ;
        return 0 ;
    }

    /* Configura los distintos modos disponibles */

    if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
    {
        png_set_gray_to_rgb (png_ptr);
        if (color == PNG_COLOR_TYPE_GRAY)
            png_set_filler (png_ptr, 0xFF, PNG_FILLER_AFTER) ;
    }
    if (depth == 16)
        png_set_strip_16(png_ptr) ;
    if (color == PNG_COLOR_TYPE_RGB)
        png_set_filler (png_ptr, 0xFF, PNG_FILLER_AFTER) ;
    png_set_bgr(png_ptr) ;

    /* Recupera el fichero, convirtiendo a 16 bits si es preciso */

    rowbytes = png_get_rowbytes (png_ptr, info_ptr) ;
    bitmap = bitmap_new (0, width, height, (color == PNG_COLOR_TYPE_PALETTE) ? 8 : 16, 1) ;
    if (!bitmap) {
        file_close (png) ;
        free ( rowpointers ) ;
        free ( row ) ;
        return 0 ;
    }

    if (color == PNG_COLOR_TYPE_PALETTE) {
        /* Read the color palette */

            png_colorp png_palette = (png_colorp) png_malloc (png_ptr, 256*sizeof(png_color)) ;
        if (!png_palette) gr_error (_("Out of memory")) ;

                png_get_PLTE (png_ptr, info_ptr, &png_palette, &n) ;

        bitmap->palette = pal_new(NULL);

        for (n = 0; n < 256 ; n++) {
            bitmap->palette->rgb[n].r = png_palette[n].red;
            bitmap->palette->rgb[n].g = png_palette[n].green;
            bitmap->palette->rgb[n].b = png_palette[n].blue;
            if (!palette_loaded) {
                    palette[n].r = png_palette[n].red;
                    palette[n].g = png_palette[n].green;
                    palette[n].b = png_palette[n].blue;
                }
            }

        pal_refresh(bitmap->palette);

            palette_loaded = 1 ;
            palette_changed = 1 ;

        for (n = 0 ; n < height ; n++) rowpointers[n] = ((Uint8*)bitmap->data) + n*bitmap->pitch ;
        png_read_image (png_ptr, rowpointers) ;

        /* If the depth is less than 8, expand the pixel values */

        if (depth == 4)
        {
            shift = ((width & 0x01) << 2);
            for (n =  0 ; n < height ; n++)
            {
                char * orig = rowpointers[n];
                char * dest = orig + width - 1;
                orig += (width-1)/2;

                for (x = 0 ; x < width ; x++)
                {
                    *dest-- = ((*orig >> shift) & 0x0F);
                    if (shift == 4)
                    {
                        shift = 0;
                        orig--;
                    }
                    else shift = 4;
                }
            }
        }
        else if (depth == 2)
        {
            shift = ((3 - ((width + 3) & 0x03)) << 1);
            for (n = 0 ; n < height ; n++)
            {
                char * orig = rowpointers[n];
                char * dest = orig + width - 1;
                orig += (width-1)/4;

                for (x = 0 ; x < width ; x++)
                {
                    *dest-- = ((*orig >> shift) & 0x03);
                    if (shift == 6)
                    {
                        shift = 0;
                        orig--;
                    }
                    else shift += 2 ;
                }
            }
        }
        else if (depth == 1)
        {
            shift = ((7 - ((width + 7) & 0x07)) << 1);
            for (n = shift = 0 ; n < height ; n++)
            {
                char * orig = rowpointers[n];
                char * dest = orig + width - 1;
                orig += (width-1)/8;

                for (x = 0 ; x < width ; x++)
                {
                    *dest-- = ((*orig >> shift) & 0x01);
                    if (shift == 7)
                    {
                        shift = 0;
                        orig--;
                    }
                    else shift++ ;
                }
            }
        }
    }
    else
    {
        if (scr_initialized)
        {
            Bshift = 16-screen->format->Rshift ;
            Gshift =  8-screen->format->Gshift ;
            Rshift =  0-screen->format->Bshift ;

            Bmask  = 0xFF0000 ;
            Gmask  = 0x00FF00 ;
            Rmask  = 0x0000FF ;

            for (n = 0 ; n < screen->format->Rloss ; n++) Rshift++, Rmask <<= 1 ;
            for (n = 0 ; n < screen->format->Gloss ; n++) Gshift++, Gmask <<= 1 ;
            for (n = 0 ; n < screen->format->Bloss ; n++) Bshift++, Bmask <<= 1 ;

            Bmask &= 0xFF0000 ;
            Gmask &= 0x00FF00 ;
            Rmask &= 0x0000FF ;
        } else {
            Bshift = 8;
            Gshift = 5;
            Rshift = 3;
            Bmask  = 0xF70000;
            Gmask  = 0x00FC00;
            Rmask  = 0x0000F7;
        }

        for (n = 0 ; n < height ; n++) {
            rowpointers[0] = (void *)row ;
            png_read_rows (png_ptr, rowpointers, 0, 1) ;

            ptr = (Uint16*) bitmap->data + n*bitmap->pitch/2;
            orig = row ;
            for (x = 0 ; x < width ; x++) {
                ARRANGE_DWORD(orig);

                if ((*orig) & 0x80000000) {
                  *ptr = ((*orig & Rmask) >> Rshift)|((*orig & Gmask) >> Gshift)|((*orig & Bmask) >> Bshift)  ;
                  if (!*ptr) (*ptr)++ ;
                }
                else
                    *ptr = 0 ;
                ptr++, orig++ ;
            }
        }
    }

    /* Fin */
#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
    if (!setjmp(png_jmpbuf(png_ptr))) png_read_end (png_ptr, 0);
#else
    if (!setjmp (png_ptr->jmpbuf)) png_read_end (png_ptr, 0) ;
#endif
    file_close (png) ;
    bitmap->modified = 1 ;

    free ( row ) ;
    free ( rowpointers ) ;

    return bitmap ;
}

/*
 *  FUNCTION : gr_save_png
 *
 *  Save a GRAPH into a PNG file
 *
 *  PARAMS :
 *      gr              GRAPH with the image to save
 *      filename        name for the file
 *
 *  RETURN VALUE :
 *      0 - FAILURE
 *      1 - SUCCESS
 *
 */

int gr_save_png (GRAPH * gr, const char * filename)
{
    FILE * file = fopen (filename, "wb") ;

    png_structp png_ptr ;
    png_infop   info_ptr ;
    png_uint_32 k, i ;
    png_bytep   * rowpointers ;
    png_colorp  pal ;
    Uint32      * data, * ptr ;
    Uint16      * orig ;
    SDL_Color * gpal = NULL;

    if (!file) return(0) ;

    rowpointers = malloc( sizeof(png_bytep) * gr->height );
    if ( !rowpointers ) {
        fclose(file) ;
        return 0 ;
    }

    png_ptr  = png_create_write_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0) ;
    info_ptr = png_create_info_struct  (png_ptr) ;

    if (!png_ptr || !info_ptr) {
        free ( rowpointers ) ;
        fclose(file) ;
        return(0) ;
    }

    /* Error handling... */

#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
    if (setjmp(png_jmpbuf(png_ptr))) {
#else
    if (setjmp(png_ptr->jmpbuf)) {
#endif
        fclose (file) ;
        png_destroy_write_struct (&png_ptr, NULL) ;
        free ( rowpointers ) ;
        return(0) ;
    }

    png_init_io (png_ptr, file) ;

    /* NOTE: NO SUPPORT FOR ANIMATIONS! */

    if (gr->depth == 8) {
        /* 8 bits PNG file */
        png_set_IHDR (png_ptr, info_ptr, gr->width,
                      gr->height, 8, PNG_COLOR_TYPE_PALETTE,
                      PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
                      PNG_FILTER_TYPE_BASE) ;

        pal = (png_colorp) png_malloc (png_ptr, 256*sizeof(png_color)) ;
        if (!pal) {
            fclose (file) ;
            png_destroy_write_struct (&png_ptr, NULL) ;
            free ( rowpointers ) ;
            return(0) ;
        }

        if (gr->palette) gpal = gr->palette->rgb; else gpal = palette;

        /* Generate palette info */
        for (k = 0 ; k < 256 ; k++) {
            pal[k].red   = gpal[k].r ;
            pal[k].green = gpal[k].g ;
            pal[k].blue  = gpal[k].b ;
        }
        png_set_PLTE (png_ptr, info_ptr, pal, 256) ;
        png_write_info (png_ptr, info_ptr) ;

        /* no need to rearrange data */
        for (k = 0 ; k < (unsigned)gr->height ; k++)
            rowpointers[k] = (Uint8 *)gr->data + gr->pitch*k ;
        png_write_image (png_ptr, rowpointers) ;

        /* Free allocated palette... */
        png_free (png_ptr, (png_voidp) pal) ;
    } else {
        png_set_IHDR (png_ptr, info_ptr, gr->width,
                      gr->height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
                      PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
                      PNG_FILTER_TYPE_BASE) ;
        png_write_info (png_ptr, info_ptr) ;

        data = malloc (gr->width * gr->height * 4) ;
        if (!data) {
            fclose (file) ;
            png_destroy_write_struct (&png_ptr, NULL) ;
            free ( rowpointers ) ;
            return(0) ;
        }
        for (k = 0 ; k < (unsigned)gr->height ; k++) {
            ptr  = data + gr->width * k ; /* uses dword for each pixel! */
            orig = (Uint16 *)gr->data + gr->width * k ;
            rowpointers[k] = (Uint8 *)ptr ;
            for (i = 0 ; i < (unsigned)gr->width ; i++) {
                if (*orig == 0 && !(gr->info_flags & GI_NOCOLORKEY))
                    *ptr = 0x00000000 ;
                else {
                    *ptr = ((*orig & 0xf800) >> 8) |
                           ((*orig & 0x07e0) << 5) |
                           ((*orig & 0x001f) << 19)|
                           0xFF000000 ;
                    /* Rearrange data */
                    ARRANGE_DWORD(ptr) ;
                }
                orig++ ;
                ptr++  ;
            }
        }
        png_write_image (png_ptr, rowpointers) ;
        free (data) ;
        data = NULL ;
    }

    png_write_end (png_ptr, info_ptr) ;
    fclose (file) ;
    free ( rowpointers ) ;
    return (1) ;
}