fukasawa e60969
/*-------------------------------------
fukasawa e60969
 *  PNGFILE.C -- Image File Functions
fukasawa e60969
 *-------------------------------------
fukasawa e60969
 *
fukasawa e60969
 * Copyright 2000, Willem van Schaik.
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include <windows.h></windows.h>
fukasawa e60969
#include <commdlg.h></commdlg.h>
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <zlib.h></zlib.h>
fukasawa e60969
fukasawa e60969
#include "png.h"
fukasawa e60969
#include "pngfile.h"
fukasawa e60969
#include "cexcept.h"
fukasawa e60969
fukasawa e60969
define_exception_type(const char *);
fukasawa e60969
extern struct exception_context the_exception_context[1];
fukasawa e60969
struct exception_context the_exception_context[1];
fukasawa e60969
png_const_charp msg;
fukasawa e60969
fukasawa e60969
static OPENFILENAME ofn;
fukasawa e60969
fukasawa e60969
static png_structp png_ptr = NULL;
fukasawa e60969
static png_infop info_ptr = NULL;
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* cexcept interface */
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
png_cexcept_error(png_structp png_ptr, png_const_charp msg)
fukasawa e60969
{
fukasawa e60969
   if(png_ptr)
fukasawa e60969
     ;
fukasawa e60969
#ifdef PNG_CONSOLE_IO_SUPPORTED
fukasawa e60969
   fprintf(stderr, "libpng error: %s\n", msg);
fukasawa e60969
#endif
fukasawa e60969
   {
fukasawa e60969
      Throw msg;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Windows open-file functions */
fukasawa e60969
fukasawa e60969
void PngFileInitialize (HWND hwnd)
fukasawa e60969
{
fukasawa e60969
    static TCHAR szFilter[] = TEXT ("PNG Files (*.PNG)\0*.png\0")
fukasawa e60969
        TEXT ("All Files (*.*)\0*.*\0\0");
fukasawa e60969
fukasawa e60969
    ofn.lStructSize       = sizeof (OPENFILENAME);
fukasawa e60969
    ofn.hwndOwner         = hwnd;
fukasawa e60969
    ofn.hInstance         = NULL;
fukasawa e60969
    ofn.lpstrFilter       = szFilter;
fukasawa e60969
    ofn.lpstrCustomFilter = NULL;
fukasawa e60969
    ofn.nMaxCustFilter    = 0;
fukasawa e60969
    ofn.nFilterIndex      = 0;
fukasawa e60969
    ofn.lpstrFile         = NULL;          /* Set in Open and Close functions */
fukasawa e60969
    ofn.nMaxFile          = MAX_PATH;
fukasawa e60969
    ofn.lpstrFileTitle    = NULL;          /* Set in Open and Close functions */
fukasawa e60969
    ofn.nMaxFileTitle     = MAX_PATH;
fukasawa e60969
    ofn.lpstrInitialDir   = NULL;
fukasawa e60969
    ofn.lpstrTitle        = NULL;
fukasawa e60969
    ofn.Flags             = 0;             /* Set in Open and Close functions */
fukasawa e60969
    ofn.nFileOffset       = 0;
fukasawa e60969
    ofn.nFileExtension    = 0;
fukasawa e60969
    ofn.lpstrDefExt       = TEXT ("png");
fukasawa e60969
    ofn.lCustData         = 0;
fukasawa e60969
    ofn.lpfnHook          = NULL;
fukasawa e60969
    ofn.lpTemplateName    = NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
BOOL PngFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
fukasawa e60969
{
fukasawa e60969
    ofn.hwndOwner         = hwnd;
fukasawa e60969
    ofn.lpstrFile         = pstrFileName;
fukasawa e60969
    ofn.lpstrFileTitle    = pstrTitleName;
fukasawa e60969
    ofn.Flags             = OFN_HIDEREADONLY;
fukasawa e60969
fukasawa e60969
    return GetOpenFileName (&ofn);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
BOOL PngFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
fukasawa e60969
{
fukasawa e60969
    ofn.hwndOwner         = hwnd;
fukasawa e60969
    ofn.lpstrFile         = pstrFileName;
fukasawa e60969
    ofn.lpstrFileTitle    = pstrTitleName;
fukasawa e60969
    ofn.Flags             = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
fukasawa e60969
fukasawa e60969
    return GetSaveFileName (&ofn);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* PNG image handler functions */
fukasawa e60969
fukasawa e60969
BOOL PngLoadImage (PTSTR pstrFileName, png_byte **ppbImageData,
fukasawa e60969
                   int *piWidth, int *piHeight, int *piChannels, png_color *pBkgColor)
fukasawa e60969
{
fukasawa e60969
    static FILE        *pfFile;
fukasawa e60969
    png_byte            pbSig[8];
fukasawa e60969
    int                 iBitDepth;
fukasawa e60969
    int                 iColorType;
fukasawa e60969
    double              dGamma;
fukasawa e60969
    png_color_16       *pBackground;
fukasawa e60969
    png_uint_32         ulChannels;
fukasawa e60969
    png_uint_32         ulRowBytes;
fukasawa e60969
    png_byte           *pbImageData = *ppbImageData;
fukasawa e60969
    static png_byte   **ppbRowPointers = NULL;
fukasawa e60969
    int                 i;
fukasawa e60969
fukasawa e60969
    /* open the PNG input file */
fukasawa e60969
fukasawa e60969
    if (!pstrFileName)
fukasawa e60969
    {
fukasawa e60969
        *ppbImageData = pbImageData = NULL;
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    if (!(pfFile = fopen(pstrFileName, "rb")))
fukasawa e60969
    {
fukasawa e60969
        *ppbImageData = pbImageData = NULL;
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    /* first check the eight byte PNG signature */
fukasawa e60969
fukasawa e60969
    fread(pbSig, 1, 8, pfFile);
fukasawa e60969
    if (png_sig_cmp(pbSig, 0, 8))
fukasawa e60969
    {
fukasawa e60969
        *ppbImageData = pbImageData = NULL;
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    /* create the two png(-info) structures */
fukasawa e60969
fukasawa e60969
    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
fukasawa e60969
      (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
fukasawa e60969
    if (!png_ptr)
fukasawa e60969
    {
fukasawa e60969
        *ppbImageData = pbImageData = NULL;
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    info_ptr = png_create_info_struct(png_ptr);
fukasawa e60969
    if (!info_ptr)
fukasawa e60969
    {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, NULL, NULL);
fukasawa e60969
        *ppbImageData = pbImageData = NULL;
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    Try
fukasawa e60969
    {
fukasawa e60969
fukasawa e60969
        /* initialize the png structure */
fukasawa e60969
fukasawa e60969
#ifdef PNG_STDIO_SUPPORTED
fukasawa e60969
        png_init_io(png_ptr, pfFile);
fukasawa e60969
#else
fukasawa e60969
        png_set_read_fn(png_ptr, (png_voidp)pfFile, png_read_data);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
        png_set_sig_bytes(png_ptr, 8);
fukasawa e60969
fukasawa e60969
        /* read all PNG info up to image data */
fukasawa e60969
fukasawa e60969
        png_read_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
        /* get width, height, bit-depth and color-type */
fukasawa e60969
fukasawa e60969
        png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
fukasawa e60969
            &iColorType, NULL, NULL, NULL);
fukasawa e60969
fukasawa e60969
        /* expand images of all color-type and bit-depth to 3x8-bit RGB */
fukasawa e60969
        /* let the library process alpha, transparency, background, etc. */
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_16_TO_8_SUPPORTED
fukasawa e60969
    if (iBitDepth == 16)
fukasawa e60969
#  ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
fukasawa e60969
        png_set_scale_16(png_ptr);
fukasawa e60969
#  else
fukasawa e60969
        png_set_strip_16(png_ptr);
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
        if (iColorType == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
            png_set_expand(png_ptr);
fukasawa e60969
        if (iBitDepth < 8)
fukasawa e60969
            png_set_expand(png_ptr);
fukasawa e60969
        if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
fukasawa e60969
            png_set_expand(png_ptr);
fukasawa e60969
        if (iColorType == PNG_COLOR_TYPE_GRAY ||
fukasawa e60969
            iColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
fukasawa e60969
            png_set_gray_to_rgb(png_ptr);
fukasawa e60969
fukasawa e60969
        /* set the background color to draw transparent and alpha images over */
fukasawa e60969
        if (png_get_bKGD(png_ptr, info_ptr, &pBackground))
fukasawa e60969
        {
fukasawa e60969
            png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
fukasawa e60969
            pBkgColor->red   = (byte) pBackground->red;
fukasawa e60969
            pBkgColor->green = (byte) pBackground->green;
fukasawa e60969
            pBkgColor->blue  = (byte) pBackground->blue;
fukasawa e60969
        }
fukasawa e60969
        else
fukasawa e60969
        {
fukasawa e60969
            pBkgColor = NULL;
fukasawa e60969
        }
fukasawa e60969
fukasawa e60969
        /* if required set gamma conversion */
fukasawa e60969
        if (png_get_gAMA(png_ptr, info_ptr, &dGamma))
fukasawa e60969
            png_set_gamma(png_ptr, (double) 2.2, dGamma);
fukasawa e60969
fukasawa e60969
        /* after the transformations are registered, update info_ptr data */
fukasawa e60969
fukasawa e60969
        png_read_update_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
        /* get again width, height and the new bit-depth and color-type */
fukasawa e60969
fukasawa e60969
        png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
fukasawa e60969
            &iColorType, NULL, NULL, NULL);
fukasawa e60969
fukasawa e60969
fukasawa e60969
        /* row_bytes is the width x number of channels */
fukasawa e60969
fukasawa e60969
        ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
fukasawa e60969
        ulChannels = png_get_channels(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
        *piChannels = ulChannels;
fukasawa e60969
fukasawa e60969
        /* now we can allocate memory to store the image */
fukasawa e60969
fukasawa e60969
        if (pbImageData)
fukasawa e60969
        {
fukasawa e60969
            free (pbImageData);
fukasawa e60969
            pbImageData = NULL;
fukasawa e60969
        }
fukasawa e60969
        if ((pbImageData = (png_byte *) malloc(ulRowBytes * (*piHeight)
fukasawa e60969
                            * sizeof(png_byte))) == NULL)
fukasawa e60969
        {
fukasawa e60969
            png_error(png_ptr, "Visual PNG: out of memory");
fukasawa e60969
        }
fukasawa e60969
        *ppbImageData = pbImageData;
fukasawa e60969
fukasawa e60969
        /* and allocate memory for an array of row-pointers */
fukasawa e60969
fukasawa e60969
        if ((ppbRowPointers = (png_bytepp) malloc((*piHeight)
fukasawa e60969
                            * sizeof(png_bytep))) == NULL)
fukasawa e60969
        {
fukasawa e60969
            png_error(png_ptr, "Visual PNG: out of memory");
fukasawa e60969
        }
fukasawa e60969
fukasawa e60969
        /* set the individual row-pointers to point at the correct offsets */
fukasawa e60969
fukasawa e60969
        for (i = 0; i < (*piHeight); i++)
fukasawa e60969
            ppbRowPointers[i] = pbImageData + i * ulRowBytes;
fukasawa e60969
fukasawa e60969
        /* now we can go ahead and just read the whole image */
fukasawa e60969
fukasawa e60969
        png_read_image(png_ptr, ppbRowPointers);
fukasawa e60969
fukasawa e60969
        /* read the additional chunks in the PNG file (not really needed) */
fukasawa e60969
fukasawa e60969
        png_read_end(png_ptr, NULL);
fukasawa e60969
fukasawa e60969
        /* and we're done */
fukasawa e60969
fukasawa e60969
        free (ppbRowPointers);
fukasawa e60969
        ppbRowPointers = NULL;
fukasawa e60969
fukasawa e60969
        /* yepp, done */
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    Catch (msg)
fukasawa e60969
    {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
fukasawa e60969
        *ppbImageData = pbImageData = NULL;
fukasawa e60969
fukasawa e60969
        if(ppbRowPointers)
fukasawa e60969
            free (ppbRowPointers);
fukasawa e60969
fukasawa e60969
        fclose(pfFile);
fukasawa e60969
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    fclose (pfFile);
fukasawa e60969
fukasawa e60969
    return TRUE;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
BOOL PngSaveImage (PTSTR pstrFileName, png_byte *pDiData,
fukasawa e60969
                   int iWidth, int iHeight, png_color bkgColor)
fukasawa e60969
{
fukasawa e60969
    const int           ciBitDepth = 8;
fukasawa e60969
    const int           ciChannels = 3;
fukasawa e60969
fukasawa e60969
    static FILE        *pfFile;
fukasawa e60969
    png_uint_32         ulRowBytes;
fukasawa e60969
    static png_byte   **ppbRowPointers = NULL;
fukasawa e60969
    int                 i;
fukasawa e60969
fukasawa e60969
    /* open the PNG output file */
fukasawa e60969
fukasawa e60969
    if (!pstrFileName)
fukasawa e60969
        return FALSE;
fukasawa e60969
fukasawa e60969
    if (!(pfFile = fopen(pstrFileName, "wb")))
fukasawa e60969
        return FALSE;
fukasawa e60969
fukasawa e60969
    /* prepare the standard PNG structures */
fukasawa e60969
fukasawa e60969
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
fukasawa e60969
      (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
fukasawa e60969
    if (!png_ptr)
fukasawa e60969
    {
fukasawa e60969
        fclose(pfFile);
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    info_ptr = png_create_info_struct(png_ptr);
fukasawa e60969
    if (!info_ptr) {
fukasawa e60969
        fclose(pfFile);
fukasawa e60969
        png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    Try
fukasawa e60969
    {
fukasawa e60969
        /* initialize the png structure */
fukasawa e60969
fukasawa e60969
#ifdef PNG_STDIO_SUPPORTED
fukasawa e60969
        png_init_io(png_ptr, pfFile);
fukasawa e60969
#else
fukasawa e60969
        png_set_write_fn(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
        /* we're going to write a very simple 3x8-bit RGB image */
fukasawa e60969
fukasawa e60969
        png_set_IHDR(png_ptr, info_ptr, iWidth, iHeight, ciBitDepth,
fukasawa e60969
            PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
fukasawa e60969
            PNG_FILTER_TYPE_BASE);
fukasawa e60969
fukasawa e60969
        /* write the file header information */
fukasawa e60969
fukasawa e60969
        png_write_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
        /* swap the BGR pixels in the DiData structure to RGB */
fukasawa e60969
fukasawa e60969
        png_set_bgr(png_ptr);
fukasawa e60969
fukasawa e60969
        /* row_bytes is the width x number of channels */
fukasawa e60969
fukasawa e60969
        ulRowBytes = iWidth * ciChannels;
fukasawa e60969
fukasawa e60969
        /* we can allocate memory for an array of row-pointers */
fukasawa e60969
fukasawa e60969
        if ((ppbRowPointers = (png_bytepp) malloc(iHeight * sizeof(png_bytep))) == NULL)
fukasawa e60969
            Throw "Visualpng: Out of memory";
fukasawa e60969
fukasawa e60969
        /* set the individual row-pointers to point at the correct offsets */
fukasawa e60969
fukasawa e60969
        for (i = 0; i < iHeight; i++)
fukasawa e60969
            ppbRowPointers[i] = pDiData + i * (((ulRowBytes + 3) >> 2) << 2);
fukasawa e60969
fukasawa e60969
        /* write out the entire image data in one call */
fukasawa e60969
fukasawa e60969
        png_write_image (png_ptr, ppbRowPointers);
fukasawa e60969
fukasawa e60969
        /* write the additional chunks to the PNG file (not really needed) */
fukasawa e60969
fukasawa e60969
        png_write_end(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
        /* and we're done */
fukasawa e60969
fukasawa e60969
        free (ppbRowPointers);
fukasawa e60969
        ppbRowPointers = NULL;
fukasawa e60969
fukasawa e60969
        /* clean up after the write, and free any memory allocated */
fukasawa e60969
fukasawa e60969
        png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
fukasawa e60969
fukasawa e60969
        /* yepp, done */
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    Catch (msg)
fukasawa e60969
    {
fukasawa e60969
        png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
fukasawa e60969
fukasawa e60969
        if(ppbRowPointers)
fukasawa e60969
            free (ppbRowPointers);
fukasawa e60969
fukasawa e60969
        fclose(pfFile);
fukasawa e60969
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    fclose (pfFile);
fukasawa e60969
fukasawa e60969
    return TRUE;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifndef PNG_STDIO_SUPPORTED
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   png_size_t check;
fukasawa e60969
fukasawa e60969
   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
fukasawa e60969
    * instead of an int, which is what fread() actually returns.
fukasawa e60969
    */
fukasawa e60969
   check = (png_size_t)fread(data, (png_size_t)1, length,
fukasawa e60969
      (FILE *)png_ptr->io_ptr);
fukasawa e60969
fukasawa e60969
   if (check != length)
fukasawa e60969
   {
fukasawa e60969
      png_error(png_ptr, "Read Error");
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 check;
fukasawa e60969
fukasawa e60969
   check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
fukasawa e60969
   if (check != length)
fukasawa e60969
   {
fukasawa e60969
      png_error(png_ptr, "Write Error");
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
png_flush(png_structp png_ptr)
fukasawa e60969
{
fukasawa e60969
   FILE *io_ptr;
fukasawa e60969
   io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
fukasawa e60969
   if (io_ptr != NULL)
fukasawa e60969
      fflush(io_ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/*-----------------
fukasawa e60969
 *  end of source
fukasawa e60969
 *-----------------
fukasawa e60969
 */