summaryrefslogtreecommitdiffstats
path: root/libdimension/png.c
blob: 0c6ffddc9da67f48c29fb1ec206f8d9adf87333c (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*************************************************************************
 * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com>     *
 *                                                                       *
 * This file is part of The Dimension Library.                           *
 *                                                                       *
 * The Dimension Library is free software; you can redistribute it and/  *
 * or modify it under the terms of the GNU Lesser General Public License *
 * as published by the Free Software Foundation; either version 3 of the *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension Library 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  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

/**
 * @file
 * PNG import/export.
 */

#include "dimension-impl.h"
#include <png.h>
#include <errno.h>
#include <setjmp.h>
#include <stdlib.h>
#include <stdint.h>

/** PNG optimizer callback. */
static void dmnsn_png_optimizer_fn(const dmnsn_canvas *canvas,
                                   dmnsn_canvas_optimizer optimizer,
                                   size_t x, size_t y);

/* Optimize canvas for PNG exporting */
int
dmnsn_png_optimize_canvas(dmnsn_canvas *canvas)
{
  /* Check if we've already optimized this canvas */
  DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, canvas->optimizers) {
    if (i->optimizer_fn == dmnsn_png_optimizer_fn) {
      return 0;
    }
  }

  dmnsn_canvas_optimizer optimizer;
  optimizer.optimizer_fn = dmnsn_png_optimizer_fn;
  optimizer.free_fn = dmnsn_free;

  optimizer.ptr = dmnsn_malloc(4*canvas->width*canvas->height*sizeof(uint16_t));

  dmnsn_optimize_canvas(canvas, optimizer);
  return 0;
}

/* PNG optimizer callback */
static void
dmnsn_png_optimizer_fn(const dmnsn_canvas *canvas,
                       dmnsn_canvas_optimizer optimizer, size_t x, size_t y)
{
  dmnsn_color color;
  uint16_t *pixel = (uint16_t *)optimizer.ptr + 4*(y*canvas->width + x);

  color = dmnsn_get_pixel(canvas, x, y);
  color = dmnsn_remove_filter(color);
  color = dmnsn_color_to_sRGB(color);

  /* Saturate R, G, and B to [0, UINT16_MAX] */

  if (color.R <= 0.0) {
    pixel[0] = 0;
  } else if (color.R >= 1.0) {
    pixel[0] = UINT16_MAX;
  } else {
    pixel[0] = color.R*UINT16_MAX;
  }

  if (color.G <= 0.0) {
    pixel[1] = 0;
  } else if (color.G >= 1.0) {
    pixel[1] = UINT16_MAX;
  } else {
    pixel[1] = color.G*UINT16_MAX;
  }

  if (color.B <= 0.0) {
    pixel[2] = 0;
  } else if (color.B >= 1.0) {
    pixel[2] = UINT16_MAX;
  } else {
    pixel[2] = color.B*UINT16_MAX;
  }

  if (color.trans <= 0.0) {
    pixel[3] = 0;
  } else if (color.trans >= 1.0) {
    pixel[3] = UINT16_MAX;
  } else {
    pixel[3] = color.trans*UINT16_MAX;
  }
}

/** Payload type for PNG write thread callback. */
typedef struct {
  dmnsn_progress *progress;
  const dmnsn_canvas *canvas;
  FILE *file;
} dmnsn_png_write_payload;

/** Payload type for PNG read thread callback. */
typedef struct {
  dmnsn_progress *progress;
  dmnsn_canvas **canvas;
  FILE *file;
} dmnsn_png_read_payload;

/** PNG write thread callback. */
static int dmnsn_png_write_canvas_thread(void *ptr);
/** PNG read thread callback. */
static int dmnsn_png_read_canvas_thread(void *ptr);

/* Write a canvas to a png file, using libpng.  Return 0 on success, nonzero on
   failure. */
int
dmnsn_png_write_canvas(const dmnsn_canvas *canvas, FILE *file)
{
  dmnsn_progress *progress = dmnsn_png_write_canvas_async(canvas, file);
  return dmnsn_finish_progress(progress);
}

/* Write a canvas to a png file in the background */
dmnsn_progress *
dmnsn_png_write_canvas_async(const dmnsn_canvas *canvas, FILE *file)
{
  dmnsn_progress *progress = dmnsn_new_progress();

  dmnsn_png_write_payload *payload
    = dmnsn_malloc(sizeof(dmnsn_png_write_payload));
  payload->progress = progress;
  payload->canvas   = canvas;
  payload->file     = file;

  /* Create the worker thread */
  dmnsn_new_thread(progress, dmnsn_png_write_canvas_thread, payload);

  return progress;
}

/* Read a canvas from the PNG file `file'.  Return NULL on error. */
dmnsn_canvas *
dmnsn_png_read_canvas(FILE *file)
{
  dmnsn_canvas *canvas;
  dmnsn_progress *progress = dmnsn_png_read_canvas_async(&canvas, file);
  dmnsn_finish_progress(progress);
  return canvas;
}

/* Read a canvas from a png file in the background */
dmnsn_progress *
dmnsn_png_read_canvas_async(dmnsn_canvas **canvas, FILE *file)
{
  dmnsn_progress *progress = dmnsn_new_progress();
  dmnsn_png_read_payload *payload
    = dmnsn_malloc(sizeof(dmnsn_png_write_payload));

  payload->progress = progress;
  payload->canvas   = canvas;
  *payload->canvas  = NULL;
  payload->file     = file;

  /* Create the worker thread */
  dmnsn_new_thread(progress, dmnsn_png_read_canvas_thread, payload);

  return progress;
}

/*
 * Thread callbacks
 */

/* Write a PNG file */
static int
dmnsn_png_write_canvas_thread(void *ptr)
{
  dmnsn_png_write_payload *payload = ptr;

  png_uint_32 width = payload->canvas->width;
  png_uint_32 height = payload->canvas->height;

  dmnsn_set_progress_total(payload->progress, height);

  png_structp png_ptr
    = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (!png_ptr) {
    /* Couldn't create libpng write struct */
    dmnsn_free(payload);
    return -1;
  }

  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    /* Couldn't create libpng info struct */
    png_destroy_write_struct(&png_ptr, NULL);
    dmnsn_free(payload);
    return -1;
  }

  /* libpng will longjmp here if it encounters an error from here on */
  uint16_t *row = NULL;
  if (setjmp(png_jmpbuf(png_ptr))) {
    /* libpng error */
    dmnsn_free(row);
    png_destroy_write_struct(&png_ptr, &info_ptr);
    dmnsn_free(payload);
    return -1;
  }

  /* Associate file with the libpng write struct */
  png_init_io(png_ptr, payload->file);

  /* Set header correctly for 16-bit sRGB image */
  png_set_IHDR(png_ptr, info_ptr, width, height, 16,
               PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
               PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, PNG_sRGB_INTENT_ABSOLUTE);

  /* We think of transparency in the opposite way that PNG does */
  png_set_invert_alpha(png_ptr);

  /* Write the info struct */
  png_write_info(png_ptr, info_ptr);

  if (dmnsn_is_little_endian()) {
    /* We are little-endian; swap the byte order of the pixels */
    png_set_swap(png_ptr);
  }

  /* Check if we can optimize this */
  DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i,
                       payload->canvas->optimizers)
  {
    if (i->optimizer_fn == dmnsn_png_optimizer_fn) {
      for (size_t y = 0; y < height; ++y) {
        /* Invert the rows.  PNG coordinates are fourth quadrant. */
        uint16_t *row = (uint16_t *)i->ptr + 4*(height - y - 1)*width;
        png_write_row(png_ptr, (png_bytep)row);
        dmnsn_increment_progress(payload->progress);
      }

      /* Finish the PNG file */
      png_write_end(png_ptr, info_ptr);
      png_destroy_write_struct(&png_ptr, &info_ptr);
      dmnsn_free(payload);
      return 0;
    }
  }

  /* Allocate the temporary row of RGBA values */
  row = dmnsn_malloc(4*sizeof(uint16_t)*width);

  /* Write the pixels */
  for (size_t y = 0; y < height; ++y) {
    for (size_t x = 0; x < width; ++x) {
      /* Invert the rows.  PNG coordinates are fourth quadrant. */
      dmnsn_color color = dmnsn_get_pixel(payload->canvas, x, height - y - 1);
      color = dmnsn_remove_filter(color);
      color = dmnsn_color_to_sRGB(color);

      /* Saturate R, G, and B to [0, UINT16_MAX] */

      if (color.R <= 0.0) {
        row[4*x] = 0;
      } else if (color.R >= 1.0) {
        row[4*x] = UINT16_MAX;
      } else {
        row[4*x] = color.R*UINT16_MAX;
      }

      if (color.G <= 0.0) {
        row[4*x + 1] = 0;
      } else if (color.G >= 1.0) {
        row[4*x + 1] = UINT16_MAX;
      } else {
        row[4*x + 1] = color.G*UINT16_MAX;
      }

      if (color.B <= 0.0) {
        row[4*x + 2] = 0;
      } else if (color.B >= 1.0) {
        row[4*x + 2] = UINT16_MAX;
      } else {
        row[4*x + 2] = color.B*UINT16_MAX;
      }

      if (color.trans <= 0.0) {
        row[4*x + 3] = 0;
      } else if (color.trans >= 1.0) {
        row[4*x + 3] = UINT16_MAX;
      } else {
        row[4*x + 3] = color.trans*UINT16_MAX;
      }
    }

    /* Write the row */
    png_write_row(png_ptr, (png_bytep)row);
    dmnsn_increment_progress(payload->progress);
  }

  /* Finish the PNG file */
  png_write_end(png_ptr, info_ptr);

  dmnsn_free(row);
  png_destroy_write_struct(&png_ptr, &info_ptr);
  dmnsn_free(payload);
  return 0;
}

/** Thread-specific pointer to the appropriate dmnsn_progress* for
    dmnsn_png_read_row_callback. */
static __thread dmnsn_progress *dmnsn_tl_png_read_progress;

/** Callback to increment the progress after a row has been read. */
static void
dmnsn_png_read_row_callback(png_structp png_ptr, png_uint_32 row, int pass)
{
  dmnsn_increment_progress(dmnsn_tl_png_read_progress);
}

/* Read a PNG file */
static int
dmnsn_png_read_canvas_thread(void *ptr)
{
  dmnsn_png_read_payload *payload = ptr;
  dmnsn_tl_png_read_progress = payload->progress;

  png_byte header[8];
  if (fread(header, 1, 8, payload->file) != 8) {
    dmnsn_free(payload);
    return -1;
  }
  if (png_sig_cmp(header, 0, 8)) {
    /* payload->file is not a PNG file */
    dmnsn_free(payload);
    errno = EINVAL;
    return -1;
  }

  /* Create the libpng read struct */
  png_structp png_ptr
    = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (!png_ptr) {
    dmnsn_free(payload);
    return -1;
  }

  /* Create the libpng info struct */
  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    png_destroy_read_struct(&png_ptr, NULL, NULL);
    dmnsn_free(payload);
    return -1;
  }

  /* libpng will longjmp here if it encounters an error from here on */
  png_bytep image = NULL;
  png_bytep *row_pointers = NULL;
  if (setjmp(png_jmpbuf(png_ptr))) {
    /* libpng error */
    dmnsn_free(row_pointers);
    dmnsn_free(image);
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    dmnsn_free(payload);
    return -1;
  }

  /* Associate the read struct with the file, and tell it we've already checked
     8 bytes of signature */
  png_init_io(png_ptr, payload->file);
  png_set_sig_bytes(png_ptr, 8);

  /* Read the PNG header into info struct */
  png_read_info(png_ptr, info_ptr);

  /* Get useful information from the info struct */
  png_uint_32 width, height;
  int bit_depth, color_type, interlace_type, compression_type, filter_method;
  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
               &interlace_type, &compression_type, &filter_method);
  int number_of_passes = png_set_interlace_handling(png_ptr);

  dmnsn_set_progress_total(payload->progress, (number_of_passes + 1)*height);
  png_set_read_status_fn(png_ptr, dmnsn_png_read_row_callback);

  /*
   * - Convert paletted images to RGB.
   * - Convert a tRNS chunk to an alpha channel
   * - Convert grayscale to RGB
   * - Invert the alpha channel
   */
  if (color_type == PNG_COLOR_TYPE_PALETTE) {
    png_set_palette_to_rgb(png_ptr);
  }
  if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
    png_set_tRNS_to_alpha(png_ptr);
  }
  if (color_type == PNG_COLOR_TYPE_GRAY
      || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  {
    png_set_gray_to_rgb(png_ptr);
  }
  png_set_invert_alpha(png_ptr);

  /* Update the info struct */
  png_read_update_info(png_ptr, info_ptr);

  /* Get bytes/image row */
  png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);

  /* Allocate the temporary image buffer */
  image = dmnsn_malloc(rowbytes*height);

  /* Allocate and set an array of pointers to rows in image */
  row_pointers = dmnsn_malloc(sizeof(png_bytep)*height);

  for (size_t y = 0; y < height; ++y) {
    row_pointers[y] = image + y*rowbytes;
  }

  /* Read the image to memory all at once.  At the expense of greater memory
     use, this handles interlacing for us. */
  png_read_image(png_ptr, row_pointers);

  /* Allocate the canvas */
  *payload->canvas = dmnsn_new_canvas(width, height);

  /* Now we convert the image to our canvas format.  This depends on the image
     bit depth (which has been scaled up to at least 8 or 16), and the presence
     of an alpha channel. */
  for (size_t y = 0; y < height; ++y) {
    for (size_t x = 0; x < width; ++x) {
      dmnsn_color color;
      color.filter = 0.0;

      if (color_type & PNG_COLOR_MASK_ALPHA) {
        if (bit_depth == 16) {
          png_bytep png_pixel = image + 8*(y*width + x);
          color.R = (double)((png_pixel[0] << 8) + png_pixel[1])/UINT16_MAX;
          color.G = (double)((png_pixel[2] << 8) + png_pixel[3])/UINT16_MAX;
          color.B = (double)((png_pixel[4] << 8) + png_pixel[5])/UINT16_MAX;
          color.trans = (double)((png_pixel[6] << 8) + png_pixel[7])/UINT16_MAX;
        } else {
          png_bytep png_pixel = image + 4*(y*width + x);
          color.R = (double)png_pixel[0]/UINT16_MAX;
          color.G = (double)png_pixel[1]/UINT16_MAX;
          color.B = (double)png_pixel[2]/UINT16_MAX;
          color.trans = (double)png_pixel[3]/UINT16_MAX;
        }
      } else {
        color.trans = 0.0;

        if (bit_depth == 16) {
          png_bytep png_pixel = image + 6*(y*width + x);
          color.R = (double)((png_pixel[0] << 8) + png_pixel[1])/UINT16_MAX;
          color.G = (double)((png_pixel[2] << 8) + png_pixel[3])/UINT16_MAX;
          color.B = (double)((png_pixel[4] << 8) + png_pixel[5])/UINT16_MAX;
        } else {
          png_bytep png_pixel = image + 3*(y*width + x);
          color.R = (double)png_pixel[0]/UINT16_MAX;
          color.G = (double)png_pixel[1]/UINT16_MAX;
          color.B = (double)png_pixel[2]/UINT16_MAX;
        }
      }

      color = dmnsn_color_from_sRGB(color);
      dmnsn_set_pixel(*payload->canvas, x, height - y - 1, color);
    }

    dmnsn_increment_progress(payload->progress);
  }

  dmnsn_free(row_pointers);
  dmnsn_free(image);
  png_read_end(png_ptr, NULL);
  png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  dmnsn_free(payload);
  return 0;
}