1 /* example.c - an example of using libpng */ 2 3 /* This is an example of how to use libpng to read and write PNG files. 4 * The file libpng.txt is much more verbose then this. If you have not 5 * read it, do so first. This was designed to be a starting point of an 6 * implementation. This is not officially part of libpng, and therefore 7 * does not require a copyright notice. 8 * 9 * This file does not currently compile, because it is missing certain 10 * parts, like allocating memory to hold an image. You will have to 11 * supply these parts to get it to compile. For an example of a minimal 12 * working PNG reader/writer, see pngtest.c, included in this distribution. 13 */ 14 15 #include <png.h> 16 17 /* Check to see if a file is a PNG file using png_check_sig(). Returns 18 * non-zero if the image is a PNG, and 0 if it isn't a PNG. 19 * 20 * If this call is successful, and you are going to keep the file open, 21 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once 22 * you have created the png_ptr, so that libpng knows your application 23 * has read that many bytes from the start of the file. Make sure you 24 * don't call png_set_sig_bytes() with more than 8 bytes read or give it 25 * an incorrect number of bytes read, or you will either have read too 26 * many bytes (your fault), or you are telling libpng to read the wrong 27 * number of magic bytes (also your fault). 28 * 29 * Many applications already read the first 2 or 4 bytes from the start 30 * of the image to determine the file type, so it would be easiest just 31 * to pass the bytes to png_check_sig() or even skip that if you know 32 * you have a PNG file, and call png_set_sig_bytes(). 33 */ 34 #define PNG_BYTES_TO_CHECK 4 35 int check_if_png(char *file_name, FILE **fp) 36 { 37 char buf[PNG_BYTES_TO_CHECK]; 38 39 /* Open the prospective PNG file. */ 40 if ((*fp = fopen(file_name, "rb")) != NULL); 41 return 0; 42 43 /* Read in the signature bytes */ 44 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) 45 return 0; 46 47 /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */ 48 return(png_check_sig(buf, PNG_BYTES_TO_CHECK)); 49 } 50 51 /* Read a PNG file. You may want to return an error code if the read 52 * fails (depending upon the failure). There are two "prototypes" given 53 * here - one where we are given the filename, and we need to open the 54 * file, and the other where we are given an open file (possibly with 55 * some or all of the magic bytes read - see comments above). 56 */ 57 **** prototype 1 **** 58 void read_png(char *file_name) /* We need to open the file */ 59 { 60 png_structp png_ptr; 61 png_infop info_ptr; 62 unsigned int sig_read = 0; 63 png_uint_32 width, height; 64 int bit_depth, color_type, interlace_type; 65 FILE *fp; 66 67 if ((fp = fopen(file_name, "rb")) == NULL) 68 return; 69 **** prototype 2 **** 70 void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ 71 { 72 png_structp png_ptr; 73 png_infop info_ptr; 74 png_uint_32 width, height; 75 int bit_depth, color_type, interlace_type; 76 **** only use one prototype! **** 77 78 /* Create and initialize the png_struct with the desired error handler 79 * functions. If you want to use the default stderr and longjump method, 80 * you can supply NULL for the last three parameters. We also supply the 81 * the compiler header file version, so that we know if the application 82 * was compiled with a compatible version of the library. REQUIRED 83 */ 84 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 85 (void *)user_error_ptr, user_error_fn, user_warning_fn); 86 87 if (png_ptr == NULL) 88 { 89 fclose(fp); 90 return; 91 } 92 93 /* Allocate/initialize the memory for image information. REQUIRED. */ 94 info_ptr = png_create_info_struct(); 95 if (info_ptr == NULL) 96 { 97 fclose(fp); 98 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); 99 return; 100 } 101 102 /* Set error handling if you are using the setjmp/longjmp method (this is 103 * the normal method of doing things with libpng). REQUIRED unless you 104 * set up your own error handlers in the png_create_read_struct() earlier. 105 */ 106 if (setjmp(png_ptr->jmpbuf)) 107 { 108 /* Free all of the memory associated with the png_ptr and info_ptr */ 109 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); 110 fclose(fp); 111 /* If we get here, we had a problem reading the file */ 112 return; 113 } 114 115 /* One of the following I/O initialization methods is REQUIRED */ 116 **** PNG file I/O method 1 **** 117 /* Set up the input control if you are using standard C streams */ 118 png_init_io(png_ptr, fp); 119 120 **** PNG file I/O method 2 **** 121 /* If you are using replacement read functions, instead of calling 122 * png_init_io() here you would call: 123 */ 124 png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn); 125 /* where user_io_ptr is a structure you want available to the callbacks */ 126 **** Use only one I/O method! **** 127 128 /* If we have already read some of the signature */ 129 png_set_sig_bytes_read(png_ptr, sig_read); 130 131 /* The call to png_read_info() gives us all of the information from the 132 * PNG file before the first IDAT (image data chunk). REQUIRED 133 */ 134 png_read_info(png_ptr, info_ptr); 135 136 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 137 &interlace_type, NULL, NULL); 138 139 /**** Set up the data transformations you want. Note that these are all 140 **** optional. Only call them if you want/need them. Many of the 141 **** transformations only work on specific types of images, and many 142 **** are mutually exclusive. 143 ****/ 144 145 /* tell libpng to strip 16 bit/color files down to 8 bits/color */ 146 png_set_strip_16(png_ptr); 147 148 /* Strip alpha bytes from the input data without combining with th 149 * background (not recommended). 150 */ 151 png_set_strip_alpha(png_ptr); 152 153 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single 154 * byte into separate bytes (useful for paletted and grayscale images). 155 */ 156 png_set_packing(png_ptr); 157 158 /* Change the order of packed pixels to least significant bit first 159 * (not useful if you are using png_set_packing). */ 160 png_set_packswap(png_ptr); 161 162 /* Expand paletted colors into true RGB triplets */ 163 if (color_type == PNG_COLOR_TYPE_PALETTE) 164 png_set_expand(png_ptr); 165 166 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */ 167 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) 168 png_set_expand(png_ptr); 169 170 /* Expand paletted or RGB images with transparency to full alpha channels 171 * so the data will be available as RGBA quartets. 172 */ 173 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) 174 png_set_expand(png_ptr); 175 176 /* Set the background color to draw transparent and alpha images over. 177 * It is possible to set the red, green, and blue components directly 178 * for paletted images instead of supplying a palette index. Note that 179 * even if the PNG file supplies a background, you are not required to 180 * use it - you should use the (solid) application background if it has one. 181 */ 182 183 png_color_16 my_background, *image_background); 184 185 if (png_get_bKGD(png_ptr, info_ptr, &image_background); 186 png_set_background(png_ptr, image_background), 187 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); 188 else 189 png_set_background(png_ptr, &my_background, 190 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); 191 192 /* Some suggestions as to how to get a screen gamma value */ 193 194 /* Note that screen gamma is (display_gamma/viewing_gamma) 195 if (/* We have a user-defined screen gamma value */) 196 { 197 screen_gamma = user-defined screen_gamma; 198 } 199 /* This is one way that applications share the same screen gamma value */ 200 else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL) 201 { 202 screen_gamma = atof(gamma_str); 203 } 204 /* If we don't have another value */ 205 else 206 { 207 screen_gamma = 2.2; /* A good guess for a PC monitors in a dimly 208 lit room */ 209 screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */ 210 } 211 212 /* Tell libpng to handle the gamma conversion for you. The second call 213 * is a good guess for PC generated images, but it should be configurable 214 * by the user at run time by the user. It is strongly suggested that 215 * your application support gamma correction. 216 */ 217 218 int intent; 219 220 if (png_get_sRGB(png_ptr, info_ptr, &intent) 221 png_set_sRGB(png_ptr, intent, 0); 222 else 223 if (png_get_gAMA(png_ptr, info_ptr, &image_gamma) 224 png_set_gamma(png_ptr, screen_gamma, image_gamma); 225 else 226 png_set_gamma(png_ptr, screen_gamma, 0.50); 227 228 /* Dither RGB files down to 8 bit palette or reduce palettes 229 * to the number of colors available on your screen. 230 */ 231 if (color_type & PNG_COLOR_MASK_COLOR) 232 { 233 png_uint_32 num_palette; 234 png_colorp palette; 235 236 /* This reduces the image to the application supplied palette */ 237 if (we have our own palette) 238 { 239 /* An array of colors to which the image should be dithered */ 240 png_color std_color_cube[MAX_SCREEN_COLORS]; 241 242 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS, 243 MAX_SCREEN_COLORS, NULL, 0); 244 } 245 /* This reduces the image to the palette supplied in the file */ 246 else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette))) 247 { 248 png_color16p histogram; 249 250 png_get_hIST(png_ptr, info_ptr, &histogram); 251 252 png_set_dither(png_ptr, palette, num_palette, 253 max_screen_colors, histogram, 0); 254 } 255 } 256 257 /* invert monocrome files to have 0 as white and 1 as black */ 258 png_set_invert_mono(png_ptr); 259 260 /* If you want to shift the pixel values from the range [0,255] or 261 * [0,65535] to the original [0,7] or [0,31], or whatever range the 262 * colors were originally in: 263 */ 264 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) 265 { 266 png_color8p sig_bit; 267 268 png_get_sBIT(png_ptr, info_ptr, &sig_bit); 269 png_set_shift(png_ptr, sig_bit); 270 } 271 272 /* flip the RGB pixels to BGR (or RGBA to BGRA) */ 273 png_set_bgr(png_ptr); 274 275 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ 276 png_set_swap_alpha(png_ptr); 277 278 /* swap bytes of 16 bit files to least significant byte first */ 279 png_set_swap(png_ptr); 280 281 /* Add filler (or alpha) byte (before/after each RGB triplet) */ 282 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); 283 284 /* Turn on interlace handling. REQUIRED if you are not using 285 * png_read_image(). To see how to handle interlacing passes, 286 * see the png_read_row() method below: 287 */ 288 number_passes = png_set_interlace_handling(png_ptr); 289 290 /* Optional call to gamma correct and add the background to the palette 291 * and update info structure. REQUIRED if you are expecting libpng to 292 * update the palette for you (ie you selected such a transform above). 293 */ 294 png_read_update_info(png_ptr, info_ptr); 295 296 /* Allocate the memory to hold the image using the fields of info_ptr. */ 297 298 /* The easiest way to read the image: */ 299 png_bytep row_pointers[height]; 300 301 for (row = 0; row < height; row++) 302 { 303 row_pointers[row] = malloc(png_get_rowbytes(png_ptr, info_ptr)); 304 } 305 306 /* Now it's time to read the image. One of these methods is REQUIRED */ 307 **** Read the entire image in one go **** 308 png_read_image(png_ptr, row_pointers); 309 310 **** Read the image one or more scanlines at a time **** 311 /* The other way to read images - deal with interlacing: */ 312 313 for (pass = 0; pass < number_passes; pass++) 314 { 315 [[[[[[[ Read the image a single row at a time ]]]]]]] 316 for (y = 0; y < height; y++) 317 { 318 png_bytep row_pointers = row[y]; 319 png_read_rows(png_ptr, &row_pointers, NULL, 1); 320 } 321 322 [[[[[[[ Read the image several rows at a time ]]]]]]] 323 for (y = 0; y < height; y += number_of_rows) 324 { 325 <<<<<<<<<< Read the image using the "sparkle" effect. >>>>>>>>>> 326 png_read_rows(png_ptr, row_pointers, NULL, number_of_rows); 327 328 <<<<<<<<<< Read the image using the "rectangle" effect >>>>>>>>>> 329 png_read_rows(png_ptr, NULL, row_pointers, number_of_rows); 330 <<<<<<<<<< use only one of these two methods >>>>>>>>>> 331 } 332 333 /* if you want to display the image after every pass, do 334 so here */ 335 [[[[[[[ use only one of these two methods ]]]]]]] 336 } 337 **** use only one of these two methods **** 338 339 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */ 340 png_read_end(png_ptr, info_ptr); 341 342 /* clean up after the read, and free any memory allocated - REQUIRED */ 343 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); 344 345 /* close the file */ 346 fclose(fp); 347 348 /* that's it */ 349 return; 350 } 351 352 /* progressively read a file */ 353 354 int 355 initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr) 356 { 357 /* Create and initialize the png_struct with the desired error handler 358 * functions. If you want to use the default stderr and longjump method, 359 * you can supply NULL for the last three parameters. We also check that 360 * the library version is compatible in case we are using dynamically 361 * linked libraries. 362 */ 363 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 364 (void *)user_error_ptr, user_error_fn, user_warning_fn); 365 366 if (*png_ptr == NULL) 367 { 368 *info_ptr = NULL; 369 return ERROR; 370 } 371 372 *info_ptr = png_create_info_struct(png_ptr); 373 374 if (*info_ptr == NULL) 375 { 376 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL); 377 return ERROR; 378 } 379 380 if (setjmp((*png_ptr)->jmpbuf)) 381 { 382 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL); 383 return ERROR; 384 } 385 386 /* this one's new. You will need to provide all three 387 * function callbacks, even if you aren't using them all. 388 * These functions shouldn't be dependent on global or 389 * static variables if you are decoding several images 390 * simultaneously. You should store stream specific data 391 * in a separate struct, given as the second parameter, 392 * and retrieve the pointer from inside the callbacks using 393 * the function png_get_progressive_ptr(png_ptr). 394 */ 395 png_set_progressive_read_fn(*png_ptr, (void *)stream_data, 396 info_callback, row_callback, end_callback); 397 398 return OK; 399 } 400 401 int 402 process_data(png_structp *png_ptr, png_infop *info_ptr, 403 png_bytep buffer, png_uint_32 length) 404 { 405 if (setjmp((*png_ptr)->jmpbuf)) 406 { 407 /* Free the png_ptr and info_ptr memory on error */ 408 png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL); 409 return ERROR; 410 } 411 412 /* This one's new also. Simply give it chunks of data as 413 * they arrive from the data stream (in order, of course). 414 * On Segmented machines, don't give it any more than 64K. 415 * The library seems to run fine with sizes of 4K, although 416 * you can give it much less if necessary (I assume you can 417 * give it chunks of 1 byte, but I haven't tried with less 418 * than 256 bytes yet). When this function returns, you may 419 * want to display any rows that were generated in the row 420 * callback, if you aren't already displaying them there. 421 */ 422 png_process_data(*png_ptr, *info_ptr, buffer, length); 423 return OK; 424 } 425 426 info_callback(png_structp png_ptr, png_infop info) 427 { 428 /* do any setup here, including setting any of the transformations 429 * mentioned in the Reading PNG files section. For now, you _must_ 430 * call either png_start_read_image() or png_read_update_info() 431 * after all the transformations are set (even if you don't set 432 * any). You may start getting rows before png_process_data() 433 * returns, so this is your last chance to prepare for that. 434 */ 435 } 436 437 row_callback(png_structp png_ptr, png_bytep new_row, 438 png_uint_32 row_num, int pass) 439 { 440 /* this function is called for every row in the image. If the 441 * image is interlacing, and you turned on the interlace handler, 442 * this function will be called for every row in every pass. 443 * Some of these rows will not be changed from the previous pass. 444 * When the row is not changed, the new_row variable will be NULL. 445 * The rows and passes are called in order, so you don't really 446 * need the row_num and pass, but I'm supplying them because it 447 * may make your life easier. 448 * 449 * For the non-NULL rows of interlaced images, you must call 450 * png_progressive_combine_row() passing in the row and the 451 * old row. You can call this function for NULL rows (it will 452 * just return) and for non-interlaced images (it just does the 453 * memcpy for you) if it will make the code easier. Thus, you 454 * can just do this for all cases: 455 */ 456 457 png_progressive_combine_row(png_ptr, old_row, new_row); 458 459 /* where old_row is what was displayed for previous rows. Note 460 * that the first pass (pass == 0 really) will completely cover 461 * the old row, so the rows do not have to be initialized. After 462 * the first pass (and only for interlaced images), you will have 463 * to pass the current row, and the function will combine the 464 * old row and the new row. 465 */ 466 } 467 468 end_callback(png_structp png_ptr, png_infop info) 469 { 470 /* this function is called when the whole image has been read, 471 * including any chunks after the image (up to and including 472 * the IEND). You will usually have the same info chunk as you 473 * had in the header, although some data may have been added 474 * to the comments and time fields. 475 * 476 * Most people won't do much here, perhaps setting a flag that 477 * marks the image as finished. 478 */ 479 } 480 481 /* write a png file */ 482 void write_png(char *file_name, ... other image information ...) 483 { 484 FILE *fp; 485 png_structp png_ptr; 486 png_infop info_ptr; 487 488 /* open the file */ 489 fp = fopen(file_name, "wb"); 490 if (fp == NULL) 491 return; 492 493 /* Create and initialize the png_struct with the desired error handler 494 * functions. If you want to use the default stderr and longjump method, 495 * you can supply NULL for the last three parameters. We also check that 496 * the library version is compatible with the one used at compile time, 497 * in case we are using dynamically linked libraries. REQUIRED. 498 */ 499 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 500 (void *)user_error_ptr, user_error_fn, user_warning_fn); 501 502 if (png_ptr == NULL) 503 { 504 fclose(fp); 505 return; 506 } 507 508 /* Allocate/initialize the image information data. REQUIRED */ 509 info_ptr = png_create_info_struct(png_ptr); 510 if (info_ptr == NULL) 511 { 512 fclose(fp); 513 png_destroy_write_struct(&png_ptr, (png_infopp)NULL); 514 return; 515 } 516 517 /* Set error handling. REQUIRED if you aren't supplying your own 518 * error hadnling functions in the png_create_write_struct() call. 519 */ 520 if (setjmp(png_ptr->jmpbuf)) 521 { 522 /* If we get here, we had a problem reading the file */ 523 fclose(fp); 524 png_destroy_write_struct(&png_ptr, (png_infopp)NULL); 525 return; 526 } 527 528 /* One of the following I/O initialization functions is REQUIRED */ 529 **** I/O initialization method 1 **** 530 /* set up the output control if you are using standard C streams */ 531 png_init_io(png_ptr, fp); 532 **** I/O initialization method 2 **** 533 /* If you are using replacement read functions, instead of calling 534 * png_init_io() here you would call */ 535 png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn, 536 user_IO_flush_function); 537 /* where user_io_ptr is a structure you want available to the callbacks */ 538 **** only use 1 initialization method **** 539 540 /* Set the image information here. Width and height are up to 2^31, 541 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on 542 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, 543 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, 544 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or 545 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST 546 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED 547 */ 548 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???, 549 PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 550 551 /* set the palette if there is one. REQUIRED for indexed-color images */ 552 palette = (png_colorp)png_malloc(png_ptr, 256 * sizeof (png_color)); 553 ... set palette colors ... 554 png_set_PLTE(png_ptr, info_ptr, palette, 256); 555 556 /* optional significant bit chunk */ 557 /* if we are dealing with a grayscale image then */ 558 sig_bit.gray = true_bit_depth; 559 /* otherwise, if we are dealing with a color image then */ 560 sig_bit.red = true_red_bit_depth; 561 sig_bit.green = true_green_bit_depth; 562 sig_bit.blue = true_blue_bit_depth; 563 /* if the image has an alpha channel then */ 564 sig_bit.alpha = true_alpha_bit_depth; 565 png_set_sBIT(png_ptr, info_ptr, sig_bit); 566 567 568 /* Optional gamma chunk is strongly suggested if you have any guess 569 * as to the correct gamma of the image. 570 */ 571 png_set_gAMA(png_ptr, info_ptr, gamma); 572 573 /* Optionally write comments into the image */ 574 text_ptr[0].key = "Title"; 575 text_ptr[0].text = "Mona Lisa"; 576 text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; 577 text_ptr[1].key = "Author"; 578 text_ptr[1].text = "Leonardo DaVinci"; 579 text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE; 580 text_ptr[2].key = "Description"; 581 text_ptr[2].text = "<long text>"; 582 text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; 583 png_set_text(png_ptr, info_ptr, text_ptr, 2); 584 585 /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */ 586 /* note that if sRGB is present the cHRM chunk must be ignored 587 * on read and must be written in accordance with the sRGB profile */ 588 589 /* Write the file header information. REQUIRED */ 590 png_write_info(png_ptr, info_ptr); 591 592 /* Once we write out the header, the compression type on the text 593 * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or 594 * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again 595 * at the end. 596 */ 597 598 /* set up the transformations you want. Note that these are 599 * all optional. Only call them if you want them. 600 */ 601 602 /* invert monocrome pixels */ 603 png_set_invert_mono(png_ptr); 604 605 /* Shift the pixels up to a legal bit depth and fill in 606 * as appropriate to correctly scale the image. 607 */ 608 png_set_shift(png_ptr, &sig_bit); 609 610 /* pack pixels into bytes */ 611 png_set_packing(png_ptr); 612 613 /* swap location of alpha bytes from ARGB to RGBA */ 614 png_set_swap_alpha(png_ptr); 615 616 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into 617 * RGB (4 channels -> 3 channels). The second parameter is not used. 618 */ 619 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); 620 621 /* flip BGR pixels to RGB */ 622 png_set_bgr(png_ptr); 623 624 /* swap bytes of 16-bit files to most significant byte first */ 625 png_set_swap(png_ptr); 626 627 /* swap bits of 1, 2, 4 bit packed pixel formats */ 628 png_set_packswap(png_ptr); 629 630 /* turn on interlace handling if you are not using png_write_image() */ 631 if (interlacing) 632 number_passes = png_set_interlace_handling(png_ptr); 633 else 634 number_passes = 1; 635 636 /* The easiest way to write the image (you may have a different memory 637 * layout, however, so choose what fits your needs best). You need to 638 * use the first method if you aren't handling interlacing yourself. 639 */ 640 png_byte row_pointers[height][width]; 641 642 /* One of the following output methods is REQUIRED */ 643 **** write out the entire image data in one call *** 644 png_write_image(png_ptr, row_pointers); 645 646 /* the other way to write the image - deal with interlacing */ 647 648 **** write out the image data by one or more scanlines **** 649 /* The number of passes is either 1 for non-interlaced images, 650 * or 7 for interlaced images. 651 */ 652 for (pass = 0; pass < number_passes; pass++) 653 { 654 /* Write a few rows at a time. */ 655 png_write_rows(png_ptr, row_pointers, number_of_rows); 656 657 /* If you are only writing one row at a time, this works */ 658 for (y = 0; y < height; y++) 659 { 660 png_bytep row_pointers = row[y]; 661 png_write_rows(png_ptr, &row_pointers, 1); 662 } 663 } 664 **** use only one output method **** 665 666 /* You can write optional chunks like tEXt, zTXt, and tIME at the end 667 * as well. 668 */ 669 670 /* It is REQUIRED to call this to finish writing the rest of the file */ 671 png_write_end(png_ptr, info_ptr); 672 673 /* if you malloced the palette, free it here */ 674 free(info_ptr->palette); 675 676 /* if you allocated any text comments, free them here */ 677 678 /* clean up after the write, and free any memory allocated */ 679 png_destroy_write_struct(&png_ptr, (png_infopp)NULL); 680 681 /* close the file */ 682 fclose(fp); 683 684 /* that's it */ 685 return; 686 } 687 688