Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

image_Png_mod.c

Go to the documentation of this file.
00001 /*
00002  *    gui - [gega user interface] the flexible solution for user interface problems
00003  *    Copyright (C) 2002  Gergely Gati
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; version 2 of the License.
00008  *
00009  *    This program is distributed in the hope that it will be useful,
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *    GNU General Public License for more details.
00013  *
00014  *    You should have received a copy of the GNU General Public License
00015  *    along with this program; if not, write to the Free Software
00016  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *    Gergely Gati
00019  *      email:           g.gati@freemail.hu
00020  *      AIM screenname:  GatiGergely
00021  *      ICQ number:      93131690
00022  *
00023  */
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <png.h>
00027 
00028 #define __IMAGE_MODULE
00029 
00030 #include "Bases.h"
00031 #include "Module.h"
00032 #include "Gui.h"
00033 #include "Memory.h"
00034 #include "Common.h"
00035 #include "images/image_Png.h"
00036 #include "debug.h"
00037 
00038 
00039 typedef struct png_CustomData_s
00040 {
00041   Image_t *img;
00042   int fp;
00043   png_structp pngptr;
00044   png_infop info;
00045 } png_CustomData_t;
00046 
00047 
00048 static bases_Modules_t *api;
00049 VERSION("Png.image_mod",1,0,"Gergely Gati","g.gati@freemail.hu");
00050 
00051 
00052 static void my_read(png_structp png_ptr, png_bytep data, png_size_t length)
00053 {
00054   png_CustomData_t *cusdat;
00055 
00056   debug_Begin();
00057 
00058   cusdat=(png_CustomData_t *)png_get_io_ptr(png_ptr);
00059   memcpy(data,(cusdat->img->iim.data+cusdat->fp),length);
00060   cusdat->fp+=length;
00061 
00062   debug_End();
00063 }
00064 
00065 
00066 
00067 // ez kitolti az image struktura hianyzo mezoit
00068 // ha az input struktura format mezoje ki van toltve,
00069 // es az nem "png", akkor FALSE,
00070 // ha NULL akkor egy statikus "png"-re allitja, ha
00071 // a data egy valid png-nek latszik.
00072 // kitolti a meret mezoket is.
00073 static int png_ImgInit(Image_t *img, void (*update)(void *),void *userdata)
00074 {
00075   int ret=-1;
00076   int is_png;
00077   png_infop info;
00078   png_structp p;
00079   png_CustomData_t *cdata;
00080   int color_type;
00081 
00082   debug_Begin();
00083 
00084   if(img!=NULL)
00085   {
00086     is_png=!png_sig_cmp(img->iim.data,(png_size_t)0,8);
00087     if(is_png)
00088     {
00089       if(NULL!=(cdata=mem_malloc(sizeof(png_CustomData_t))))
00090       {
00091         img->formatdata=cdata;
00092         cdata->img=img;
00093         cdata->fp=8;
00094         if(NULL!=(cdata->pngptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL)))
00095         {
00096           p=cdata->pngptr;
00097           png_set_read_fn(p,cdata,my_read);
00098           png_set_sig_bytes(p,8);
00099           if(NULL!=(info=png_create_info_struct(p)))
00100           {
00101             cdata->info=info;
00102             png_read_info(p,info);
00103             img->minwidth=img->maxwidth=png_get_image_width(p,info);
00104             img->minheight=img->maxheight=png_get_image_height(p,info);
00105             color_type=png_get_color_type(p,info);
00106             if(color_type==PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(p);
00107             if(png_get_valid(p,info,PNG_INFO_tRNS)) png_set_tRNS_to_alpha(p);
00108             png_set_filler(p,0,PNG_FILLER_BEFORE);
00109             png_set_gamma(p,2.2,0.45455);
00110             png_set_swap_alpha(p);
00111             png_read_update_info(p,info);
00112             png_destroy_info_struct(p,&cdata->info);
00113             png_destroy_read_struct(&p,png_infopp_NULL,png_infopp_NULL);
00114           }
00115           ret=0;
00116         }
00117       }
00118     }
00119   }
00120 
00121   debug_End();
00122 
00123   return(ret);
00124 }
00125 
00126 
00127 static void png_ImgCleanUp(Image_t *img)
00128 {
00129   png_structp p;
00130   png_CustomData_t *cdata;
00131 
00132   debug_Begin();
00133 
00134   if(img!=NULL)
00135   {
00136     cdata=(png_CustomData_t *)img->formatdata;
00137     p=cdata->pngptr;
00138     if(img->formatdata!=NULL) mem_free(img->formatdata);
00139   }
00140 
00141   debug_End();
00142 }
00143 
00144 
00145 // a dest memoriat a hivo prezentalja destsize
00146 // meretben, ha kicsi, hiba (-1)
00147 // a rendereles 32bitesen tortenik, az atlatszo
00148 // pixelek helyere 0xffffffff kerul
00149 static int png_ImgRender(Image_t *img, u8 *dest, int destsize, int width, int height)
00150 {
00151   png_infop info;
00152   png_bytep *row_pointers;
00153   png_CustomData_t *cdata;
00154   png_structp p;
00155   int color_type;
00156   int i;
00157 
00158   debug_Begin();
00159 
00160   if(img!=NULL&&img->formatdata!=NULL&&dest!=NULL&&destsize>=(width*height*4)&&width==img->minwidth&&height==img->minheight)
00161   {
00162     cdata=(png_CustomData_t *)img->formatdata;
00163     if(NULL!=(cdata->pngptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL)))
00164     {
00165       p=cdata->pngptr;
00166       if(NULL!=(info=png_create_info_struct(p)))
00167       {
00168         cdata->info=info;
00169         cdata->fp=8;
00170         png_set_read_fn(p,cdata,my_read);
00171         png_set_sig_bytes(p,8);
00172         png_read_info(p,info);
00173         color_type=png_get_color_type(p,info);
00174         if(color_type==PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(p);
00175         if(png_get_valid(p,info,PNG_INFO_tRNS)) png_set_tRNS_to_alpha(p);
00176         png_set_filler(p,0,PNG_FILLER_BEFORE);
00177         png_set_gamma(p,2.2,0.45455);
00178         png_set_swap_alpha(p);
00179         png_read_update_info(p,info);
00180         row_pointers=png_malloc(p,height*sizeof(png_bytep));
00181         for(i=0;i<4*width*height;i++) dest[i]=0;
00182         for(i=0;i<height;i++) row_pointers[i]=&dest[width*(i*4)];
00183         png_read_image(p,row_pointers);
00184         png_read_end(p,info);
00185         png_free(p,row_pointers);
00186         png_destroy_info_struct(p,&cdata->info);
00187         png_destroy_read_struct(&p,png_infopp_NULL,png_infopp_NULL);
00188       }
00189     }
00190   }
00191 
00192   debug_End();
00193 
00194   return(0);
00195 }
00196 
00197 
00198 static int png_PutImage(Image_t *img, u32 window, Rect_t *rect)
00199 {
00200   Vector_t buff;
00201 
00202   debug_Begin();
00203 
00204   buff.size=(rect->width*rect->height*4)+1;
00205   if(NULL!=(buff.data=mem_malloc(buff.size)))
00206   {
00207     if(0==(png_ImgRender(img,buff.data,buff.size,rect->width,rect->height)))
00208     {
00209       api->glw_Call(GLW_PUTIMAGE,TAG_GLW_WINDOW,window,TAG_GLW_PIXELS,&buff,TAG_GLW_CLIPRECT,rect,TAG_GLW_LEFT,rect->left,TAG_GLW_TOP,rect->top,TAG_GLW_WIDTH,rect->width,TAG_GLW_HEIGHT,rect->height,TAG_DONE);
00210     }
00211     mem_free(buff.data);
00212   }
00213 
00214   debug_End();
00215 
00216   return(0);
00217 }
00218 
00219 
00220 static void png_CleanUp(void)
00221 {
00222   debug_Begin();
00223   debug_End();
00224 }
00225 
00226 
00227 static int png_Init(u32 module)
00228 {
00229   int ret=-1;
00230 
00231   debug_Begin();
00232 
00233   ret=api->img_Call(IMG_REGISTERFORMATCODE,
00234                 TAG_IMG_NAME,(u32)"png",
00235                 TAG_IMG_IMG_INIT,(u32)png_ImgInit,
00236                 TAG_IMG_IMG_RENDER,(u32)png_ImgRender,
00237                 TAG_IMG_IMG_PUTIMAGE,(u32)png_PutImage,
00238                 TAG_IMG_IMG_CLEAN,(u32)png_ImgCleanUp,
00239                 TAG_IMG_CLEANUP,(u32)png_CleanUp,
00240                 TAG_IMG_MODULE,module,
00241                 TAG_DONE);
00242 
00243   debug_End();
00244 
00245   return(ret);
00246 }
00247 
00248 EXPORT int module_Init(u32 module, bases_Modules_t *bases)
00249 {
00250     api=bases;
00251     return(png_Init(module));
00252 }

Generated on Tue Jan 7 12:11:22 2003 for THEGUI by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002