Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / src / modules / mod_png / mptr_png.cpp
index 4bfc6de..ffdc153 100644 (file)
@@ -1,18 +1,23 @@
-/*! ========================================================================
-** Sinfg
-** ppm Target Module
-** $Id: mptr_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
+/* === S Y N F I G ========================================================= */
+/*!    \file mptr_png.cpp
+**     \brief ppm Target Module
 **
-** Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     $Id$
 **
-** This software and associated documentation
-** are CONFIDENTIAL and PROPRIETARY property of
-** the above-mentioned copyright holder.
+**     \legal
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007 Chris Moore
 **
-** You may not copy, print, publish, or in any
-** other way distribute this software without
-** a prior written agreement with
-** the copyright holder.
+**     This package is free software; you can redistribute it and/or
+**     modify it under the terms of the GNU General Public License as
+**     published by the Free Software Foundation; either version 2 of
+**     the License, or (at your option) any later version.
+**
+**     This package is distributed in the hope that it will be useful,
+**     but WITHOUT ANY WARRANTY; without even the implied warranty of
+**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+**     General Public License for more details.
+**     \endlegal
 **
 ** === N O T E S ===========================================================
 **
@@ -34,9 +39,9 @@
 #endif
 
 #include "mptr_png.h"
-#include <sinfg/importer.h>
-#include <sinfg/time.h>
-#include <sinfg/general.h>
+#include <synfig/importer.h>
+#include <synfig/time.h>
+#include <synfig/general.h>
 
 
 #include <cstdio>
@@ -46,7 +51,7 @@
 
 /* === M A C R O S ========================================================= */
 
-using namespace sinfg;
+using namespace synfig;
 using namespace std;
 using namespace etl;
 
@@ -54,32 +59,32 @@ using namespace etl;
 
 /* === G L O B A L S ======================================================= */
 
-SINFG_IMPORTER_INIT(png_mptr);
-SINFG_IMPORTER_SET_NAME(png_mptr,"png_mptr");
-SINFG_IMPORTER_SET_EXT(png_mptr,"png");
-SINFG_IMPORTER_SET_VERSION(png_mptr,"0.1");
-SINFG_IMPORTER_SET_CVS_ID(png_mptr,"$Id: mptr_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
+SYNFIG_IMPORTER_INIT(png_mptr);
+SYNFIG_IMPORTER_SET_NAME(png_mptr,"png");
+SYNFIG_IMPORTER_SET_EXT(png_mptr,"png");
+SYNFIG_IMPORTER_SET_VERSION(png_mptr,"0.1");
+SYNFIG_IMPORTER_SET_CVS_ID(png_mptr,"$Id$");
 
 /* === M E T H O D S ======================================================= */
 
 void
-png_mptr::png_out_error(png_struct *png_data,const char *msg)
+png_mptr::png_out_error(png_struct */*png_data*/,const char *msg)
 {
        //png_mptr *me=(png_mptr*)png_data->error_ptr;
-       sinfg::error(strprintf("png_mptr: error: %s",msg));
+       synfig::error(strprintf("png_mptr: error: %s",msg));
        //me->ready=false;
 }
 
 void
-png_mptr::png_out_warning(png_struct *png_data,const char *msg)
+png_mptr::png_out_warning(png_struct */*png_data*/,const char *msg)
 {
        //png_mptr *me=(png_mptr*)png_data->error_ptr;
-       sinfg::warning(strprintf("png_mptr: warning: %s",msg));
+       synfig::warning(strprintf("png_mptr: warning: %s",msg));
        //me->ready=false;
 }
 
 int
-png_mptr::read_chunk_callback(png_struct *png_data, png_unknown_chunkp chunk)
+png_mptr::read_chunk_callback(png_struct */*png_data*/, png_unknown_chunkp /*chunk*/)
 {
        /* The unknown chunk structure contains your
          chunk data: */
@@ -88,10 +93,10 @@ png_mptr::read_chunk_callback(png_struct *png_data, png_unknown_chunkp chunk)
        //png_size_t size;
        /* Note that libpng has already taken care of
          the CRC handling */
-       
+
        /* put your code here.  Return one of the
          following: */
-       
+
        //return (-n); /* chunk had an error */
        return (0); /* did not recognize */
        //return (n); /* success */
@@ -100,7 +105,7 @@ png_mptr::read_chunk_callback(png_struct *png_data, png_unknown_chunkp chunk)
 png_mptr::png_mptr(const char *file_name)
 {
        filename=file_name;
-       
+
        /* Open the file pointer */
     FILE *file = fopen(file_name, "rb");
     if (!file)
@@ -109,8 +114,8 @@ png_mptr::png_mptr(const char *file_name)
                throw strprintf("Unable to physically open %s",file_name);
                return;
     }
-    
-       
+
+
        /* Make sure we are dealing with a PNG format file */
        png_byte header[PNG_CHECK_BYTES];
        fread(header, 1, PNG_CHECK_BYTES, file);
@@ -121,8 +126,8 @@ png_mptr::png_mptr(const char *file_name)
                throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
                return;
     }
-       
-       
+
+
        png_structp png_ptr = png_create_read_struct
        (PNG_LIBPNG_VER_STRING, (png_voidp)this,
         &png_mptr::png_out_error, &png_mptr::png_out_warning);
@@ -153,23 +158,38 @@ png_mptr::png_mptr(const char *file_name)
                return;
     }
 
-       
-       
+
+
        png_init_io(png_ptr, file);
        png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);
 
+       png_read_info(png_ptr, info_ptr);
+
+       int bit_depth,color_type,interlace_type, compression_type,filter_method;
+       png_uint_32 width,height;
+
+       png_get_IHDR(png_ptr, info_ptr, &width, &height,
+                                &bit_depth, &color_type, &interlace_type,
+                                &compression_type, &filter_method);
+
+       if (bit_depth == 16)
+               png_set_strip_16(png_ptr);
+
+       if (bit_depth < 8)
+               png_set_packing(png_ptr);
+
        double fgamma;
        if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
        {
-               sinfg::info("PNG: Image gamma is %f",fgamma);
+               synfig::info("PNG: Image gamma is %f",fgamma);
                png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
        }
-       
-       
+
+
        /*
        if (setjmp(png_jmpbuf(png_ptr)))
        {
-               sinfg::error("Unable to setup longjump");
+               synfig::error("Unable to setup longjump");
                png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
                fclose(file);
         //! \todo THROW SOMETHING
@@ -177,31 +197,34 @@ png_mptr::png_mptr(const char *file_name)
                return;
        }
        */
-       
+
        png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
-       
-       
-       png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
 
-       int bit_depth,color_type,interlace_type, compression_type,filter_method;
-       png_uint_32 width,height;
-       
-    png_get_IHDR(png_ptr, info_ptr, &width, &height,
-       &bit_depth, &color_type, &interlace_type,
-       &compression_type, &filter_method);
-       
+       // man libpng tells me:
+       //   You must use png_transforms and not call any
+       //   png_set_transform() functions when you use png_read_png().
+       // but we used png_set_gamma(), which may be why we were seeing a crash at the end
+       //   png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
+
+       png_read_update_info(png_ptr, info_ptr);
+       png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
+
+       // allocate buffer to read image data into
        png_bytep *row_pointers=new png_bytep[height];
-       row_pointers = png_get_rows(png_ptr, info_ptr);
-       int x;
-       int y;
+       png_byte *data = new png_byte[rowbytes*height];
+       for (png_uint_32 i = 0; i < height; i++)
+               row_pointers[i] = &(data[rowbytes*i]);
+
+       png_read_image(png_ptr, row_pointers);
+
+       png_uint_32 x, y;
        surface_buffer.set_wh(width,height);
 
        switch(color_type)
        {
        case PNG_COLOR_TYPE_RGB:
-               DEBUGPOINT();
-               for(y=0;y<surface_buffer.get_h();y++)
-                       for(x=0;x<surface_buffer.get_w();x++)
+               for(y=0;y<height;y++)
+                       for(x=0;x<width;x++)
                        {
                                float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
                                float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
@@ -222,11 +245,10 @@ png_mptr::png_mptr(const char *file_name)
 */
                        }
                break;
-                       
+
        case PNG_COLOR_TYPE_RGB_ALPHA:
-               DEBUGPOINT();
-               for(y=0;y<surface_buffer.get_h();y++)
-                       for(x=0;x<surface_buffer.get_w();x++)
+               for(y=0;y<height;y++)
+                       for(x=0;x<width;x++)
                        {
                                float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
                                float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
@@ -247,10 +269,10 @@ png_mptr::png_mptr(const char *file_name)
                                */
                        }
                break;
-                       
+
        case PNG_COLOR_TYPE_GRAY:
-               for(y=0;y<surface_buffer.get_h();y++)
-                       for(x=0;x<surface_buffer.get_w();x++)
+               for(y=0;y<height;y++)
+                       for(x=0;x<width;x++)
                        {
                                float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
                                //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
@@ -264,8 +286,8 @@ png_mptr::png_mptr(const char *file_name)
                break;
 
        case PNG_COLOR_TYPE_GRAY_ALPHA:
-               for(y=0;y<surface_buffer.get_h();y++)
-                       for(x=0;x<surface_buffer.get_w();x++)
+               for(y=0;y<height;y++)
+                       for(x=0;x<width;x++)
                        {
                                float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
 //                             float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
@@ -279,52 +301,131 @@ png_mptr::png_mptr(const char *file_name)
                break;
 
        case PNG_COLOR_TYPE_PALETTE:
-               sinfg::warning("png_mptr: Paletted PNGs aren't yet fully supported.");
-               for(y=0;y<surface_buffer.get_h();y++)
-                       for(x=0;x<surface_buffer.get_w();x++)
+               for(y=0;y<height;y++)
+                       for(x=0;x<width;x++)
                        {
                                float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
                                float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
                                float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
+                               float a=1.0;
+                               if(info_ptr->valid & PNG_INFO_tRNS)
+                                   a = (float)(unsigned char)png_ptr->trans[row_pointers[y][x]]*(1.0/255.0);
                                surface_buffer[y][x]=Color(
                                        r,
                                        g,
                                        b,
-                                       1.0
+                                       a
                                );
                        }
                break;
        default:
-               sinfg::error("png_mptr: error: Unsupported color type");
+               synfig::error("png_mptr: error: Unsupported color type");
         //! \todo THROW SOMETHING
                throw String("error on importer construction, *WRITEME*6");
                return;
        }
 
-       DEBUGPOINT();
-       
-       // \fixme These shouldn't be uncommented, but for some
-       // reason, they crash the program. I will have to look into this
-       // later. This is a memory leak, but it shouldn't be too bad.
-       
-       /*
        png_read_end(png_ptr, end_info);
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        fclose(file);
-       */
 
        delete [] row_pointers;
+       delete [] data;
+
+       trim = false;
+
+       if (getenv("SYNFIG_DISABLE_CROP_IMPORTED_IMAGES"))
+               return;
+
+       switch(color_type)
+       {
+       case PNG_COLOR_TYPE_RGB_ALPHA:
+       case PNG_COLOR_TYPE_GRAY_ALPHA:
+       case PNG_COLOR_TYPE_PALETTE:
+               for(y=0;y<height;y++)
+               {
+                       for(x=0;x<width;x++)
+                               if (surface_buffer[y][x].get_a()) break;
+                       if (x != width) break;
+               }
+
+               if (y != height)
+               {
+#define BORDER 1                               // don't chop off all the transparent space - leave a border this many pixels wide for the interpolation
+
+                       png_uint_32 min_x, min_y, max_x, max_y;
+                       if (y>BORDER) min_y = y-BORDER; else min_y = 0;
+
+                       for(y=height-1;y>0;y--)
+                       {
+                               for(x=0;x<width;x++)
+                                       if (surface_buffer[y][x].get_a()) break;
+                               if (x != width) break;
+                       }
+                       max_y = std::min(y+BORDER,height-1);
+
+                       for(x=0;x<width;x++)
+                       {
+                               for(y=0;y<height;y++)
+                                       if (surface_buffer[y][x].get_a()) break;
+                               if (y != height) break;
+                       }
+                       if (x>BORDER) min_x = x-BORDER; else min_x = 0;
+
+                       for(x=width-1;x>0;x--)
+                       {
+                               for(y=0;y<height;y++)
+                                       if (surface_buffer[y][x].get_a()) break;
+                               if (y != height) break;
+                       }
+                       max_x = std::min(x+BORDER,width-1);
+
+                       if (min_x != 0 || max_x != width-1 ||
+                               min_y != 0 || max_y != height-1)
+                       {
+                               trim = true;
+                               orig_width = width;
+                               orig_height = height;
+                               trimmed_x = min_x;
+                               trimmed_y = min_y;
+
+                               width=max_x-min_x+1;
+                               height=max_y-min_y+1;
+                               synfig::Surface tmp_buffer;
+                               tmp_buffer.set_wh(width,height);
+                               for(y=0;y<height;y++)
+                                       for(x=0;x<width;x++)
+                                               tmp_buffer[y][x] = surface_buffer[y+min_y][x+min_x];
+                               surface_buffer = tmp_buffer;
+                       }
+               }
+       }
 }
 
 png_mptr::~png_mptr()
 {
-       DEBUGPOINT();
 }
 
 bool
-png_mptr::get_frame(sinfg::Surface &surface,Time, sinfg::ProgressCallback *cb)
+png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback */*cb*/)
 {
-       surface.mirror(surface_buffer);
-//     surface=surface_buffer;
+       assert(0);                                      // shouldn't be called?
+       surface=surface_buffer;
+       return true;
+}
+
+bool
+png_mptr::get_frame(synfig::Surface &surface,Time,
+                                       bool &trimmed, unsigned int &width, unsigned int &height, unsigned int &top, unsigned int &left,
+                                       synfig::ProgressCallback */*cb*/)
+{
+       surface=surface_buffer;
+       if ((trimmed = trim))
+       {
+               width = orig_width;
+               height = orig_height;
+               top = trimmed_y;
+               left = trimmed_x;
+       }
        return true;
 }