diff options
author | Glenn Randers-Pehrson <glennrp at users.sourceforge.net> | 2013-03-02 14:58:22 -0600 |
---|---|---|
committer | Glenn Randers-Pehrson <glennrp at users.sourceforge.net> | 2013-03-02 15:03:15 -0600 |
commit | 871b1d0fabee7995383cd6941362b68a03f86e25 (patch) | |
tree | 5b685f9d4c874b4e435db96b66cd20654b8b6ddf /example.c | |
parent | f3af706c2a489333f816843bc986a6bafe47719b (diff) |
[libpng16] Bump version to 1.6.1beta05
Diffstat (limited to 'example.c')
-rw-r--r-- | example.c | 86 |
1 files changed, 58 insertions, 28 deletions
@@ -2,8 +2,8 @@ #if 0 /* in case someone actually tries to compile this */ /* example.c - an example of using libpng - * Last changed in libpng 1.6.0 [(PENDING RELEASE)] - * Maintained 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.0 [February 14, 2013] + * Maintained 1998-2013 Glenn Randers-Pehrson * Maintained 1996, 1997 Andreas Dilger) * Written 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * To the extent possible under law, the authors have waived @@ -48,7 +48,8 @@ int main(int argc, const char **argv) png_image image; /* The control structure used by libpng */ /* Initialize the 'png_image' structure. */ - memset(&image, 0, sizeof image); + memset(&image, 0, (sizeof image)); + image.version = PNG_IMAGE_VERSION; /* The first argument is the file to read: */ if (png_image_begin_read_from_file(&image, argv[1])) @@ -75,16 +76,28 @@ int main(int argc, const char **argv) * be supplied or the output buffer would have to be initialized to the * actual background of the image. * - * The final argument to png_image_finish_read is the 'row_stride' - + * The fourth argument to png_image_finish_read is the 'row_stride' - * this is the number of components allocated for the image in each * row. It has to be at least as big as the value returned by * PNG_IMAGE_ROW_STRIDE, but if you just allocate space for the * default, minimum, size using PNG_IMAGE_SIZE as above you can pass * zero. + * + * The final argument is a pointer to a buffer for the colormap; + * colormaps have exactly the same format as a row of image pixels (so + * you choose what format to make the colormap by setting + * image.format). A colormap is only returned if + * PNG_FORMAT_FLAG_COLORMAP is also set in image.format, so in this + * case NULL is passed as the final argument. If you do want to force + * all images into an index/color-mapped format then you can use: + * + * PNG_IMAGE_COLORMAP_SIZE(image) + * + * to find the maximum size of the colormap in bytes. */ if (buffer != NULL && png_image_finish_read(&image, NULL/*background*/, buffer, - 0/*row_stride*/)) + 0/*row_stride*/, NULL/*colormap*/)) { /* Now write the image out to the second argument. In the write * call 'convert_to_8bit' allows 16-bit data to be squashed down to @@ -92,7 +105,7 @@ int main(int argc, const char **argv) * to the 8-bit format. */ if (png_image_write_to_file(&image, argv[2], 0/*convert_to_8bit*/, - buffer, 0/*row_stride*/)) + buffer, 0/*row_stride*/, NULL/*colormap*/)) { /* The image has been written successfully. */ exit(0); @@ -833,7 +846,7 @@ void write_png(char *file_name /* , ... other image information ... */) /* Set the palette if there is one. REQUIRED for indexed-color images */ palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH - * png_sizeof(png_color)); + * (sizeof (png_color))); /* ... Set palette colors ... */ png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH); /* You must not free palette here, because png_set_PLTE only makes a link to @@ -864,25 +877,38 @@ void write_png(char *file_name /* , ... other image information ... */) png_set_gAMA(png_ptr, info_ptr, gamma); /* Optionally write comments into the image */ - text_ptr[0].key = "Title"; - text_ptr[0].text = "Mona Lisa"; - text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; - text_ptr[0].itxt_length = 0; - text_ptr[0].lang = NULL; - text_ptr[0].lang_key = NULL; - text_ptr[1].key = "Author"; - text_ptr[1].text = "Leonardo DaVinci"; - text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE; - text_ptr[1].itxt_length = 0; - text_ptr[1].lang = NULL; - text_ptr[1].lang_key = NULL; - text_ptr[2].key = "Description"; - text_ptr[2].text = "<long text>"; - text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; - text_ptr[2].itxt_length = 0; - text_ptr[2].lang = NULL; - text_ptr[2].lang_key = NULL; - png_set_text(png_ptr, info_ptr, text_ptr, 3); + { + png_text text_ptr[3]; + + char key0[]="Title"; + char text0[]="Mona Lisa"; + text_ptr[0].key = key0; + text_ptr[0].text = text0; + text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; + text_ptr[0].itxt_length = 0; + text_ptr[0].lang = NULL; + text_ptr[0].lang_key = NULL; + + char key1[]="Author"; + char text1[]="Leonardo DaVinci"; + text_ptr[1].key = key1; + text_ptr[1].text = text1; + text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE; + text_ptr[1].itxt_length = 0; + text_ptr[1].lang = NULL; + text_ptr[1].lang_key = NULL; + + char key2[]="Description"; + char text2[]="<long text>"; + text_ptr[2].key = key2; + text_ptr[2].text = text2; + text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; + text_ptr[2].itxt_length = 0; + text_ptr[2].lang = NULL; + text_ptr[2].lang_key = NULL; + + png_set_text(write_ptr, write_info_ptr, text_ptr, 3); + } /* Other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs */ @@ -955,12 +981,16 @@ void write_png(char *file_name /* , ... other image information ... */) * use the first method if you aren't handling interlacing yourself. */ png_uint_32 k, height, width; - png_byte image[height][width*bytes_per_pixel]; + + /* In this example, "image" is a one-dimensional array of bytes */ + png_byte image[height*width*bytes_per_pixel]; + png_bytep row_pointers[height]; - if (height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) + if (height > PNG_UINT_32_MAX/(sizeof (png_bytep))) png_error (png_ptr, "Image is too tall to process in memory"); + /* Set up pointers into your "image" byte array */ for (k = 0; k < height; k++) row_pointers[k] = image + k*width*bytes_per_pixel; |