00001 00002 #ifdef KNW_WHAT_YOU ARE DOING /* in case someone actually tries to compile this */ 00003 00004 /* example.c - an example of using libpng */ 00005 00006 /* This is an example of how to use libpng to read and write PNG files. 00007 * The file libpng.txt is much more verbose then this. If you have not 00008 * read it, do so first. This was designed to be a starting point of an 00009 * implementation. This is not officially part of libpng, is hereby placed 00010 * in the public domain, and therefore does not require a copyright notice. 00011 * 00012 * This file does not currently compile, because it is missing certain 00013 * parts, like allocating memory to hold an image. You will have to 00014 * supply these parts to get it to compile. For an example of a minimal 00015 * working PNG reader/writer, see pngtest.c, included in this distribution; 00016 * see also the programs in the contrib directory. 00017 */ 00018 00019 #include "png.h" 00020 00021 /* The png_jmpbuf() macro, used in error handling, became available in 00022 * libpng version 1.0.6. If you want to be able to run your code with older 00023 * versions of libpng, you must define the macro yourself (but only if it 00024 * is not already defined by libpng!). 00025 */ 00026 00027 #ifndef png_jmpbuf 00028 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) 00029 #endif 00030 00031 /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp() 00032 * returns zero if the image is a PNG and nonzero if it isn't a PNG. 00033 * 00034 * The function check_if_png() shown here, but not used, returns nonzero (true) 00035 * if the file can be opened and is a PNG, 0 (false) otherwise. 00036 * 00037 * If this call is successful, and you are going to keep the file open, 00038 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once 00039 * you have created the png_ptr, so that libpng knows your application 00040 * has read that many bytes from the start of the file. Make sure you 00041 * don't call png_set_sig_bytes() with more than 8 bytes read or give it 00042 * an incorrect number of bytes read, or you will either have read too 00043 * many bytes (your fault), or you are telling libpng to read the wrong 00044 * number of magic bytes (also your fault). 00045 * 00046 * Many applications already read the first 2 or 4 bytes from the start 00047 * of the image to determine the file type, so it would be easiest just 00048 * to pass the bytes to png_sig_cmp() or even skip that if you know 00049 * you have a PNG file, and call png_set_sig_bytes(). 00050 */ 00051 #define PNG_BYTES_TO_CHECK 4 00052 int check_if_png(char *file_name, FILE **fp) 00053 { 00054 char buf[PNG_BYTES_TO_CHECK]; 00055 00056 /* Open the prospective PNG file. */ 00057 if ((*fp = fopen(file_name, "rb")) == NULL) 00058 return 0; 00059 00060 /* Read in some of the signature bytes */ 00061 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) 00062 return 0; 00063 00064 /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. 00065 Return nonzero (true) if they match */ 00066 00067 return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK)); 00068 } 00069 00070 /* Read a PNG file. You may want to return an error code if the read 00071 * fails (depending upon the failure). There are two "prototypes" given 00072 * here - one where we are given the filename, and we need to open the 00073 * file, and the other where we are given an open file (possibly with 00074 * some or all of the magic bytes read - see comments above). 00075 */ 00076 #ifdef open_file /* prototype 1 */ 00077 void read_png(char *file_name) /* We need to open the file */ 00078 { 00079 png_structp png_ptr; 00080 png_infop info_ptr; 00081 unsigned int sig_read = 0; 00082 png_uint_32 width, height; 00083 int bit_depth, color_type, interlace_type; 00084 FILE *fp; 00085 00086 if ((fp = fopen(file_name, "rb")) == NULL) 00087 return (ERROR); 00088 #else no_open_file /* prototype 2 */ 00089 void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ 00090 { 00091 png_structp png_ptr; 00092 png_infop info_ptr; 00093 png_uint_32 width, height; 00094 int bit_depth, color_type, interlace_type; 00095 #endif no_open_file /* only use one prototype! */ 00096 00097 /* Create and initialize the png_struct with the desired error handler 00098 * functions. If you want to use the default stderr and longjump method, 00099 * you can supply NULL for the last three parameters. We also supply the 00100 * the compiler header file version, so that we know if the application 00101 * was compiled with a compatible version of the library. REQUIRED 00102 */ 00103 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 00104 png_voidp user_error_ptr, user_error_fn, user_warning_fn); 00105 00106 if (png_ptr == NULL) 00107 { 00108 fclose(fp); 00109 return (ERROR); 00110 } 00111 00112 /* Allocate/initialize the memory for image information. REQUIRED. */ 00113 info_ptr = png_create_info_struct(png_ptr); 00114 if (info_ptr == NULL) 00115 { 00116 fclose(fp); 00117 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL); 00118 return (ERROR); 00119 } 00120 00121 /* Set error handling if you are using the setjmp/longjmp method (this is 00122 * the normal method of doing things with libpng). REQUIRED unless you 00123 * set up your own error handlers in the png_create_read_struct() earlier. 00124 */ 00125 00126 if (setjmp(png_jmpbuf(png_ptr))) 00127 { 00128 /* Free all of the memory associated with the png_ptr and info_ptr */ 00129 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); 00130 fclose(fp); 00131 /* If we get here, we had a problem reading the file */ 00132 return (ERROR); 00133 } 00134 00135 /* One of the following I/O initialization methods is REQUIRED */ 00136 #ifdef streams /* PNG file I/O method 1 */ 00137 /* Set up the input control if you are using standard C streams */ 00138 png_init_io(png_ptr, fp); 00139 00140 #else no_streams /* PNG file I/O method 2 */ 00141 /* If you are using replacement read functions, instead of calling 00142 * png_init_io() here you would call: 00143 */ 00144 png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn); 00145 /* where user_io_ptr is a structure you want available to the callbacks */ 00146 #endif no_streams /* Use only one I/O method! */ 00147 00148 /* If we have already read some of the signature */ 00149 png_set_sig_bytes(png_ptr, sig_read); 00150 00151 #ifdef hilevel 00152 /* 00153 * If you have enough memory to read in the entire image at once, 00154 * and you need to specify only transforms that can be controlled 00155 * with one of the PNG_TRANSFORM_* bits (this presently excludes 00156 * dithering, filling, setting background, and doing gamma 00157 * adjustment), then you can read the entire image (including 00158 * pixels) into the info structure with this call: 00159 */ 00160 png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL); 00161 #else 00162 /* OK, you're doing it the hard way, with the lower-level functions */ 00163 00164 /* The call to png_read_info() gives us all of the information from the 00165 * PNG file before the first IDAT (image data chunk). REQUIRED 00166 */ 00167 png_read_info(png_ptr, info_ptr); 00168 00169 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 00170 &interlace_type, int_p_NULL, int_p_NULL); 00171 00172 /* Set up the data transformations you want. Note that these are all 00173 * optional. Only call them if you want/need them. Many of the 00174 * transformations only work on specific types of images, and many 00175 * are mutually exclusive. 00176 */ 00177 00178 /* tell libpng to strip 16 bit/color files down to 8 bits/color */ 00179 png_set_strip_16(png_ptr); 00180 00181 /* Strip alpha bytes from the input data without combining with the 00182 * background (not recommended). 00183 */ 00184 png_set_strip_alpha(png_ptr); 00185 00186 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single 00187 * byte into separate bytes (useful for paletted and grayscale images). 00188 */ 00189 png_set_packing(png_ptr); 00190 00191 /* Change the order of packed pixels to least significant bit first 00192 * (not useful if you are using png_set_packing). */ 00193 png_set_packswap(png_ptr); 00194 00195 /* Expand paletted colors into true RGB triplets */ 00196 if (color_type == PNG_COLOR_TYPE_PALETTE) 00197 png_set_palette_rgb(png_ptr); 00198 00199 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */ 00200 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) 00201 png_set_gray_1_2_4_to_8(png_ptr); 00202 00203 /* Expand paletted or RGB images with transparency to full alpha channels 00204 * so the data will be available as RGBA quartets. 00205 */ 00206 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) 00207 png_set_tRNS_to_alpha(png_ptr); 00208 00209 /* Set the background color to draw transparent and alpha images over. 00210 * It is possible to set the red, green, and blue components directly 00211 * for paletted images instead of supplying a palette index. Note that 00212 * even if the PNG file supplies a background, you are not required to 00213 * use it - you should use the (solid) application background if it has one. 00214 */ 00215 00216 png_color_16 my_background, *image_background; 00217 00218 if (png_get_bKGD(png_ptr, info_ptr, &image_background)) 00219 png_set_background(png_ptr, image_background, 00220 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); 00221 else 00222 png_set_background(png_ptr, &my_background, 00223 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); 00224 00225 /* Some suggestions as to how to get a screen gamma value */ 00226 00227 /* Note that screen gamma is the display_exponent, which includes 00228 * the CRT_exponent and any correction for viewing conditions */ 00229 if (/* We have a user-defined screen gamma value */) 00230 { 00231 screen_gamma = user-defined screen_gamma; 00232 } 00233 /* This is one way that applications share the same screen gamma value */ 00234 else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL) 00235 { 00236 screen_gamma = atof(gamma_str); 00237 } 00238 /* If we don't have another value */ 00239 else 00240 { 00241 screen_gamma = 2.2; /* A good guess for a PC monitors in a dimly 00242 lit room */ 00243 screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */ 00244 } 00245 00246 /* Tell libpng to handle the gamma conversion for you. The final call 00247 * is a good guess for PC generated images, but it should be configurable 00248 * by the user at run time by the user. It is strongly suggested that 00249 * your application support gamma correction. 00250 */ 00251 00252 int intent; 00253 00254 if (png_get_sRGB(png_ptr, info_ptr, &intent)) 00255 png_set_gamma(png_ptr, screen_gamma, 0.45455); 00256 else 00257 { 00258 double image_gamma; 00259 if (png_get_gAMA(png_ptr, info_ptr, &image_gamma)) 00260 png_set_gamma(png_ptr, screen_gamma, image_gamma); 00261 else 00262 png_set_gamma(png_ptr, screen_gamma, 0.45455); 00263 } 00264 00265 /* Dither RGB files down to 8 bit palette or reduce palettes 00266 * to the number of colors available on your screen. 00267 */ 00268 if (color_type & PNG_COLOR_MASK_COLOR) 00269 { 00270 int num_palette; 00271 png_colorp palette; 00272 00273 /* This reduces the image to the application supplied palette */ 00274 if (/* we have our own palette */) 00275 { 00276 /* An array of colors to which the image should be dithered */ 00277 png_color std_color_cube[MAX_SCREEN_COLORS]; 00278 00279 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS, 00280 MAX_SCREEN_COLORS, png_uint_16p_NULL, 0); 00281 } 00282 /* This reduces the image to the palette supplied in the file */ 00283 else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)) 00284 { 00285 png_uint_16p histogram = NULL; 00286 00287 png_get_hIST(png_ptr, info_ptr, &histogram); 00288 00289 png_set_dither(png_ptr, palette, num_palette, 00290 max_screen_colors, histogram, 0); 00291 } 00292 } 00293 00294 /* invert monochrome files to have 0 as white and 1 as black */ 00295 png_set_invert_mono(png_ptr); 00296 00297 /* If you want to shift the pixel values from the range [0,255] or 00298 * [0,65535] to the original [0,7] or [0,31], or whatever range the 00299 * colors were originally in: 00300 */ 00301 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) 00302 { 00303 png_color_8p sig_bit; 00304 00305 png_get_sBIT(png_ptr, info_ptr, &sig_bit); 00306 png_set_shift(png_ptr, sig_bit); 00307 } 00308 00309 /* flip the RGB pixels to BGR (or RGBA to BGRA) */ 00310 if (color_type & PNG_COLOR_MASK_COLOR) 00311 png_set_bgr(png_ptr); 00312 00313 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ 00314 png_set_swap_alpha(png_ptr); 00315 00316 /* swap bytes of 16 bit files to least significant byte first */ 00317 png_set_swap(png_ptr); 00318 00319 /* Add filler (or alpha) byte (before/after each RGB triplet) */ 00320 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); 00321 00322 /* Turn on interlace handling. REQUIRED if you are not using 00323 * png_read_image(). To see how to handle interlacing passes, 00324 * see the png_read_row() method below: 00325 */ 00326 number_passes = png_set_interlace_handling(png_ptr); 00327 00328 /* Optional call to gamma correct and add the background to the palette 00329 * and update info structure. REQUIRED if you are expecting libpng to 00330 * update the palette for you (ie you selected such a transform above). 00331 */ 00332 png_read_update_info(png_ptr, info_ptr); 00333 00334 /* Allocate the memory to hold the image using the fields of info_ptr. */ 00335 00336 /* The easiest way to read the image: */ 00337 png_bytep row_pointers[height]; 00338 00339 for (row = 0; row < height; row++) 00340 { 00341 row_pointers[row] = png_malloc(png_ptr, png_get_rowbytes(png_ptr, 00342 info_ptr)); 00343 } 00344 00345 /* Now it's time to read the image. One of these methods is REQUIRED */ 00346 #ifdef entire /* Read the entire image in one go */ 00347 png_read_image(png_ptr, row_pointers); 00348 00349 #else no_entire /* Read the image one or more scanlines at a time */ 00350 /* The other way to read images - deal with interlacing: */ 00351 00352 for (pass = 0; pass < number_passes; pass++) 00353 { 00354 #ifdef single /* Read the image a single row at a time */ 00355 for (y = 0; y < height; y++) 00356 { 00357 png_read_rows(png_ptr, &row_pointers[y], png_bytepp_NULL, 1); 00358 } 00359 00360 #else no_single /* Read the image several rows at a time */ 00361 for (y = 0; y < height; y += number_of_rows) 00362 { 00363 #ifdef sparkle /* Read the image using the "sparkle" effect. */ 00364 png_read_rows(png_ptr, &row_pointers[y], png_bytepp_NULL, 00365 number_of_rows); 00366 #else no_sparkle /* Read the image using the "rectangle" effect */ 00367 png_read_rows(png_ptr, png_bytepp_NULL, &row_pointers[y], 00368 number_of_rows); 00369 #endif no_sparkle /* use only one of these two methods */ 00370 } 00371 00372 /* if you want to display the image after every pass, do 00373 so here */ 00374 #endif no_single /* use only one of these two methods */ 00375 } 00376 #endif no_entire /* use only one of these two methods */ 00377 00378 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */ 00379 png_read_end(png_ptr, info_ptr); 00380 #endif hilevel 00381 00382 /* At this point you have read the entire image */ 00383 00384 /* clean up after the read, and free any memory allocated - REQUIRED */ 00385 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); 00386 00387 /* close the file */ 00388 fclose(fp); 00389 00390 /* that's it */ 00391 return (OK); 00392 } 00393 00394 /* progressively read a file */ 00395 00396 int 00397 initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr) 00398 { 00399 /* Create and initialize the png_struct with the desired error handler 00400 * functions. If you want to use the default stderr and longjump method, 00401 * you can supply NULL for the last three parameters. We also check that 00402 * the library version is compatible in case we are using dynamically 00403 * linked libraries. 00404 */ 00405 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 00406 png_voidp user_error_ptr, user_error_fn, user_warning_fn); 00407 00408 if (*png_ptr == NULL) 00409 { 00410 *info_ptr = NULL; 00411 return (ERROR); 00412 } 00413 00414 *info_ptr = png_create_info_struct(png_ptr); 00415 00416 if (*info_ptr == NULL) 00417 { 00418 png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL); 00419 return (ERROR); 00420 } 00421 00422 if (setjmp(png_jmpbuf((*png_ptr)))) 00423 { 00424 png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL); 00425 return (ERROR); 00426 } 00427 00428 /* This one's new. You will need to provide all three 00429 * function callbacks, even if you aren't using them all. 00430 * If you aren't using all functions, you can specify NULL 00431 * parameters. Even when all three functions are NULL, 00432 * you need to call png_set_progressive_read_fn(). 00433 * These functions shouldn't be dependent on global or 00434 * static variables if you are decoding several images 00435 * simultaneously. You should store stream specific data 00436 * in a separate struct, given as the second parameter, 00437 * and retrieve the pointer from inside the callbacks using 00438 * the function png_get_progressive_ptr(png_ptr). 00439 */ 00440 png_set_progressive_read_fn(*png_ptr, (void *)stream_data, 00441 info_callback, row_callback, end_callback); 00442 00443 return (OK); 00444 } 00445 00446 int 00447 process_data(png_structp *png_ptr, png_infop *info_ptr, 00448 png_bytep buffer, png_uint_32 length) 00449 { 00450 if (setjmp(png_jmpbuf((*png_ptr)))) 00451 { 00452 /* Free the png_ptr and info_ptr memory on error */ 00453 png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL); 00454 return (ERROR); 00455 } 00456 00457 /* This one's new also. Simply give it chunks of data as 00458 * they arrive from the data stream (in order, of course). 00459 * On Segmented machines, don't give it any more than 64K. 00460 * The library seems to run fine with sizes of 4K, although 00461 * you can give it much less if necessary (I assume you can 00462 * give it chunks of 1 byte, but I haven't tried with less 00463 * than 256 bytes yet). When this function returns, you may 00464 * want to display any rows that were generated in the row 00465 * callback, if you aren't already displaying them there. 00466 */ 00467 png_process_data(*png_ptr, *info_ptr, buffer, length); 00468 return (OK); 00469 } 00470 00471 info_callback(png_structp png_ptr, png_infop info) 00472 { 00473 /* do any setup here, including setting any of the transformations 00474 * mentioned in the Reading PNG files section. For now, you _must_ 00475 * call either png_start_read_image() or png_read_update_info() 00476 * after all the transformations are set (even if you don't set 00477 * any). You may start getting rows before png_process_data() 00478 * returns, so this is your last chance to prepare for that. 00479 */ 00480 } 00481 00482 row_callback(png_structp png_ptr, png_bytep new_row, 00483 png_uint_32 row_num, int pass) 00484 { 00485 /* 00486 * This function is called for every row in the image. If the 00487 * image is interlaced, and you turned on the interlace handler, 00488 * this function will be called for every row in every pass. 00489 * 00490 * In this function you will receive a pointer to new row data from 00491 * libpng called new_row that is to replace a corresponding row (of 00492 * the same data format) in a buffer allocated by your application. 00493 * 00494 * The new row data pointer new_row may be NULL, indicating there is 00495 * no new data to be replaced (in cases of interlace loading). 00496 * 00497 * If new_row is not NULL then you need to call 00498 * png_progressive_combine_row() to replace the corresponding row as 00499 * shown below: 00500 */ 00501 /* Check if row_num is in bounds. */ 00502 if((row_num >= 0) && (row_num < height)) 00503 { 00504 /* Get pointer to corresponding row in our 00505 * PNG read buffer. 00506 */ 00507 png_bytep old_row = ((png_bytep *)our_data)[row_num]; 00508 00509 /* If both rows are allocated then copy the new row 00510 * data to the corresponding row data. 00511 */ 00512 if((old_row != NULL) && (new_row != NULL)) 00513 png_progressive_combine_row(png_ptr, old_row, new_row); 00514 } 00515 /* 00516 * The rows and passes are called in order, so you don't really 00517 * need the row_num and pass, but I'm supplying them because it 00518 * may make your life easier. 00519 * 00520 * For the non-NULL rows of interlaced images, you must call 00521 * png_progressive_combine_row() passing in the new row and the 00522 * old row, as demonstrated above. You can call this function for 00523 * NULL rows (it will just return) and for non-interlaced images 00524 * (it just does the png_memcpy for you) if it will make the code 00525 * easier. Thus, you can just do this for all cases: 00526 */ 00527 00528 png_progressive_combine_row(png_ptr, old_row, new_row); 00529 00530 /* where old_row is what was displayed for previous rows. Note 00531 * that the first pass (pass == 0 really) will completely cover 00532 * the old row, so the rows do not have to be initialized. After 00533 * the first pass (and only for interlaced images), you will have 00534 * to pass the current row as new_row, and the function will combine 00535 * the old row and the new row. 00536 */ 00537 } 00538 00539 end_callback(png_structp png_ptr, png_infop info) 00540 { 00541 /* this function is called when the whole image has been read, 00542 * including any chunks after the image (up to and including 00543 * the IEND). You will usually have the same info chunk as you 00544 * had in the header, although some data may have been added 00545 * to the comments and time fields. 00546 * 00547 * Most people won't do much here, perhaps setting a flag that 00548 * marks the image as finished. 00549 */ 00550 } 00551 00552 /* write a png file */ 00553 void write_png(char *file_name /* , ... other image information ... */) 00554 { 00555 FILE *fp; 00556 png_structp png_ptr; 00557 png_infop info_ptr; 00558 png_colorp palette; 00559 00560 /* open the file */ 00561 fp = fopen(file_name, "wb"); 00562 if (fp == NULL) 00563 return (ERROR); 00564 00565 /* Create and initialize the png_struct with the desired error handler 00566 * functions. If you want to use the default stderr and longjump method, 00567 * you can supply NULL for the last three parameters. We also check that 00568 * the library version is compatible with the one used at compile time, 00569 * in case we are using dynamically linked libraries. REQUIRED. 00570 */ 00571 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 00572 png_voidp user_error_ptr, user_error_fn, user_warning_fn); 00573 00574 if (png_ptr == NULL) 00575 { 00576 fclose(fp); 00577 return (ERROR); 00578 } 00579 00580 /* Allocate/initialize the image information data. REQUIRED */ 00581 info_ptr = png_create_info_struct(png_ptr); 00582 if (info_ptr == NULL) 00583 { 00584 fclose(fp); 00585 png_destroy_write_struct(&png_ptr, png_infopp_NULL); 00586 return (ERROR); 00587 } 00588 00589 /* Set error handling. REQUIRED if you aren't supplying your own 00590 * error handling functions in the png_create_write_struct() call. 00591 */ 00592 if (setjmp(png_jmpbuf(png_ptr))) 00593 { 00594 /* If we get here, we had a problem reading the file */ 00595 fclose(fp); 00596 png_destroy_write_struct(&png_ptr, &info_ptr); 00597 return (ERROR); 00598 } 00599 00600 /* One of the following I/O initialization functions is REQUIRED */ 00601 #ifdef streams /* I/O initialization method 1 */ 00602 /* set up the output control if you are using standard C streams */ 00603 png_init_io(png_ptr, fp); 00604 #else no_streams /* I/O initialization method 2 */ 00605 /* If you are using replacement read functions, instead of calling 00606 * png_init_io() here you would call */ 00607 png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn, 00608 user_IO_flush_function); 00609 /* where user_io_ptr is a structure you want available to the callbacks */ 00610 #endif no_streams /* only use one initialization method */ 00611 00612 #ifdef hilevel 00613 /* This is the easy way. Use it if you already have all the 00614 * image info living info in the structure. You could "|" many 00615 * PNG_TRANSFORM flags into the png_transforms integer here. 00616 */ 00617 png_write_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL); 00618 #else 00619 /* This is the hard way */ 00620 00621 /* Set the image information here. Width and height are up to 2^31, 00622 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on 00623 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, 00624 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, 00625 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or 00626 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST 00627 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED 00628 */ 00629 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???, 00630 PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 00631 00632 /* set the palette if there is one. REQUIRED for indexed-color images */ 00633 palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH 00634 * png_sizeof (png_color)); 00635 /* ... set palette colors ... */ 00636 png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH); 00637 /* You must not free palette here, because png_set_PLTE only makes a link to 00638 the palette that you malloced. Wait until you are about to destroy 00639 the png structure. */ 00640 00641 /* optional significant bit chunk */ 00642 /* if we are dealing with a grayscale image then */ 00643 sig_bit.gray = true_bit_depth; 00644 /* otherwise, if we are dealing with a color image then */ 00645 sig_bit.red = true_red_bit_depth; 00646 sig_bit.green = true_green_bit_depth; 00647 sig_bit.blue = true_blue_bit_depth; 00648 /* if the image has an alpha channel then */ 00649 sig_bit.alpha = true_alpha_bit_depth; 00650 png_set_sBIT(png_ptr, info_ptr, sig_bit); 00651 00652 00653 /* Optional gamma chunk is strongly suggested if you have any guess 00654 * as to the correct gamma of the image. 00655 */ 00656 png_set_gAMA(png_ptr, info_ptr, gamma); 00657 00658 /* Optionally write comments into the image */ 00659 text_ptr[0].key = "Title"; 00660 text_ptr[0].text = "Mona Lisa"; 00661 text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; 00662 text_ptr[1].key = "Author"; 00663 text_ptr[1].text = "Leonardo DaVinci"; 00664 text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE; 00665 text_ptr[2].key = "Description"; 00666 text_ptr[2].text = "<long text>"; 00667 text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; 00668 #ifdef PNG_iTXt_SUPPORTED 00669 text_ptr[0].lang = NULL; 00670 text_ptr[1].lang = NULL; 00671 text_ptr[2].lang = NULL; 00672 #endif 00673 png_set_text(png_ptr, info_ptr, text_ptr, 3); 00674 00675 /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */ 00676 /* note that if sRGB is present the gAMA and cHRM chunks must be ignored 00677 * on read and must be written in accordance with the sRGB profile */ 00678 00679 /* Write the file header information. REQUIRED */ 00680 png_write_info(png_ptr, info_ptr); 00681 00682 /* If you want, you can write the info in two steps, in case you need to 00683 * write your private chunk ahead of PLTE: 00684 * 00685 * png_write_info_before_PLTE(write_ptr, write_info_ptr); 00686 * write_my_chunk(); 00687 * png_write_info(png_ptr, info_ptr); 00688 * 00689 * However, given the level of known- and unknown-chunk support in 1.1.0 00690 * and up, this should no longer be necessary. 00691 */ 00692 00693 /* Once we write out the header, the compression type on the text 00694 * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or 00695 * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again 00696 * at the end. 00697 */ 00698 00699 /* set up the transformations you want. Note that these are 00700 * all optional. Only call them if you want them. 00701 */ 00702 00703 /* invert monochrome pixels */ 00704 png_set_invert_mono(png_ptr); 00705 00706 /* Shift the pixels up to a legal bit depth and fill in 00707 * as appropriate to correctly scale the image. 00708 */ 00709 png_set_shift(png_ptr, &sig_bit); 00710 00711 /* pack pixels into bytes */ 00712 png_set_packing(png_ptr); 00713 00714 /* swap location of alpha bytes from ARGB to RGBA */ 00715 png_set_swap_alpha(png_ptr); 00716 00717 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into 00718 * RGB (4 channels -> 3 channels). The second parameter is not used. 00719 */ 00720 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); 00721 00722 /* flip BGR pixels to RGB */ 00723 png_set_bgr(png_ptr); 00724 00725 /* swap bytes of 16-bit files to most significant byte first */ 00726 png_set_swap(png_ptr); 00727 00728 /* swap bits of 1, 2, 4 bit packed pixel formats */ 00729 png_set_packswap(png_ptr); 00730 00731 /* turn on interlace handling if you are not using png_write_image() */ 00732 if (interlacing) 00733 number_passes = png_set_interlace_handling(png_ptr); 00734 else 00735 number_passes = 1; 00736 00737 /* The easiest way to write the image (you may have a different memory 00738 * layout, however, so choose what fits your needs best). You need to 00739 * use the first method if you aren't handling interlacing yourself. 00740 */ 00741 png_uint_32 k, height, width; 00742 png_byte image[height][width*bytes_per_pixel]; 00743 png_bytep row_pointers[height]; 00744 00745 if (height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) 00746 png_error (png_ptr, "Image is too tall to process in memory"); 00747 00748 for (k = 0; k < height; k++) 00749 row_pointers[k] = image + k*width*bytes_per_pixel; 00750 00751 /* One of the following output methods is REQUIRED */ 00752 #ifdef entire /* write out the entire image data in one call */ 00753 png_write_image(png_ptr, row_pointers); 00754 00755 /* the other way to write the image - deal with interlacing */ 00756 00757 #else no_entire /* write out the image data by one or more scanlines */ 00758 /* The number of passes is either 1 for non-interlaced images, 00759 * or 7 for interlaced images. 00760 */ 00761 for (pass = 0; pass < number_passes; pass++) 00762 { 00763 /* Write a few rows at a time. */ 00764 png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows); 00765 00766 /* If you are only writing one row at a time, this works */ 00767 for (y = 0; y < height; y++) 00768 { 00769 png_write_rows(png_ptr, &row_pointers[y], 1); 00770 } 00771 } 00772 #endif no_entire /* use only one output method */ 00773 00774 /* You can write optional chunks like tEXt, zTXt, and tIME at the end 00775 * as well. Shouldn't be necessary in 1.1.0 and up as all the public 00776 * chunks are supported and you can use png_set_unknown_chunks() to 00777 * register unknown chunks into the info structure to be written out. 00778 */ 00779 00780 /* It is REQUIRED to call this to finish writing the rest of the file */ 00781 png_write_end(png_ptr, info_ptr); 00782 #endif hilevel 00783 00784 /* If you png_malloced a palette, free it here (don't free info_ptr->palette, 00785 as recommended in versions 1.0.5m and earlier of this example; if 00786 libpng mallocs info_ptr->palette, libpng will free it). If you 00787 allocated it with malloc() instead of png_malloc(), use free() instead 00788 of png_free(). */ 00789 png_free(png_ptr, palette); 00790 palette=NULL; 00791 00792 /* Similarly, if you png_malloced any data that you passed in with 00793 png_set_something(), such as a hist or trans array, free it here, 00794 when you can be sure that libpng is through with it. */ 00795 png_free(png_ptr, trans); 00796 trans=NULL; 00797 00798 /* clean up after the write, and free any memory allocated */ 00799 png_destroy_write_struct(&png_ptr, &info_ptr); 00800 00801 /* close the file */ 00802 fclose(fp); 00803 00804 /* that's it */ 00805 return (OK); 00806 } 00807 00808 #endif /* if 0 */