2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2015, Matthieu Darbois
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_apps_config.h"
52 #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
54 /* PNG allows bits per sample: 1, 2, 4, 8, 16 */
56 typedef void (* convert_32s_CXPX)(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length);
57 static void convert_32s_C1P1(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
59 memcpy(pDst[0], pSrc, length * sizeof(OPJ_INT32));
61 static void convert_32s_C2P2(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
64 OPJ_INT32* pDst0 = pDst[0];
65 OPJ_INT32* pDst1 = pDst[1];
67 for (i = 0; i < length; i++) {
68 pDst0[i] = pSrc[2*i+0];
69 pDst1[i] = pSrc[2*i+1];
72 static void convert_32s_C3P3(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
75 OPJ_INT32* pDst0 = pDst[0];
76 OPJ_INT32* pDst1 = pDst[1];
77 OPJ_INT32* pDst2 = pDst[2];
79 for (i = 0; i < length; i++) {
80 pDst0[i] = pSrc[3*i+0];
81 pDst1[i] = pSrc[3*i+1];
82 pDst2[i] = pSrc[3*i+2];
85 static void convert_32s_C4P4(const OPJ_INT32* pSrc, OPJ_INT32* const* pDst, OPJ_SIZE_T length)
88 OPJ_INT32* pDst0 = pDst[0];
89 OPJ_INT32* pDst1 = pDst[1];
90 OPJ_INT32* pDst2 = pDst[2];
91 OPJ_INT32* pDst3 = pDst[3];
93 for (i = 0; i < length; i++) {
94 pDst0[i] = pSrc[4*i+0];
95 pDst1[i] = pSrc[4*i+1];
96 pDst2[i] = pSrc[4*i+2];
97 pDst3[i] = pSrc[4*i+3];
101 typedef void (* convert_XXx32s_C1R)(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length);
102 static void convert_1u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
105 for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
106 OPJ_UINT32 val = *pSrc++;
107 pDst[i+0] = (OPJ_INT32)( val >> 7);
108 pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
109 pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
110 pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
111 pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
112 pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
113 pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
114 pDst[i+7] = (OPJ_INT32)(val & 0x1U);
117 OPJ_UINT32 val = *pSrc++;
118 length = length & 7U;
119 pDst[i+0] = (OPJ_INT32)(val >> 7);
122 pDst[i+1] = (OPJ_INT32)((val >> 6) & 0x1U);
124 pDst[i+2] = (OPJ_INT32)((val >> 5) & 0x1U);
126 pDst[i+3] = (OPJ_INT32)((val >> 4) & 0x1U);
128 pDst[i+4] = (OPJ_INT32)((val >> 3) & 0x1U);
130 pDst[i+5] = (OPJ_INT32)((val >> 2) & 0x1U);
132 pDst[i+6] = (OPJ_INT32)((val >> 1) & 0x1U);
141 static void convert_2u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
144 for (i = 0; i < (length & ~(OPJ_SIZE_T)3U); i+=4U) {
145 OPJ_UINT32 val = *pSrc++;
146 pDst[i+0] = (OPJ_INT32)( val >> 6);
147 pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
148 pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
149 pDst[i+3] = (OPJ_INT32)(val & 0x3U);
152 OPJ_UINT32 val = *pSrc++;
153 length = length & 3U;
154 pDst[i+0] = (OPJ_INT32)(val >> 6);
157 pDst[i+1] = (OPJ_INT32)((val >> 4) & 0x3U);
159 pDst[i+2] = (OPJ_INT32)((val >> 2) & 0x3U);
165 static void convert_4u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
168 for (i = 0; i < (length & ~(OPJ_SIZE_T)1U); i+=2U) {
169 OPJ_UINT32 val = *pSrc++;
170 pDst[i+0] = (OPJ_INT32)(val >> 4);
171 pDst[i+1] = (OPJ_INT32)(val & 0xFU);
174 OPJ_UINT8 val = *pSrc++;
175 pDst[i+0] = (OPJ_INT32)(val >> 4);
178 static void convert_8u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
181 for (i = 0; i < length; i++) {
185 static void convert_16u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
188 for (i = 0; i < length; i++) {
189 OPJ_INT32 val0 = *pSrc++;
190 OPJ_INT32 val1 = *pSrc++;
191 pDst[i] = val0 << 8 | val1;
195 opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
200 int bit_depth, interlace_type,compression_type, filter_type;
202 png_uint_32 width, height;
205 OPJ_BYTE** rows = NULL;
206 OPJ_INT32* row32s = NULL;
208 opj_image_t *image = NULL;
209 opj_image_cmptparm_t cmptparm[4];
212 convert_XXx32s_C1R cvtXXTo32s = NULL;
213 convert_32s_CXPX cvtCxToPx = NULL;
214 OPJ_INT32* planes[4];
216 if((reader = fopen(read_idf, "rb")) == NULL)
218 fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
222 if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
223 || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
225 fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
229 if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
230 NULL, NULL, NULL)) == NULL)
232 if((info = png_create_info_struct(png)) == NULL)
235 if(setjmp(png_jmpbuf(png)))
238 png_init_io(png, reader);
239 png_set_sig_bytes(png, MAGIC_SIZE);
241 png_read_info(png, info);
243 if(png_get_IHDR(png, info, &width, &height,
244 &bit_depth, &color_type, &interlace_type,
245 &compression_type, &filter_type) == 0)
249 * expand paletted images to RGB, expand grayscale images of
250 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
253 if(color_type == PNG_COLOR_TYPE_PALETTE) {
257 if(png_get_valid(png, info, PNG_INFO_tRNS)) {
260 /* We might wan't to expand background */
262 if(png_get_valid(png, info, PNG_INFO_bKGD)) {
264 png_get_bKGD(png, info, &bgnd);
265 png_set_background(png, bgnd, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
269 if( !png_get_gAMA(png, info, &gamma))
272 /* we're not displaying but converting, screen gamma == 1.0 */
273 png_set_gamma(png, 1.0, gamma);
275 png_read_update_info(png, info);
277 color_type = png_get_color_type(png, info);
279 switch (color_type) {
280 case PNG_COLOR_TYPE_GRAY:
282 cvtCxToPx = convert_32s_C1P1;
284 case PNG_COLOR_TYPE_GRAY_ALPHA:
286 cvtCxToPx = convert_32s_C2P2;
288 case PNG_COLOR_TYPE_RGB:
290 cvtCxToPx = convert_32s_C3P3;
292 case PNG_COLOR_TYPE_RGB_ALPHA:
294 cvtCxToPx = convert_32s_C4P4;
297 fprintf(stderr,"pngtoimage: colortype %d is not supported\n", color_type);
300 bit_depth = png_get_bit_depth(png, info);
304 cvtXXTo32s = convert_1u32s_C1R;
307 cvtXXTo32s = convert_2u32s_C1R;
310 cvtXXTo32s = convert_4u32s_C1R;
313 cvtXXTo32s = convert_8u32s_C1R;
316 cvtXXTo32s = convert_16u32s_C1R;
319 fprintf(stderr,"pngtoimage: bit depth %d is not supported\n", bit_depth);
324 rows = (OPJ_BYTE**)calloc(height+1, sizeof(OPJ_BYTE*));
325 for(i = 0; i < height; ++i)
326 rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png,info));
328 png_read_image(png, rows);
331 memset(cmptparm, 0, sizeof(cmptparm));
332 for(i = 0; i < nr_comp; ++i)
334 cmptparm[i].prec = (OPJ_UINT32)bit_depth;
335 /* bits_per_pixel: 8 or 16 */
336 cmptparm[i].bpp = (OPJ_UINT32)bit_depth;
337 cmptparm[i].sgnd = 0;
338 cmptparm[i].dx = (OPJ_UINT32)params->subsampling_dx;
339 cmptparm[i].dy = (OPJ_UINT32)params->subsampling_dy;
340 cmptparm[i].w = (OPJ_UINT32)width;
341 cmptparm[i].h = (OPJ_UINT32)height;
344 image = opj_image_create(nr_comp, &cmptparm[0], (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY);
345 if(image == NULL) goto fin;
346 image->x0 = (OPJ_UINT32)params->image_offset_x0;
347 image->y0 = (OPJ_UINT32)params->image_offset_y0;
348 image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32)params->subsampling_dx + 1 + image->x0);
349 image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)params->subsampling_dy + 1 + image->y0);
351 row32s = malloc((size_t)width * nr_comp * sizeof(OPJ_INT32));
352 if(row32s == NULL) goto fin;
354 /* Set alpha channel */
355 image->comps[nr_comp-1U].alpha = 1U - (nr_comp & 1U);
357 for(i = 0; i < nr_comp; i++)
359 planes[i] = image->comps[i].data;
362 for(i = 0; i < height; ++i)
364 cvtXXTo32s(rows[i], row32s, (OPJ_SIZE_T)width * nr_comp);
365 cvtCxToPx(row32s, planes, width);
374 for(i = 0; i < height; ++i)
382 png_destroy_read_struct(&png, &info, NULL);
390 int imagetopng(opj_image_t * image, const char *write_idf)
395 int *red, *green, *blue, *alpha;
396 unsigned char *row_buf, *d;
397 int has_alpha, width, height, nr_comp, color_type;
398 int adjustR, adjustG, adjustB, adjustA, x, y, fails;
399 int prec, ushift, dshift, is16, force16, force8;
400 unsigned short mask = 0xffff;
403 is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
404 prec = (int)image->comps[0].prec;
405 nr_comp = (int)image->numcomps;
407 if(prec > 8 && prec < 16)
409 ushift = 16 - prec; dshift = prec - ushift;
410 prec = 16; force16 = 1;
413 if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
415 ushift = 8 - prec; dshift = 8 - ushift;
416 prec = 8; force8 = 1;
419 if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
421 fprintf(stderr,"imagetopng: can not create %s"
422 "\n\twrong bit_depth %d\n", write_idf, prec);
425 writer = fopen(write_idf, "wb");
427 if(writer == NULL) return fails;
429 info = NULL; has_alpha = 0;
431 /* Create and initialize the png_struct with the desired error handler
432 * functions. If you want to use the default stderr and longjump method,
433 * you can supply NULL for the last three parameters. We also check that
434 * the library version is compatible with the one used at compile time,
435 * in case we are using dynamically linked libraries. REQUIRED.
437 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
439 /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
441 if(png == NULL) goto fin;
443 /* Allocate/initialize the image information data. REQUIRED
445 info = png_create_info_struct(png);
447 if(info == NULL) goto fin;
449 /* Set error handling. REQUIRED if you are not supplying your own
450 * error handling functions in the png_create_write_struct() call.
452 if(setjmp(png_jmpbuf(png))) goto fin;
454 /* I/O initialization functions is REQUIRED
456 png_init_io(png, writer);
458 /* Set the image information here. Width and height are up to 2^31,
459 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
460 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
461 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
462 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
463 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
464 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
469 * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
470 * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
471 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
472 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
475 png_set_compression_level(png, Z_BEST_COMPRESSION);
477 if(prec == 16) mask = 0xffff;
479 if(prec == 8) mask = 0x00ff;
481 if(prec == 4) mask = 0x000f;
483 if(prec == 2) mask = 0x0003;
485 if(prec == 1) mask = 0x0001;
488 && image->comps[0].dx == image->comps[1].dx
489 && image->comps[1].dx == image->comps[2].dx
490 && image->comps[0].dy == image->comps[1].dy
491 && image->comps[1].dy == image->comps[2].dy
492 && image->comps[0].prec == image->comps[1].prec
493 && image->comps[1].prec == image->comps[2].prec)
497 has_alpha = (nr_comp > 3);
501 width = (int)image->comps[0].w;
502 height = (int)image->comps[0].h;
504 red = image->comps[0].data;
505 green = image->comps[1].data;
506 blue = image->comps[2].data;
508 sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec;
512 sig_bit.alpha = (png_byte)prec;
513 alpha = image->comps[3].data;
514 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
515 adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
519 sig_bit.alpha = 0; alpha = NULL;
520 color_type = PNG_COLOR_TYPE_RGB;
523 png_set_sBIT(png, info, &sig_bit);
525 png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, prec,
528 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
530 png_set_gamma(png, 2.2, 1./2.2);
531 png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL);
532 /*=============================*/
533 png_write_info(png, info);
534 /*=============================*/
537 png_set_packing(png);
540 printf("%s:%d:sgnd(%d,%d,%d) w(%d) h(%d) alpha(%d)\n",__FILE__,__LINE__,
541 image->comps[0].sgnd,
542 image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
545 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
546 adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
547 adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
549 row_buf = (unsigned char*)malloc((size_t)width * (size_t)nr_comp * 2);
551 for(y = 0; y < height; ++y)
555 for(x = 0; x < width; ++x)
559 v = *red + adjustR; ++red;
560 if(v > 65535) v = 65535; else if(v < 0) v = 0;
562 if(force16) { v = (v<<ushift) + (v>>dshift); }
564 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
566 v = *green + adjustG; ++green;
567 if(v > 65535) v = 65535; else if(v < 0) v = 0;
569 if(force16) { v = (v<<ushift) + (v>>dshift); }
571 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
573 v = *blue + adjustB; ++blue;
574 if(v > 65535) v = 65535; else if(v < 0) v = 0;
576 if(force16) { v = (v<<ushift) + (v>>dshift); }
578 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
582 v = *alpha + adjustA; ++alpha;
583 if(v > 65535) v = 65535; else if(v < 0) v = 0;
585 if(force16) { v = (v<<ushift) + (v>>dshift); }
587 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
592 v = *red + adjustR; ++red;
593 if(v > 255) v = 255; else if(v < 0) v = 0;
595 if(force8) { v = (v<<ushift) + (v>>dshift); }
597 *d++ = (unsigned char)(v & mask);
599 v = *green + adjustG; ++green;
600 if(v > 255) v = 255; else if(v < 0) v = 0;
602 if(force8) { v = (v<<ushift) + (v>>dshift); }
604 *d++ = (unsigned char)(v & mask);
606 v = *blue + adjustB; ++blue;
607 if(v > 255) v = 255; else if(v < 0) v = 0;
609 if(force8) { v = (v<<ushift) + (v>>dshift); }
611 *d++ = (unsigned char)(v & mask);
615 v = *alpha + adjustA; ++alpha;
616 if(v > 255) v = 255; else if(v < 0) v = 0;
618 if(force8) { v = (v<<ushift) + (v>>dshift); }
620 *d++ = (unsigned char)(v & mask);
624 png_write_row(png, row_buf);
631 if(nr_comp == 1 /* GRAY */
632 || ( nr_comp == 2 /* GRAY_ALPHA */
633 && image->comps[0].dx == image->comps[1].dx
634 && image->comps[0].dy == image->comps[1].dy
635 && image->comps[0].prec == image->comps[1].prec))
639 red = image->comps[0].data;
641 sig_bit.gray = (png_byte)prec;
642 sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
643 alpha = NULL; adjustA = 0;
644 color_type = PNG_COLOR_TYPE_GRAY;
648 has_alpha = 1; sig_bit.alpha = (png_byte)prec;
649 alpha = image->comps[1].data;
650 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
651 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
653 width = (int)image->comps[0].w;
654 height = (int)image->comps[0].h;
656 png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, sig_bit.gray,
659 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
661 png_set_sBIT(png, info, &sig_bit);
663 png_set_gamma(png, 2.2, 1./2.2);
664 png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL);
665 /*=============================*/
666 png_write_info(png, info);
667 /*=============================*/
668 adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
672 png_set_packing(png);
677 row_buf = (unsigned char*)
678 malloc((size_t)width * (size_t)nr_comp * sizeof(unsigned short));
680 for(y = 0; y < height; ++y)
684 for(x = 0; x < width; ++x)
686 v = *red + adjustR; ++red;
687 if(v > 65535) v = 65535; else if(v < 0) v = 0;
689 if(force16) { v = (v<<ushift) + (v>>dshift); }
691 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
696 if(v > 65535) v = 65535; else if(v < 0) v = 0;
698 if(force16) { v = (v<<ushift) + (v>>dshift); }
700 *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
703 png_write_row(png, row_buf);
710 row_buf = (unsigned char*)calloc((size_t)width, (size_t)nr_comp * 2);
712 for(y = 0; y < height; ++y)
716 for(x = 0; x < width; ++x)
718 v = *red + adjustR; ++red;
719 if(v > 255) v = 255; else if(v < 0) v = 0;
721 if(force8) { v = (v<<ushift) + (v>>dshift); }
723 *d++ = (unsigned char)(v & mask);
727 v = *alpha + adjustA; ++alpha;
728 if(v > 255) v = 255; else if(v < 0) v = 0;
730 if(force8) { v = (v<<ushift) + (v>>dshift); }
732 *d++ = (unsigned char)(v & mask);
736 png_write_row(png, row_buf);
744 fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
747 png_write_end(png, info);
755 png_destroy_write_struct(&png, &info);
759 if(fails) remove(write_idf);