Changeset 561 for trunk/src/3rdparty/libpng/example.c
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/3rdparty/libpng/example.c
r2 r561 3 3 4 4 /* example.c - an example of using libpng 5 * Last changed in libpng 1.2. 1 December 7, 2001.5 * Last changed in libpng 1.2.37 [June 4, 2009] 6 6 * This file has been placed in the public domain by the authors. 7 * Maintained 1998-200 7Glenn Randers-Pehrson7 * Maintained 1998-2009 Glenn Randers-Pehrson 8 8 * Maintained 1996, 1997 Andreas Dilger) 9 9 * Written 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) … … 92 92 if ((fp = fopen(file_name, "rb")) == NULL) 93 93 return (ERROR); 94 94 95 #else no_open_file /* prototype 2 */ 95 void read_png(FILE *fp, unsigned int sig_read) /* file is already open */96 void read_png(FILE *fp, unsigned int sig_read) /* File is already open */ 96 97 { 97 98 png_structp png_ptr; … … 99 100 png_uint_32 width, height; 100 101 int bit_depth, color_type, interlace_type; 101 #endif no_open_file /* only use one prototype! */102 #endif no_open_file /* Only use one prototype! */ 102 103 103 104 /* Create and initialize the png_struct with the desired error handler … … 165 166 */ 166 167 png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL); 168 167 169 #else 168 170 /* OK, you're doing it the hard way, with the lower-level functions */ … … 176 178 &interlace_type, int_p_NULL, int_p_NULL); 177 179 178 /* Set up the data transformations you want. Note that these are all179 * optional. Only call them if you want/need them. Many of the180 * transformations only work on specific types of images, and many181 * are mutually exclusive.182 */183 184 /* tell libpng to strip 16 bit/color files down to 8 bits/color */180 /* Set up the data transformations you want. Note that these are all 181 * optional. Only call them if you want/need them. Many of the 182 * transformations only work on specific types of images, and many 183 * are mutually exclusive. 184 */ 185 186 /* Tell libpng to strip 16 bit/color files down to 8 bits/color */ 185 187 png_set_strip_16(png_ptr); 186 188 … … 205 207 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */ 206 208 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) 207 png_set_ gray_1_2_4_to_8(png_ptr);209 png_set_expand_gray_1_2_4_to_8(png_ptr); 208 210 209 211 /* Expand paletted or RGB images with transparency to full alpha channels … … 229 231 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); 230 232 231 /* Some suggestions as to how to get a screen gamma value */ 232 233 /* Note that screen gamma is the display_exponent, which includes 234 * the CRT_exponent and any correction for viewing conditions */ 233 /* Some suggestions as to how to get a screen gamma value 234 * 235 * Note that screen gamma is the display_exponent, which includes 236 * the CRT_exponent and any correction for viewing conditions 237 */ 235 238 if (/* We have a user-defined screen gamma value */) 236 239 { … … 245 248 else 246 249 { 247 screen_gamma = 2.2; /* A good guess for a PC monitor sin a dimly250 screen_gamma = 2.2; /* A good guess for a PC monitor in a dimly 248 251 lit room */ 249 252 screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */ … … 278 281 279 282 /* This reduces the image to the application supplied palette */ 280 if (/* we have our own palette */)283 if (/* We have our own palette */) 281 284 { 282 285 /* An array of colors to which the image should be dithered */ … … 298 301 } 299 302 300 /* invert monochrome files to have 0 as white and 1 as black */303 /* Invert monochrome files to have 0 as white and 1 as black */ 301 304 png_set_invert_mono(png_ptr); 302 305 … … 307 310 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) 308 311 { 309 png_color_8p sig_bit ;310 311 png_get_sBIT(png_ptr, info_ptr, &sig_bit );312 png_set_shift(png_ptr, sig_bit );313 } 314 315 /* flip the RGB pixels to BGR (or RGBA to BGRA) */312 png_color_8p sig_bit_p; 313 314 png_get_sBIT(png_ptr, info_ptr, &sig_bit_p); 315 png_set_shift(png_ptr, sig_bit_p); 316 } 317 318 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ 316 319 if (color_type & PNG_COLOR_MASK_COLOR) 317 320 png_set_bgr(png_ptr); 318 321 319 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */322 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ 320 323 png_set_swap_alpha(png_ptr); 321 324 322 /* swap bytes of 16 bit files to least significant byte first */325 /* Swap bytes of 16 bit files to least significant byte first */ 323 326 png_set_swap(png_ptr); 324 327 … … 343 346 png_bytep row_pointers[height]; 344 347 348 /* Clear the pointer array */ 345 349 for (row = 0; row < height; row++) 346 { 350 row_pointers[row] = NULL; 351 352 for (row = 0; row < height; row++) 347 353 row_pointers[row] = png_malloc(png_ptr, png_get_rowbytes(png_ptr, 348 354 info_ptr)); 349 }350 355 351 356 /* Now it's time to read the image. One of these methods is REQUIRED */ … … 373 378 png_read_rows(png_ptr, png_bytepp_NULL, &row_pointers[y], 374 379 number_of_rows); 375 #endif no_sparkle /* use only one of these two methods */380 #endif no_sparkle /* Use only one of these two methods */ 376 381 } 377 382 378 /* if you want to display the image after every pass, do 379 so here */ 380 #endif no_single /* use only one of these two methods */ 381 } 382 #endif no_entire /* use only one of these two methods */ 383 384 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */ 383 /* If you want to display the image after every pass, do so here */ 384 #endif no_single /* Use only one of these two methods */ 385 } 386 #endif no_entire /* Use only one of these two methods */ 387 388 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ 385 389 png_read_end(png_ptr, info_ptr); 386 390 #endif hilevel … … 388 392 /* At this point you have read the entire image */ 389 393 390 /* clean up after the read, and free any memory allocated - REQUIRED */394 /* Clean up after the read, and free any memory allocated - REQUIRED */ 391 395 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); 392 396 393 /* close the file */397 /* Close the file */ 394 398 fclose(fp); 395 399 396 /* that's it */400 /* That's it */ 397 401 return (OK); 398 402 } 399 403 400 /* progressively read a file */404 /* Progressively read a file */ 401 405 402 406 int … … 463 467 /* This one's new also. Simply give it chunks of data as 464 468 * they arrive from the data stream (in order, of course). 465 * On Segmented machines, don't give it any more than 64K.469 * On segmented machines, don't give it any more than 64K. 466 470 * The library seems to run fine with sizes of 4K, although 467 471 * you can give it much less if necessary (I assume you can … … 477 481 info_callback(png_structp png_ptr, png_infop info) 478 482 { 479 /* do any setup here, including setting any of the transformations480 * mentioned in the Reading PNG files section. For now, you _must_481 * call either png_start_read_image() or png_read_update_info()482 * after all the transformations are set (even if you don't set483 * any). You may start getting rows before png_process_data()484 * returns, so this is your last chance to prepare for that.485 */483 /* Do any setup here, including setting any of the transformations 484 * mentioned in the Reading PNG files section. For now, you _must_ 485 * call either png_start_read_image() or png_read_update_info() 486 * after all the transformations are set (even if you don't set 487 * any). You may start getting rows before png_process_data() 488 * returns, so this is your last chance to prepare for that. 489 */ 486 490 } 487 491 … … 489 493 png_uint_32 row_num, int pass) 490 494 { 491 /* 492 * This function is called for every row in the image. If the 493 * image is interlaced, and you turned on the interlace handler, 494 * this function will be called for every row in every pass. 495 * 496 * In this function you will receive a pointer to new row data from 497 * libpng called new_row that is to replace a corresponding row (of 498 * the same data format) in a buffer allocated by your application. 499 * 500 * The new row data pointer new_row may be NULL, indicating there is 501 * no new data to be replaced (in cases of interlace loading). 502 * 503 * If new_row is not NULL then you need to call 504 * png_progressive_combine_row() to replace the corresponding row as 505 * shown below: 506 */ 495 /* 496 * This function is called for every row in the image. If the 497 * image is interlaced, and you turned on the interlace handler, 498 * this function will be called for every row in every pass. 499 * 500 * In this function you will receive a pointer to new row data from 501 * libpng called new_row that is to replace a corresponding row (of 502 * the same data format) in a buffer allocated by your application. 503 * 504 * The new row data pointer "new_row" may be NULL, indicating there is 505 * no new data to be replaced (in cases of interlace loading). 506 * 507 * If new_row is not NULL then you need to call 508 * png_progressive_combine_row() to replace the corresponding row as 509 * shown below: 510 */ 511 507 512 /* Check if row_num is in bounds. */ 508 if ((row_num >= 0) && (row_num < height))513 if ((row_num >= 0) && (row_num < height)) 509 514 { 510 515 /* Get pointer to corresponding row in our … … 516 521 * data to the corresponding row data. 517 522 */ 518 if ((old_row != NULL) && (new_row != NULL))523 if ((old_row != NULL) && (new_row != NULL)) 519 524 png_progressive_combine_row(png_ptr, old_row, new_row); 520 525 } 521 /*522 * The rows and passes are called in order, so you don't really523 * need the row_num and pass, but I'm supplying them because it524 * may make your life easier.525 *526 * For the non-NULL rows of interlaced images, you must call527 * png_progressive_combine_row() passing in the new row and the528 * old row, as demonstrated above. You can call this function for529 * NULL rows (it will just return) and for non-interlaced images530 * (it just does the png_memcpy for you) if it will make the code531 * easier. Thus, you can just do this for all cases:532 */526 /* 527 * The rows and passes are called in order, so you don't really 528 * need the row_num and pass, but I'm supplying them because it 529 * may make your life easier. 530 * 531 * For the non-NULL rows of interlaced images, you must call 532 * png_progressive_combine_row() passing in the new row and the 533 * old row, as demonstrated above. You can call this function for 534 * NULL rows (it will just return) and for non-interlaced images 535 * (it just does the png_memcpy for you) if it will make the code 536 * easier. Thus, you can just do this for all cases: 537 */ 533 538 534 539 png_progressive_combine_row(png_ptr, old_row, new_row); 535 540 536 /* where old_row is what was displayed for previous rows. Note537 * that the first pass (pass == 0 really) will completely cover538 * the old row, so the rows do not have to be initialized. After539 * the first pass (and only for interlaced images), you will have540 * to pass the current row as new_row, and the function will combine541 * the old row and the new row.542 */541 /* where old_row is what was displayed for previous rows. Note 542 * that the first pass (pass == 0 really) will completely cover 543 * the old row, so the rows do not have to be initialized. After 544 * the first pass (and only for interlaced images), you will have 545 * to pass the current row as new_row, and the function will combine 546 * the old row and the new row. 547 */ 543 548 } 544 549 545 550 end_callback(png_structp png_ptr, png_infop info) 546 551 { 547 /* this function is called when the whole image has been read,548 * including any chunks after the image (up to and including549 * the IEND). You will usually have the same info chunk as you550 * had in the header, although some data may have been added551 * to the comments and time fields.552 *553 * Most people won't do much here, perhaps setting a flag that554 * marks the image as finished.555 */552 /* This function is called when the whole image has been read, 553 * including any chunks after the image (up to and including 554 * the IEND). You will usually have the same info chunk as you 555 * had in the header, although some data may have been added 556 * to the comments and time fields. 557 * 558 * Most people won't do much here, perhaps setting a flag that 559 * marks the image as finished. 560 */ 556 561 } 557 562 558 /* write a png file */563 /* Write a png file */ 559 564 void write_png(char *file_name /* , ... other image information ... */) 560 565 { … … 564 569 png_colorp palette; 565 570 566 /* open the file */571 /* Open the file */ 567 572 fp = fopen(file_name, "wb"); 568 573 if (fp == NULL) … … 598 603 if (setjmp(png_jmpbuf(png_ptr))) 599 604 { 600 /* If we get here, we had a problem reading the file */605 /* If we get here, we had a problem writing the file */ 601 606 fclose(fp); 602 607 png_destroy_write_struct(&png_ptr, &info_ptr); … … 605 610 606 611 /* One of the following I/O initialization functions is REQUIRED */ 612 607 613 #ifdef streams /* I/O initialization method 1 */ 608 /* set up the output control if you are using standard C streams */614 /* Set up the output control if you are using standard C streams */ 609 615 png_init_io(png_ptr, fp); 616 610 617 #else no_streams /* I/O initialization method 2 */ 611 /* If you are using replacement read functions, instead of calling 612 * png_init_io() here you would call */ 618 /* If you are using replacement write functions, instead of calling 619 * png_init_io() here you would call 620 */ 613 621 png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn, 614 622 user_IO_flush_function); 615 623 /* where user_io_ptr is a structure you want available to the callbacks */ 616 #endif no_streams /* only use one initialization method */624 #endif no_streams /* Only use one initialization method */ 617 625 618 626 #ifdef hilevel 619 627 /* This is the easy way. Use it if you already have all the 620 * image info living in fo inthe structure. You could "|" many628 * image info living in the structure. You could "|" many 621 629 * PNG_TRANSFORM flags into the png_transforms integer here. 622 630 */ 623 631 png_write_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL); 632 624 633 #else 625 634 /* This is the hard way */ … … 636 645 PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 637 646 638 /* set the palette if there is one. REQUIRED for indexed-color images */647 /* Set the palette if there is one. REQUIRED for indexed-color images */ 639 648 palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH 640 * png_sizeof 641 /* ... set palette colors ... */649 * png_sizeof(png_color)); 650 /* ... Set palette colors ... */ 642 651 png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH); 643 652 /* You must not free palette here, because png_set_PLTE only makes a link to 644 the palette that you malloced. Wait until you are about to destroy 645 the png structure. */ 646 647 /* optional significant bit chunk */ 648 /* if we are dealing with a grayscale image then */ 653 * the palette that you malloced. Wait until you are about to destroy 654 * the png structure. 655 */ 656 657 /* Optional significant bit (sBIT) chunk */ 658 png_color_8 sig_bit; 659 /* If we are dealing with a grayscale image then */ 649 660 sig_bit.gray = true_bit_depth; 650 /* otherwise, if we are dealing with a color image then */661 /* Otherwise, if we are dealing with a color image then */ 651 662 sig_bit.red = true_red_bit_depth; 652 663 sig_bit.green = true_green_bit_depth; 653 664 sig_bit.blue = true_blue_bit_depth; 654 /* if the image has an alpha channel then */665 /* If the image has an alpha channel then */ 655 666 sig_bit.alpha = true_alpha_bit_depth; 656 png_set_sBIT(png_ptr, info_ptr, sig_bit);667 png_set_sBIT(png_ptr, info_ptr, &sig_bit); 657 668 658 669 … … 679 690 png_set_text(png_ptr, info_ptr, text_ptr, 3); 680 691 681 /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */ 682 /* note that if sRGB is present the gAMA and cHRM chunks must be ignored 683 * on read and must be written in accordance with the sRGB profile */ 692 /* Other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs */ 693 694 /* Note that if sRGB is present the gAMA and cHRM chunks must be ignored 695 * on read and, if your application chooses to write them, they must 696 * be written in accordance with the sRGB profile 697 */ 684 698 685 699 /* Write the file header information. REQUIRED */ … … 693 707 * png_write_info(png_ptr, info_ptr); 694 708 * 695 * However, given the level of known- and unknown-chunk support in 1. 1.0709 * However, given the level of known- and unknown-chunk support in 1.2.0 696 710 * and up, this should no longer be necessary. 697 711 */ … … 703 717 */ 704 718 705 /* set up the transformations you want. Note that these are719 /* Set up the transformations you want. Note that these are 706 720 * all optional. Only call them if you want them. 707 721 */ 708 722 709 /* invert monochrome pixels */723 /* Invert monochrome pixels */ 710 724 png_set_invert_mono(png_ptr); 711 725 … … 715 729 png_set_shift(png_ptr, &sig_bit); 716 730 717 /* pack pixels into bytes */731 /* Pack pixels into bytes */ 718 732 png_set_packing(png_ptr); 719 733 720 /* swap location of alpha bytes from ARGB to RGBA */734 /* Swap location of alpha bytes from ARGB to RGBA */ 721 735 png_set_swap_alpha(png_ptr); 722 736 … … 726 740 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); 727 741 728 /* flip BGR pixels to RGB */742 /* Flip BGR pixels to RGB */ 729 743 png_set_bgr(png_ptr); 730 744 731 /* swap bytes of 16-bit files to most significant byte first */745 /* Swap bytes of 16-bit files to most significant byte first */ 732 746 png_set_swap(png_ptr); 733 747 734 /* swap bits of 1, 2, 4 bit packed pixel formats */748 /* Swap bits of 1, 2, 4 bit packed pixel formats */ 735 749 png_set_packswap(png_ptr); 736 750 737 /* turn on interlace handling if you are not using png_write_image() */751 /* Turn on interlace handling if you are not using png_write_image() */ 738 752 if (interlacing) 739 753 number_passes = png_set_interlace_handling(png_ptr); … … 756 770 757 771 /* One of the following output methods is REQUIRED */ 758 #ifdef entire /* write out the entire image data in one call */ 772 773 #ifdef entire /* Write out the entire image data in one call */ 759 774 png_write_image(png_ptr, row_pointers); 760 775 761 /* the other way to write the image - deal with interlacing */ 762 763 #else no_entire /* write out the image data by one or more scanlines */ 776 /* The other way to write the image - deal with interlacing */ 777 778 #else no_entire /* Write out the image data by one or more scanlines */ 779 764 780 /* The number of passes is either 1 for non-interlaced images, 765 781 * or 7 for interlaced images. … … 772 788 /* If you are only writing one row at a time, this works */ 773 789 for (y = 0; y < height; y++) 774 {775 790 png_write_rows(png_ptr, &row_pointers[y], 1); 776 } 777 } 778 #endif no_entire /* use only one output method */ 791 } 792 #endif no_entire /* Use only one output method */ 779 793 780 794 /* You can write optional chunks like tEXt, zTXt, and tIME at the end 781 * as well. Shouldn't be necessary in 1. 1.0 and up as all the public795 * as well. Shouldn't be necessary in 1.2.0 and up as all the public 782 796 * chunks are supported and you can use png_set_unknown_chunks() to 783 797 * register unknown chunks into the info structure to be written out. … … 789 803 790 804 /* If you png_malloced a palette, free it here (don't free info_ptr->palette, 791 as recommended in versions 1.0.5m and earlier of this example; if 792 libpng mallocs info_ptr->palette, libpng will free it). If you 793 allocated it with malloc() instead of png_malloc(), use free() instead 794 of png_free(). */ 805 * as recommended in versions 1.0.5m and earlier of this example; if 806 * libpng mallocs info_ptr->palette, libpng will free it). If you 807 * allocated it with malloc() instead of png_malloc(), use free() instead 808 * of png_free(). 809 */ 795 810 png_free(png_ptr, palette); 796 palette =NULL;811 palette = NULL; 797 812 798 813 /* Similarly, if you png_malloced any data that you passed in with 799 png_set_something(), such as a hist or trans array, free it here, 800 when you can be sure that libpng is through with it. */ 814 * png_set_something(), such as a hist or trans array, free it here, 815 * when you can be sure that libpng is through with it. 816 */ 801 817 png_free(png_ptr, trans); 802 trans=NULL; 803 804 /* clean up after the write, and free any memory allocated */ 818 trans = NULL; 819 /* Whenever you use png_free() it is a good idea to set the pointer to 820 * NULL in case your application inadvertently tries to png_free() it 821 * again. When png_free() sees a NULL it returns without action, thus 822 * avoiding the double-free security problem. 823 */ 824 825 /* Clean up after the write, and free any memory allocated */ 805 826 png_destroy_write_struct(&png_ptr, &info_ptr); 806 827 807 /* close the file */828 /* Close the file */ 808 829 fclose(fp); 809 830 810 /* that's it */831 /* That's it */ 811 832 return (OK); 812 833 }
Note:
See TracChangeset
for help on using the changeset viewer.