Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

image_Pnm_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 
00027 #define __IMAGE_MODULE
00028 
00029 #include "Bases.h"
00030 #include "Module.h"
00031 #include "Gui.h"
00032 #include "Memory.h"
00033 #include "Common.h"
00034 #include "images/image_Pnm.h"
00035 #include "debug.h"
00036 
00037 
00038 #define PNM_ST_INVALID 0
00039 #define PNM_ST_PPM     1
00040 #define PNM_ST_PGM     2
00041 
00042 
00043 
00044 typedef struct pnm_CustomData_s
00045 {
00046   Image_t *img;
00047   int width,height;
00048   int maxval;
00049   int offset;
00050   int subtype;
00051 } pnm_CustomData_t;
00052 
00053 
00054 static bases_Modules_t *api;
00055 VERSION("Pnm.image_mod",1,0,"Gergely Gati","g.gati@freemail.hu");
00056 
00057 
00058 // ez kitolti az image struktura hianyzo mezoit
00059 // ha az input struktura format mezoje ki van toltve,
00060 // es az nem "pnm", akkor FALSE,
00061 // ha NULL akkor egy statikus "pnm"-re allitja, ha
00062 // a data egy valid png-nek latszik.
00063 // kitolti a meret mezoket is.
00064 static int pnm_ImgInit(Image_t *img, void (*update)(void *),void *userdata)
00065 {
00066   int ret=-1;
00067   pnm_CustomData_t *cdata;
00068   char *p;
00069 
00070   debug_Begin();
00071 
00072   if(img!=NULL&&img->iim.data[0]=='P')
00073   {
00074     if(NULL!=(cdata=mem_malloc(sizeof(pnm_CustomData_t))))
00075     {
00076       img->formatdata=cdata;
00077       cdata->img=img;
00078       if('6'==img->iim.data[1]) cdata->subtype=PNM_ST_PPM;
00079       else if('5'==img->iim.data[1]) cdata->subtype=PNM_ST_PGM;
00080       else cdata->subtype=PNM_ST_INVALID;
00081       p=(char *)&img->iim.data[2];
00082 //      sscanf(p," %d %d %d",&cdata->width,&cdata->height,&cdata->maxval);
00083       while(*p=='\n'||*p==' '||*p=='\t'||*p==0xa||*p==0xd) p++;
00084       cdata->width=atoi(p);
00085       while(*p>='0'&&*p<='9') p++;
00086       while(*p=='\n'||*p==' '||*p=='\t'||*p==0xa||*p==0xd) p++;
00087       cdata->height=atoi(p);
00088       while(*p>='0'&&*p<='9') p++;
00089       while(*p=='\n'||*p==' '||*p=='\t'||*p==0xa||*p==0xd) p++;
00090       cdata->maxval=atoi(p);
00091       while(*p>='0'&&*p<='9') p++;
00092       while(*p=='\n'||*p==' '||*p=='\t'||*p==0xa||*p==0xd) p++;
00093       cdata->offset=((u8 *)p)-&img->iim.data[0];
00094       img->minwidth=img->maxwidth=cdata->width;
00095       img->minheight=img->maxheight=cdata->height;
00096       debug_Message("%d %d %d (offset=%d)",cdata->width,cdata->height,cdata->maxval,cdata->offset);
00097       ret=0;
00098     }
00099   }
00100 
00101   debug_End();
00102 
00103   return(ret);
00104 }
00105 
00106 
00107 static void pnm_ImgCleanUp(Image_t *img)
00108 {
00109   pnm_CustomData_t *cdata;
00110 
00111   debug_Begin();
00112 
00113   if(img!=NULL)
00114   {
00115     cdata=(pnm_CustomData_t *)img->formatdata;
00116     if(cdata!=NULL) mem_free(cdata);
00117   }
00118 
00119   debug_End();
00120 }
00121 
00122 
00123 // a dest memoriat a hivo prezentalja destsize
00124 // meretben, ha kicsi, hiba (-1)
00125 // a rendereles 32bitesen tortenik, az atlatszo
00126 // pixelek helyere 0xffffffff kerul (formatum: AARRGGBB)
00127 static int pnm_ImgRender(Image_t *img, u8 *dest, int destsize, int width, int height)
00128 {
00129   pnm_CustomData_t *cdata;
00130   int i,size;
00131   u8 *p,*d;
00132 
00133   debug_Begin();
00134 
00135   if(img!=NULL&&img->formatdata!=NULL&&dest!=NULL&&destsize>=(width*height*4)&&width==img->minwidth&&height==img->minheight)
00136   {
00137     cdata=(pnm_CustomData_t *)img->formatdata;
00138     size=cdata->width*cdata->height;
00139     p=&img->iim.data[cdata->offset];
00140     d=dest;
00141     if(cdata->subtype==PNM_ST_PPM)
00142     {
00143       for(i=0;i<size;i++)
00144       {
00145         *d++=0x00;      // A
00146         *d++=*p++;      // R
00147         *d++=*p++;      // G
00148         *d++=*p++;      // B
00149       }
00150     }
00151     else if(cdata->subtype==PNM_ST_PGM)
00152     {
00153       for(i=0;i<size;i++)
00154       {
00155         *d++=0x00;      // A
00156         *d++=*p;        // R
00157         *d++=*p;        // G
00158         *d++=*p++;      // B
00159       }
00160     }
00161     else debug_Error("invalid subtype");
00162   }
00163 
00164   debug_End();
00165 
00166   return(0);
00167 }
00168 
00169 
00170 static int pnm_PutImage(Image_t *img, u32 window, Rect_t *rect)
00171 {
00172   Vector_t buff;
00173 
00174   debug_Begin();
00175 
00176   buff.size=(rect->width*rect->height*4)+1;
00177   if(NULL!=(buff.data=mem_malloc(buff.size)))
00178   {
00179     if(0==(pnm_ImgRender(img,buff.data,buff.size,rect->width,rect->height)))
00180     {
00181       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);
00182     }
00183     mem_free(buff.data);
00184   }
00185 
00186   debug_End();
00187 
00188   return(0);
00189 }
00190 
00191 
00192 static void pnm_CleanUp(void)
00193 {
00194   debug_Begin();
00195   debug_End();
00196 }
00197 
00198 
00199 static int pnm_Init(u32 module)
00200 {
00201   int ret=-1;
00202 
00203   debug_Begin();
00204 
00205   ret=api->img_Call(IMG_REGISTERFORMATCODE,
00206                 TAG_IMG_NAME,(u32)"pnm",
00207                 TAG_IMG_IMG_INIT,(u32)pnm_ImgInit,
00208                 TAG_IMG_IMG_RENDER,(u32)pnm_ImgRender,
00209                 TAG_IMG_IMG_PUTIMAGE,(u32)pnm_PutImage,
00210                 TAG_IMG_IMG_CLEAN,(u32)pnm_ImgCleanUp,
00211                 TAG_IMG_CLEANUP,(u32)pnm_CleanUp,
00212                 TAG_IMG_MODULE,module,
00213                 TAG_DONE);
00214 
00215   debug_End();
00216 
00217   return(ret);
00218 }
00219 
00220 EXPORT int module_Init(u32 module, bases_Modules_t *bases)
00221 {
00222     api=bases;
00223     return(pnm_Init(module));
00224 }

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