Codebase list dillo / upstream/3.0.4.1 src / png.c
upstream/3.0.4.1

Tree @upstream/3.0.4.1 (Download .tar.gz)

png.c @upstream/3.0.4.1raw · history · blame

  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
/*
 * The png decoder for Dillo. It is responsible for decoding PNG data
 * and transferring it to the dicache.
 *
 * Geoff Lane nov 1999 zzassgl@twirl.mcc.ac.uk
 * Luca Rota, Jorge Arellano Cid, Eric Gaudet 2000
 * Jorge Arellano Cid 2009
 *
 * "PNG: The Definitive Guide" by Greg Roelofs, O'Reilly
 * ISBN 1-56592-542-4
 */

#include <config.h>
#ifdef ENABLE_PNG

#include <stdlib.h> /* For abort() */

#ifdef HAVE_LIBPNG_PNG_H
#include <libpng/png.h>
#else
#include <png.h>
#endif

#include "msg.h"
#include "image.hh"
#include "cache.h"
#include "dicache.h"

enum prog_state {
   IS_finished, IS_init, IS_nextdata
};

#if 0
static char *prog_state_name[] =
{
   "IS_finished", "IS_init", "IS_nextdata"
};
#endif

/*
 * This holds the data that must be saved between calls to this module.
 * Each time it is called it is supplied with a vector of data bytes
 * obtained from the web server. The module can process any amount of the
 * supplied data.  The next time the module is called, the vector may be
 * extended with additional data bytes to be processed.  The module must
 * keep track of the current start and cursor position of the input data
 * vector.  As complete output rasters are determined they are sent out of
 * the module for additional processing.
 *
 * NOTE:  There is no external control of the splitting of the input data
 * vector (only this module understands PNG format data.) This means that
 * the complete state of a PNG image being processed must be held in the
 * structure below so that processing can be suspended or resumed at any
 * point within an input image.
 *
 * In the case of the libpng library, it maintains its own state in
 * png_ptr and into_ptr so the FSM is very simple - much simpler than the
 * ones for XBM and PNM are.
 */

typedef struct {
   DilloImage *Image;           /* Image meta data */
   DilloUrl *url;               /* Primary Key for the dicache */
   int version;                 /* Secondary Key for the dicache */

   png_uint_32 width;           /* png image width */
   png_uint_32 height;          /* png image height */
   png_structp png_ptr;         /* libpng private data */
   png_infop info_ptr;          /* libpng private info */
   uchar_t *image_data;         /* decoded image data    */
   uchar_t **row_pointers;      /* pntr to row starts    */
   jmp_buf jmpbuf;              /* png error processing */
   int error;                   /* error flag */
   png_uint_32 previous_row;
   int rowbytes;                /* No. bytes in image row */
   short channels;              /* No. image channels */

/*
 * 0                                              last byte
 * +-------+-+-----------------------------------+-+
 * |       | |     -- data to be processed --    | |
 * +-------+-+-----------------------------------+-+
 * ^        ^                                     ^
 * ipbuf    ipbufstart                            ipbufsize
 */

   uchar_t *ipbuf;              /* image data in buffer */
   int ipbufstart;              /* first valid image byte */
   int ipbufsize;               /* size of valid data in */

   enum prog_state state;       /* FSM current state  */

   uchar_t *linebuf;            /* o/p raster data */

} DilloPng;

#define DATASIZE  (png->ipbufsize - png->ipbufstart)


static
void Png_error_handling(png_structp png_ptr, png_const_charp msg)
{
   DilloPng *png;

   MSG("Png_error_handling: %s\n", msg);
   png = png_get_error_ptr(png_ptr);

   png->error = 1;
   png->state = IS_finished;

   longjmp(png->jmpbuf, 1);
}

static void
Png_datainfo_callback(png_structp png_ptr, png_infop info_ptr)
{
   DilloPng *png;
   int color_type;
   int bit_depth;
   int interlace_type;
   uint_t i;
   double file_gamma = 1 / 2.2;

   _MSG("Png_datainfo_callback:\n");

   png = png_get_progressive_ptr(png_ptr);
   dReturn_if_fail (png != NULL);

   png_get_IHDR(png_ptr, info_ptr, &png->width, &png->height,
                &bit_depth, &color_type, &interlace_type, NULL, NULL);

   /* check max image size */
   if (png->width == 0 || png->height == 0 ||
       png->width > IMAGE_MAX_AREA / png->height) {
      MSG("Png_datainfo_callback: suspicious image size request %lu x %lu\n",
          (ulong_t) png->width, (ulong_t) png->height);
      Png_error_handling(png_ptr, "Aborting...");
      return; /* not reached */
   }

   _MSG("Png_datainfo_callback: png->width  = %lu\n"
        "Png_datainfo_callback: png->height = %lu\n",
        (ulong_t) png->width, (ulong_t) png->height);

   /* we need RGB/RGBA in the end */
   if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8) {
      /* Convert indexed images to RGB */
      png_set_expand (png_ptr);
   } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
      /* Convert grayscale to RGB */
      png_set_expand (png_ptr);
   } else if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) {
      /* We have transparency header, convert it to alpha channel */
      png_set_expand(png_ptr);
   } else if (bit_depth < 8) {
      png_set_expand(png_ptr);
   }

   if (bit_depth == 16) {
      png_set_strip_16(png_ptr);
   }

   /* Get and set gamma information. Beware: gamma correction 2.2 will
      only work on PC's. TODO: select screen gamma correction for other
      platforms. */
   if (png_get_gAMA(png_ptr, info_ptr, &file_gamma))
      png_set_gamma(png_ptr, 2.2, file_gamma);

   /* Convert gray scale to RGB */
   if (color_type == PNG_COLOR_TYPE_GRAY ||
       color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
      png_set_gray_to_rgb(png_ptr);
   }

   /* Interlaced */
   if (interlace_type != PNG_INTERLACE_NONE) {
      png_set_interlace_handling(png_ptr);
   }

   /* get libpng to update its state */
   png_read_update_info(png_ptr, info_ptr);

   png_get_IHDR(png_ptr, info_ptr, &png->width, &png->height,
                &bit_depth, &color_type, &interlace_type, NULL, NULL);

   png->rowbytes = png_get_rowbytes(png_ptr, info_ptr);
   png->channels = png_get_channels(png_ptr, info_ptr);

   /* init Dillo specifics */
   _MSG("Png_datainfo_callback: rowbytes = %d\n"
        "Png_datainfo_callback: width    = %lu\n"
        "Png_datainfo_callback: height   = %lu\n",
        png->rowbytes, (ulong_t) png->width, (ulong_t) png->height);

   png->image_data = (uchar_t *) dMalloc(png->rowbytes * png->height);
   png->row_pointers = (uchar_t **) dMalloc(png->height * sizeof(uchar_t *));

   for (i = 0; i < png->height; i++)
      png->row_pointers[i] = png->image_data + (i * png->rowbytes);

   png->linebuf = dMalloc(3 * png->width);

   /* Initialize the dicache-entry here */
   a_Dicache_set_parms(png->url, png->version, png->Image,
                       (uint_t)png->width, (uint_t)png->height,
                       DILLO_IMG_TYPE_RGB, file_gamma);
}

static void
 Png_datarow_callback(png_structp png_ptr, png_bytep new_row,
                      png_uint_32 row_num, int pass)
{
   DilloPng *png;
   uint_t i;

   if (!new_row)                /* work to do? */
      return;

   _MSG("Png_datarow_callback: row_num = %ld\n", row_num);

   png = png_get_progressive_ptr(png_ptr);

   png_progressive_combine_row(png_ptr, png->row_pointers[row_num], new_row);

   _MSG("png: row_num=%u previous_row=%u\n", row_num, png->previous_row);
   if (row_num < png->previous_row) {
      a_Dicache_new_scan(png->url, png->version);
   }
   png->previous_row = row_num;

   switch (png->channels) {
   case 3:
      a_Dicache_write(png->url, png->version,
                      png->image_data + (row_num * png->rowbytes),
                      (uint_t)row_num);
      break;
   case 4:
     {
        /* TODO: get the backgound color from the parent
         * of the image widget -- Livio.                 */
        int a, bg_red, bg_green, bg_blue;
        uchar_t *pl = png->linebuf;
        uchar_t *data = png->image_data + (row_num * png->rowbytes);

        /* TODO: maybe change prefs.bg_color to `a_Dw_widget_get_bg_color`,
         * when background colors are correctly implementated */
        bg_blue  = (png->Image->bg_color) & 0xFF;
        bg_green = (png->Image->bg_color>>8) & 0xFF;
        bg_red   = (png->Image->bg_color>>16) & 0xFF;

        for (i = 0; i < png->width; i++) {
           a = *(data+3);

           if (a == 255) {
              *(pl++) = *(data++);
              *(pl++) = *(data++);
              *(pl++) = *(data++);
              data++;
           } else if (a == 0) {
              *(pl++) = bg_red;
              *(pl++) = bg_green;
              *(pl++) = bg_blue;
              data += 4;
           } else {
              png_composite(*(pl++), *(data++), a, bg_red);
              png_composite(*(pl++), *(data++), a, bg_green);
              png_composite(*(pl++), *(data++), a, bg_blue);
              data++;
           }
        }
        a_Dicache_write(png->url, png->version, png->linebuf, (uint_t)row_num);
        break;
     }
   default:
      MSG("Png_datarow_callback: unexpected number of channels=%d pass=%d\n",
          png->channels, pass);
      abort();
   }
}

static void Png_dataend_callback(png_structp png_ptr, png_infop info_ptr)
{
   DilloPng *png;

   _MSG("Png_dataend_callback:\n");
   if (!info_ptr)
      MSG("Png_dataend_callback: info_ptr = NULL\n");

   png = png_get_progressive_ptr(png_ptr);
   png->state = IS_finished;
}

/*
 * Free up the resources for this image.
 */
static void Png_free(DilloPng *png)
{
   _MSG("Png_free: png=%p\n", png);

   dFree(png->image_data);
   dFree(png->row_pointers);
   dFree(png->linebuf);
   if (setjmp(png->jmpbuf))
      MSG_WARN("PNG: can't destroy read structure\n");
   else if (png->png_ptr)
      png_destroy_read_struct(&png->png_ptr, &png->info_ptr, NULL);
   dFree(png);
}

/*
 * Finish the decoding process (and free the memory)
 */
static void Png_close(DilloPng *png, CacheClient_t *Client)
{
   _MSG("Png_close\n");
   /* Let dicache know decoding is over */
   a_Dicache_close(png->url, png->version, Client);
   Png_free(png);
}

/*
 * Receive and process new chunks of PNG image data
 */
static void Png_write(DilloPng *png, void *Buf, uint_t BufSize)
{
   dReturn_if_fail ( Buf != NULL && BufSize > 0 );

   /* Keep local copies so we don't have to pass multiple args to
    * a number of functions. */
   png->ipbuf = Buf;
   png->ipbufsize = BufSize;

   /* start/resume the FSM here */
   while (png->state != IS_finished && DATASIZE) {
      _MSG("State = %s\n", prog_state_name[png->state]);

      switch (png->state) {
      case IS_init:
         if (DATASIZE < 8) {
            return;            /* need MORE data */
         }
         /* check the image signature - DON'T update ipbufstart! */
         if (png_sig_cmp(png->ipbuf, 0, DATASIZE)) {
            MSG_WARN("\"%s\" is not a PNG file.\n", URL_STR(png->url));
            png->state = IS_finished;
            break;
         }
         /* OK, it looks like a PNG image, lets do some set up stuff */
         png->png_ptr = png_create_read_struct(
                           PNG_LIBPNG_VER_STRING,
                           png,
                           (png_error_ptr)Png_error_handling,
                           (png_error_ptr)Png_error_handling);
         dReturn_if_fail (png->png_ptr != NULL);
         png->info_ptr = png_create_info_struct(png->png_ptr);
         dReturn_if_fail (png->info_ptr != NULL);

         setjmp(png->jmpbuf);
         if (!png->error) {
            png_set_progressive_read_fn(
               png->png_ptr,
               png,
               Png_datainfo_callback,   /* performs local init functions */
               Png_datarow_callback,    /* performs per row action */
               Png_dataend_callback);   /* performs cleanup actions */
            png->state = IS_nextdata;
         }
         break;

      case IS_nextdata:
         if (setjmp(png->jmpbuf)) {
            png->state = IS_finished;
         } else if (!png->error) {
            png_process_data( png->png_ptr,
                              png->info_ptr,
                              png->ipbuf + png->ipbufstart,
                              (png_size_t)DATASIZE);

            png->ipbufstart += DATASIZE;
         }
         break;

      default:
         MSG_WARN("PNG decoder: bad state = %d\n", png->state);
         abort();
      }
   }
}

/*
 * Op:  Operation to perform.
 *   If (Op == 0)
 *      start or continue processing an image if image data exists.
 *   else
 *       terminate processing, cleanup any allocated memory,
 *       close down the decoding process.
 *
 * Client->CbData  : pointer to previously allocated DilloPng work area.
 *  This holds the current state of the image processing and is kept
 *  across calls to this routine.
 * Client->Buf     : Pointer to data start.
 * Client->BufSize : the size of the data buffer.
 *
 * You have to keep track of where you are in the image data and
 * how much has been processed.
 *
 * It's entirely possible that you will not see the end of the data.  The
 * user may terminate transfer via a Stop button or there may be a network
 * failure.  This means that you can't just wait for all the data to be
 * presented before starting conversion and display.
 */
void a_Png_callback(int Op, void *data)
{
   if (Op == CA_Send) {
      CacheClient_t *Client = data;
      Png_write(Client->CbData, Client->Buf, Client->BufSize);
   } else if (Op == CA_Close) {
      CacheClient_t *Client = data;
      Png_close(Client->CbData, Client);
   } else if (Op == CA_Abort) {
      Png_free(data);
   }
}

/*
 * Create the image state data that must be kept between calls
 */
void *a_Png_new(DilloImage *Image, DilloUrl *url, int version)
{
   DilloPng *png = dNew0(DilloPng, 1);
   _MSG("a_Png_new: png=%p\n", png);

   png->Image = Image;
   png->url = url;
   png->version = version;
   png->error = 0;
   png->ipbuf = NULL;
   png->ipbufstart = 0;
   png->ipbufsize = 0;
   png->state = IS_init;
   png->linebuf = NULL;
   png->image_data = NULL;
   png->row_pointers = NULL;
   png->previous_row = 0;

   return png;
}

#else /* ENABLE_PNG */

void *a_Png_new() { return 0; }
void a_Png_callback() { return; }

#endif /* ENABLE_PNG */