summaryrefslogtreecommitdiff
path: root/src/modules/loaders/loader_png.c
blob: bd6777bace8bdb81c596acb567c09416e64811d7 (plain)
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
455
456
457
458
459
460
#include "loader_common.h"

#include <png.h>
#include <stdint.h>
#include <sys/mman.h>

#define DBG_PFX "LDR-png"

#define USE_IMLIB2_COMMENT_TAG 0

#define _PNG_MIN_SIZE	60      /* Min. PNG file size (8 + 3*12 + 13 (+3) */
#define _PNG_SIG_SIZE	8       /* Signature size */

typedef struct {
   ImlibImage         *im;
   char                load_data;
   char                rc;

   char                interlace;
} ctx_t;

#if 0
static const unsigned char png_sig[] = {
   0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a
};
#endif

#if USE_IMLIB2_COMMENT_TAG
static void
comment_free(ImlibImage * im, void *data)
{
   free(data);
}
#endif

static void
user_error_fn(png_struct * png_ptr, const char *txt)
{
#if 0
   D("%s: %s\n", __func__, txt);
#endif
}

static void
user_warning_fn(png_struct * png_ptr, const char *txt)
{
   D("%s: %s\n", __func__, txt);
}

static void
info_callback(png_struct * png_ptr, png_info * info_ptr)
{
   int                 rc;
   ctx_t              *ctx = png_get_progressive_ptr(png_ptr);
   ImlibImage         *im = ctx->im;
   png_uint_32         w32, h32;
   int                 bit_depth, color_type, interlace_type;
   int                 hasa;

   rc = LOAD_BADIMAGE;          /* Format accepted */

   png_get_IHDR(png_ptr, info_ptr, &w32, &h32, &bit_depth, &color_type,
                &interlace_type, NULL, NULL);

   D("%s: WxH=%dx%d depth=%d color=%d interlace=%d\n", __func__,
     w32, h32, bit_depth, color_type, interlace_type);

   im->w = w32;
   im->h = h32;

   if (!IMAGE_DIMENSIONS_OK(w32, h32))
      goto quit;

   hasa = 0;
   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
      hasa = 1;
   if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
      hasa = 1;
   if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
      hasa = 1;
   UPDATE_FLAG(im->flags, F_HAS_ALPHA, hasa);

   if (!ctx->load_data)
      QUIT_WITH_RC(LOAD_SUCCESS);

   /* Load data */

   /* Prep for transformations...  ultimately we want ARGB */
   /* expand palette -> RGB if necessary */
   if (color_type == PNG_COLOR_TYPE_PALETTE)
      png_set_palette_to_rgb(png_ptr);
   /* expand gray (w/reduced bits) -> 8-bit RGB if necessary */
   if ((color_type == PNG_COLOR_TYPE_GRAY) ||
       (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
     {
        png_set_gray_to_rgb(png_ptr);
        if (bit_depth < 8)
           png_set_expand_gray_1_2_4_to_8(png_ptr);
     }
   /* expand transparency entry -> alpha channel if present */
   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
      png_set_tRNS_to_alpha(png_ptr);
   /* reduce 16bit color -> 8bit color if necessary */
   if (bit_depth > 8)
      png_set_strip_16(png_ptr);
   /* pack all pixels to byte boundaries */
   png_set_packing(png_ptr);

/* note from raster:                                                         */
/* thanks to mustapha for helping debug this on PPC Linux remotely by        */
/* sending across screenshots all the time and me figuring out from them     */
/* what the hell was up with the colors                                      */
/* now png loading should work on big-endian machines nicely                 */
#ifdef WORDS_BIGENDIAN
   png_set_swap_alpha(png_ptr);
   if (!hasa)
      png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE);
#else
   png_set_bgr(png_ptr);
   if (!hasa)
      png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
#endif

   /* NB! If png_read_update_info() isn't called processing stops here */
   png_read_update_info(png_ptr, info_ptr);

   if (!__imlib_AllocateData(im))
      QUIT_WITH_RC(LOAD_OOM);

   rc = LOAD_SUCCESS;

 quit:
   ctx->rc = rc;
   if (!ctx->load_data || rc != LOAD_SUCCESS)
      png_longjmp(png_ptr, 1);
}

static void
row_callback(png_struct * png_ptr, png_byte * new_row,
             png_uint_32 row_num, int pass)
{
   ctx_t              *ctx = png_get_progressive_ptr(png_ptr);
   ImlibImage         *im = ctx->im;
   DATA32             *dptr;
   const DATA32       *sptr;
   int                 x, y, x0, dx, y0, dy;
   int                 done;

   DL("%s: png=%p data=%p row=%d, pass=%d\n", __func__, png_ptr, new_row,
      row_num, pass);

   if (!im->data)
      return;

   if (ctx->interlace)
     {
        x0 = PNG_PASS_START_COL(pass);
        dx = PNG_PASS_COL_OFFSET(pass);
        y0 = PNG_PASS_START_ROW(pass);
        dy = PNG_PASS_ROW_OFFSET(pass);

        DL("x0, dx = %d,%d y0,dy = %d,%d cols/rows=%d/%d\n",
           x0, dx, y0, dy,
           PNG_PASS_COLS(im->w, pass), PNG_PASS_ROWS(im->h, pass));
        y = y0 + dy * row_num;

        sptr = (const DATA32 *)new_row; /* Assuming aligned */
        dptr = im->data + y * im->w;
        for (x = x0; x < im->w; x += dx)
          {
#if 0
             dptr[x] = PIXEL_ARGB(new_row[ii + 3], new_row[ii + 2],
                                  new_row[ii + 1], new_row[ii + 0]);
             D("x,y = %d,%d (i,j, ii=%d,%d %d): %08x\n", x, y,
               ii / 4, row_num, ii, dptr[x]);
#else
             dptr[x] = *sptr++;
#endif
          }

        done = pass >= 6 && (int)row_num >= PNG_PASS_ROWS(im->h, pass) - 1;
        if (im->lc && done)
           __imlib_LoadProgress(im, im->frame_x, im->frame_y, im->w, im->h);
     }
   else
     {
        y = row_num;

        dptr = im->data + y * im->w;
        memcpy(dptr, new_row, sizeof(DATA32) * im->w);

        done = (int)row_num >= im->h - 1;

        if (im->lc)
          {
             if (im->frame_count > 1)
               {
                  if (done)
                     __imlib_LoadProgress(im, im->frame_x, im->frame_y, im->w,
                                          im->h);
               }
             else if (__imlib_LoadProgressRows(im, y, 1))
               {
                  png_process_data_pause(png_ptr, 0);
                  ctx->rc = LOAD_BREAK;
               }
          }
     }
}

int
load2(ImlibImage * im, int load_data)
{
   int                 rc;
   void               *fdata;
   png_structp         png_ptr = NULL;
   png_infop           info_ptr = NULL;
   ctx_t               ctx = { 0 };

   /* read header */
   rc = LOAD_FAIL;

   if (im->fsize < _PNG_MIN_SIZE)
      return rc;

   fdata = mmap(NULL, im->fsize, PROT_READ, MAP_SHARED, fileno(im->fp), 0);
   if (fdata == MAP_FAILED)
      return LOAD_BADFILE;

   /* Signature check */
   if (png_sig_cmp(fdata, 0, _PNG_SIG_SIZE))
      goto quit;

   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
                                    user_error_fn, user_warning_fn);
   if (!png_ptr)
      goto quit;

   info_ptr = png_create_info_struct(png_ptr);
   if (!info_ptr)
      goto quit;

   rc = LOAD_BADIMAGE;          /* Format accepted */

   ctx.im = im;
   ctx.load_data = load_data;
   ctx.rc = rc;

   if (setjmp(png_jmpbuf(png_ptr)))
      QUIT_WITH_RC(ctx.rc);

   /* Set up processing via callbacks */
   png_set_progressive_read_fn(png_ptr, &ctx,
                               info_callback, row_callback, NULL);

   png_process_data(png_ptr, info_ptr, fdata, im->fsize);

   rc = ctx.rc;
   if (rc <= 0)
      goto quit;

   rc = LOAD_SUCCESS;

#if USE_IMLIB2_COMMENT_TAG
#ifdef PNG_TEXT_SUPPORTED
   {
      png_textp           text;
      int                 i, num;

      num = 0;
      png_get_text(png_ptr, info_ptr, &text, &num);
      for (i = 0; i < num; i++)
        {
           if (!strcmp(text[i].key, "Imlib2-Comment"))
              __imlib_AttachTag(im, "comment", 0, strdup(text[i].text),
                                comment_free);
        }
   }
#endif
#endif

 quit:
   png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
   if (rc <= 0)
      __imlib_FreeData(im);
   munmap(fdata, im->fsize);

   return rc;
}

char
save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity)
{
   int                 rc;
   FILE               *f;
   png_structp         png_ptr;
   png_infop           info_ptr;
   DATA32             *ptr;
   int                 x, y, j, interlace;
   png_bytep           row_ptr, data;
   png_color_8         sig_bit;
   ImlibImageTag      *tag;
   int                 quality = 75, compression = 3;
   int                 pass, n_passes = 1;
   int                 has_alpha;

   f = fopen(im->real_file, "wb");
   if (!f)
      return LOAD_FAIL;

   rc = LOAD_FAIL;
   info_ptr = NULL;
   data = NULL;

   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   if (!png_ptr)
      goto quit;

   info_ptr = png_create_info_struct(png_ptr);
   if (!info_ptr)
      goto quit;

   if (setjmp(png_jmpbuf(png_ptr)))
      goto quit;

   /* check whether we should use interlacing */
   interlace = PNG_INTERLACE_NONE;
   if ((tag = __imlib_GetTag(im, "interlacing")) && tag->val)
     {
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
        interlace = PNG_INTERLACE_ADAM7;
#endif
     }

   png_init_io(png_ptr, f);
   has_alpha = IMAGE_HAS_ALPHA(im);
   if (has_alpha)
     {
        png_set_IHDR(png_ptr, info_ptr, im->w, im->h, 8,
                     PNG_COLOR_TYPE_RGB_ALPHA, interlace,
                     PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#ifdef WORDS_BIGENDIAN
        png_set_swap_alpha(png_ptr);
#else
        png_set_bgr(png_ptr);
#endif
     }
   else
     {
        png_set_IHDR(png_ptr, info_ptr, im->w, im->h, 8, PNG_COLOR_TYPE_RGB,
                     interlace, PNG_COMPRESSION_TYPE_BASE,
                     PNG_FILTER_TYPE_BASE);
        data = malloc(im->w * 3 * sizeof(png_byte));
     }
   sig_bit.red = 8;
   sig_bit.green = 8;
   sig_bit.blue = 8;
   sig_bit.alpha = 8;
   png_set_sBIT(png_ptr, info_ptr, &sig_bit);

   /* quality */
   tag = __imlib_GetTag(im, "quality");
   if (tag)
     {
        quality = tag->val;
        if (quality < 1)
           quality = 1;
        if (quality > 99)
           quality = 99;
     }
   /* convert to compression */
   quality = quality / 10;
   compression = 9 - quality;
   /* compression */
   tag = __imlib_GetTag(im, "compression");
   if (tag)
      compression = tag->val;
   if (compression < 0)
      compression = 0;
   if (compression > 9)
      compression = 9;
   png_set_compression_level(png_ptr, compression);

#if USE_IMLIB2_COMMENT_TAG
   tag = __imlib_GetTag(im, "comment");
   if (tag)
     {
#ifdef PNG_TEXT_SUPPORTED
        png_text            text;

        text.key = (char *)"Imlib2-Comment";
        text.text = tag->data;
        text.compression = PNG_TEXT_COMPRESSION_zTXt;
        png_set_text(png_ptr, info_ptr, &(text), 1);
#endif
     }
#endif

   png_write_info(png_ptr, info_ptr);
   png_set_shift(png_ptr, &sig_bit);
   png_set_packing(png_ptr);

#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   n_passes = png_set_interlace_handling(png_ptr);
#endif

   for (pass = 0; pass < n_passes; pass++)
     {
        ptr = im->data;

        if (im->lc)
           __imlib_LoadProgressSetPass(im, pass, n_passes);

        for (y = 0; y < im->h; y++)
          {
             if (has_alpha)
                row_ptr = (png_bytep) ptr;
             else
               {
                  for (j = 0, x = 0; x < im->w; x++)
                    {
                       DATA32              pixel = ptr[x];

                       data[j++] = PIXEL_R(pixel);
                       data[j++] = PIXEL_G(pixel);
                       data[j++] = PIXEL_B(pixel);
                    }
                  row_ptr = (png_bytep) data;
               }
             png_write_rows(png_ptr, &row_ptr, 1);

             if (im->lc && __imlib_LoadProgressRows(im, y, 1))
                QUIT_WITH_RC(LOAD_BREAK);

             ptr += im->w;
          }
     }

   rc = LOAD_SUCCESS;

 quit:
   free(data);
   png_write_end(png_ptr, info_ptr);
   png_destroy_write_struct(&png_ptr, (png_infopp) & info_ptr);
   if (info_ptr)
      png_destroy_info_struct(png_ptr, (png_infopp) & info_ptr);
   if (png_ptr)
      png_destroy_write_struct(&png_ptr, (png_infopp) NULL);

   fclose(f);

   return rc;
}

void
formats(ImlibLoader * l)
{
   static const char  *const list_formats[] = { "png" };
   __imlib_LoaderSetFormats(l, list_formats, ARRAY_SIZE(list_formats));
}