PngFile.c revision 671242f350d172e106580348e24bab66b0d7e6a5
671242f350d172e106580348e24bab66b0d7e6a5vboxsync//-------------------------------------
671242f350d172e106580348e24bab66b0d7e6a5vboxsync// PNGFILE.C -- Image File Functions
671242f350d172e106580348e24bab66b0d7e6a5vboxsync//-------------------------------------
671242f350d172e106580348e24bab66b0d7e6a5vboxsync// Copyright 2000, Willem van Schaik. For conditions of distribution and
671242f350d172e106580348e24bab66b0d7e6a5vboxsync// use, see the copyright/license/disclaimer notice in png.h
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncextern struct exception_context the_exception_context[1];
671242f350d172e106580348e24bab66b0d7e6a5vboxsync// cexcept interface
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncpng_cexcept_error(png_structp png_ptr, png_const_charp msg)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync// Windows open-file functions
671242f350d172e106580348e24bab66b0d7e6a5vboxsync static TCHAR szFilter[] = TEXT ("PNG Files (*.PNG)\0*.png\0")
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ofn.lpstrFile = NULL; // Set in Open and Close functions
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ofn.lpstrFileTitle = NULL; // Set in Open and Close functions
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncBOOL PngFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncBOOL PngFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync// PNG image handler functions
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncBOOL PngLoadImage (PTSTR pstrFileName, png_byte **ppbImageData,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync int *piWidth, int *piHeight, int *piChannels, png_color *pBkgColor)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // open the PNG input file
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // first check the eight byte PNG signature
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // create the two png(-info) structures
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // initialize the png structure
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_read_fn(png_ptr, (png_voidp)pfFile, png_read_data);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // read all PNG info up to image data
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // get width, height, bit-depth and color-type
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // expand images of all color-type and bit-depth to 3x8 bit RGB images
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // let the library process things like alpha, transparency, background
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // set the background color to draw transparent and alpha images over.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // if required set gamma conversion
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // after the transformations have been registered update info_ptr data
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // get again width, height and the new bit-depth and color-type
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // row_bytes is the width x number of channels
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // now we can allocate memory to store the image
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if ((pbImageData = (png_byte *) malloc(ulRowBytes * (*piHeight)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // and allocate memory for an array of row-pointers
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if ((ppbRowPointers = (png_bytepp) malloc((*piHeight)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // set the individual row-pointers to point at the correct offsets
671242f350d172e106580348e24bab66b0d7e6a5vboxsync for (i = 0; i < (*piHeight); i++)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // now we can go ahead and just read the whole image
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // read the additional chunks in the PNG file (not really needed)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // and we're done
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // yepp, done
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncBOOL PngSaveImage (PTSTR pstrFileName, png_byte *pDiData,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // open the PNG output file
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // prepare the standard PNG structures
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // initialize the png structure
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_write_fn(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // we're going to write a very simple 3x8 bit RGB image
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_IHDR(png_ptr, info_ptr, iWidth, iHeight, ciBitDepth,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // write the file header information
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // swap the BGR pixels in the DiData structure to RGB
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // row_bytes is the width x number of channels
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // we can allocate memory for an array of row-pointers
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if ((ppbRowPointers = (png_bytepp) malloc(iHeight * sizeof(png_bytep))) == NULL)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // set the individual row-pointers to point at the correct offsets
671242f350d172e106580348e24bab66b0d7e6a5vboxsync for (i = 0; i < iHeight; i++)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ppbRowPointers[i] = pDiData + i * (((ulRowBytes + 3) >> 2) << 2);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // write out the entire image data in one call
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // write the additional chunks to the PNG file (not really needed)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // and we're done
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // clean up after the write, and free any memory allocated
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync // yepp, done
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncpng_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* fread() returns 0 on error, so it is OK to store this in a png_size_t
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * instead of an int, which is what fread() actually returns.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync check = (png_size_t)fread(data, (png_size_t)1, length,
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncpng_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
671242f350d172e106580348e24bab66b0d7e6a5vboxsync//-----------------
671242f350d172e106580348e24bab66b0d7e6a5vboxsync// end of source
671242f350d172e106580348e24bab66b0d7e6a5vboxsync//-----------------