Lines Matching +full:set +full:- +full:image
4 /* example.c - an example of using libpng
6 * Maintained 2018-2024 Cosmin Truta
7 * Maintained 1998-2016 Glenn Randers-Pehrson
8 * Maintained 1996-1997 Andreas Dilger
9 * Written 1995-1996 Guy Eric Schalnat, Group 42, Inc.
17 * The file libpng-manual.txt is much more verbose then this. If you have
23 * parts, like allocating memory to hold an image. You will have to
34 * The following code reads PNG image data from a file and writes it, in a
51 png_image image; /* The control structure used by libpng */
54 memset(&image, 0, (sizeof image));
55 image.version = PNG_IMAGE_VERSION;
58 if (png_image_begin_read_from_file(&image, argv[1]) != 0)
62 /* Set the format in which to read the PNG file; this code chooses a
63 * simple sRGB format with a non-associated alpha channel, adequate to
66 image.format = PNG_FORMAT_RGBA;
68 /* Now allocate enough memory to hold the image in this format; the
69 * PNG_IMAGE_SIZE macro uses the information about the image (width,
70 * height and format) stored in 'image'.
72 buffer = malloc(PNG_IMAGE_SIZE(image));
74 /* If enough memory was available, read the image in the desired
76 * not necessary when reading the image, because the alpha channel is
80 * the actual background of the image.
82 * The fourth argument to png_image_finish_read is the 'row_stride' -
83 * this is the number of components allocated for the image in each
90 * colormaps have exactly the same format as a row of image pixels
92 * image.format). A colormap is only returned if
93 * PNG_FORMAT_FLAG_COLORMAP is also set in image.format, so in this
95 * all images into an index/color-mapped format, then you can use:
97 * PNG_IMAGE_COLORMAP_SIZE(image)
102 png_image_finish_read(&image, NULL/*background*/, buffer,
105 /* Now write the image out to the second argument. In the write
106 * call 'convert_to_8bit' allows 16-bit data to be squashed down to
108 * to the 8-bit format.
110 if (png_image_write_to_file(&image, argv[2], 0/*convert_to_8bit*/,
113 /* The image has been written successfully. */
122 * free the image:
125 png_image_free(&image);
131 /* Something went wrong reading or writing the image. libpng stores a
134 fprintf(stderr, "pngtopng: error: %s\n", image.message);
138 fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n");
142 /* That's it ;-) Of course you probably want to do more with PNG files than
143 * just converting them all to 32-bit RGBA PNG files; you can do that between
145 * ask for the image data to be presented in a number of different formats.
146 * You do this by simply changing the 'format' parameter set before allocating
150 * the image. You can simply add these together to get the format, or you can
153 * PNG_FORMAT_FLAG_COLOR: if set, the image will have three color components
154 * per pixel (red, green and blue); if not set, the image will just have one
157 * PNG_FORMAT_FLAG_ALPHA: if set, each pixel in the image will have an
159 * image pixel covers (overwrites) the contents of the existing pixel on the
162 * PNG_FORMAT_FLAG_LINEAR: if set, the components of each pixel will be
163 * returned as a series of 16-bit linear values; if not set, the components
164 * will be returned as a series of 8-bit values encoded according to the
165 * sRGB standard. The 8-bit format is the normal format for images intended
167 * the sRGB transformation to the data they receive. The 16-bit format is
168 * more common for scientific data and image data that must be further
174 * PNG_FORMAT_FLAG_BGR: if set, the components of a color pixel will be
175 * returned in the order blue, then green, then red. If not set, the pixel
178 * PNG_FORMAT_FLAG_AFIRST: if set, the alpha channel (if present) precedes the
179 * color or grayscale components. If not set, the alpha channel follows the
189 * If you read 16-bit (PNG_FORMAT_FLAG_LINEAR) data, you may need to write it
190 * in the 8-bit format for display. You do this by setting the convert_to_8bit
193 * Don't repeatedly convert between the 8-bit and 16-bit forms. There is
194 * significant data loss when 16-bit data is converted to the 8-bit encoding,
195 * and the current libpng implementation of conversion to 16-bit is also
197 * is unavoidable - the 8-bit format just doesn't have enough resolution.
205 * All these interfaces require that you do your own error handling - your
225 # define png_jmpbuf(png_ptr) ((png_ptr)->png_jmpbuf)
229 * returns zero if the image is a PNG, and nonzero otherwise.
244 * of the image to determine the file type, so it would be easiest just
269 * here - one where we are given the filename, and we need to open the
271 * some or all of the magic bytes read - see comments above).
310 /* Allocate/initialize the memory for image information. REQUIRED. */
319 /* Set error handling if you are using the setjmp/longjmp method (this is
321 * set up your own error handlers in the png_create_read_struct() earlier.
334 /* Set up the input control if you are using standard C streams. */
349 /* If you have enough memory to read in the entire image at once,
353 * adjustment), then you can read the entire image (including
359 /* OK, you're doing it the hard way, with the lower-level functions. */
362 * PNG file before the first IDAT (image data chunk). REQUIRED.
369 /* Set up the data transformations you want. Note that these are all
414 /* Set the background color to draw transparent and alpha images over.
415 * It is possible to set the red, green and blue components directly
418 * use it - you should use the (solid) application background if it has one.
434 if (/* We have a user-defined screen gamma value */)
435 screen_gamma = user-defined screen_gamma;
468 /* Quantize RGB files down to 8-bit palette, or reduce palettes
476 /* This reduces the image to the application-supplied palette. */
479 /* An array of colors to which the image should be quantized. */
484 /* This reduces the image to the palette supplied in the file. */
516 /* Swap bytes of 16-bit files to least significant byte first. */
538 /* Allocate the memory to hold the image using the fields of info_ptr. */
546 /* Now it's time to read the image. One of these methods is REQUIRED. */
547 #ifdef entire /* Read the entire image in one go */
550 #else no_entire /* Read the image one or more scanlines at a time */
551 /* The other way to read images - deal with interlacing: */
554 #ifdef single /* Read the image a single row at a time */
558 #else no_single /* Read the image several rows at a time */
561 #ifdef sparkle /* Read the image using the "sparkle" effect. */
564 #else no_sparkle /* Read the image using the "rectangle" effect */
570 /* If you want to display the image after every pass, do so here. */
579 /* At this point you have read the entire image. */
668 * after all the transformations are set (even if you don't set
677 /* This function is called for every row in the image. If the
678 * image is interlaced, and you turned on the interlace handler,
707 * For the non-NULL rows of interlaced images, you must call
710 * NULL rows (it will just return) and for non-interlaced images
728 /* This function is called when the whole image has been read,
729 * including any chunks after the image (up to and including
735 * marks the image as finished.
740 void write_png(char *file_name /* , ... other image information ... */)
766 /* Allocate/initialize the image information data. REQUIRED. */
775 /* Set up error handling. REQUIRED if you aren't supplying your own
789 /* Set up the output control if you are using standard C streams. */
803 * image info living in the structure. You could "|" many
811 /* Set the image information here. Width and height are up to 2^31,
824 /* Set the palette if there is one. REQUIRED for indexed-color images. */
827 /* ... Set palette colors ... */
837 /* If we are dealing with a grayscale image then */
840 /* Otherwise, if we are dealing with a color image then */
845 /* If the image has an alpha channel then */
851 * as to the correct gamma of the image.
855 /* Optionally write comments into the image. */
906 * However, given the level of known- and unknown-chunk support in 1.2.0
916 /* Set up the transformations you want. Note that these are
924 * as appropriate to correctly scale the image.
935 * RGB (4 channels -> 3 channels). The second parameter is not used.
942 /* Swap bytes of 16-bit files to most significant byte first. */
945 /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats. */
954 /* The easiest way to write the image (you may have a different memory
960 /* In this example, "image" is a one-dimensional array of bytes. */
964 png_error(png_ptr, "Image data buffer would be too large");
966 png_byte image[height * width * bytes_per_pixel];
970 png_error(png_ptr, "Image is too tall to process in memory");
972 /* Set up pointers into your "image" byte array. */
974 row_pointers[k] = image + k * width * bytes_per_pixel;
978 #ifdef entire /* Write out the entire image data in one call */
981 /* The other way to write the image - deal with interlacing. */
983 #else no_entire /* Write out the image data by one or more scanlines */
985 /* The number of passes is either 1 for non-interlaced images,
1010 * (Don't free info_ptr->palette, as shown in versions 1.0.5m and earlier of
1011 * this example; if libpng mallocs info_ptr->palette, libpng will free it).
1025 /* Whenever you use png_free(), it is a good idea to set the pointer to
1028 * the double-free problem.