Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages   Examples  

imlib_io.cpp

00001 /* Copyright (c) 2001 C. Grigorescu */
00002 
00003 /*********************************************************
00004  File: imlib_io.cpp                                                      
00005 
00006     Contains functions for reading, writing and
00007  displaying image files; it uses the "imlib" library 
00008  (in principle, all formats available for "imlib" 
00009  should work).
00010 
00011  The image buffer is allocated by the "read_image" function,
00012  returned as a pointer to void named "image"; it can be 
00013  later accessed as a linear array of type "type".
00014 
00015  Currently, the implemetation supports only the following
00016  types: 
00017 
00018   - BYTE_2D : "image" is a linear array of "unsigned char"
00019   - SHORT_2D : "image" is a linear array of "short int"
00020   - INT_2D : "image" is a linear array of "int"
00021   - FLOAT_2D : "image" is a linear array of type "float"
00022   - RGB_2D : "image" is a linear array of type "unsigned char" 
00023    containing red, green and blue values (RGB array) organized 
00024    as "RGBRGBRGB..."  
00025 
00026  ********************************************************/
00027 
00028 #include <Imlib.h>
00029 #include <X11/Xlib.h>
00030 #include <X11/Xutil.h>
00031 #include <X11/extensions/shape.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include "imlib_io.h"
00036 #include "Exceptions.h"
00037 
00038 #include <signal.h>
00039  
00040 #define ESC   0x09
00041 #define ENTER 0x24
00042 
00043 template byte  *read_image< byte >(char *filename, int *width, int *height);
00044 template short *read_image< short >(char *filename, int *width, int *height);
00045 template int   *read_image< int >(char *filename, int *width, int *height);
00046 template float *read_image< float >(char *filename, int *width, int *height);
00047 template double *read_image< double >(char *filename, int *width, int *height);
00048 template int write_image< byte >(char *filename, int width, int height, byte *image);
00049 template int write_image< short >(char *filename, int width, int height, short *image);
00050 template int write_image< int >(char *filename, int width, int height, int *image);
00051 template int write_image< float >(char *filename, int width, int height, float *image);
00052 template int write_image< double >(char *filename, int width, int height, double *image);
00053 template int show_image< byte >(byte *image, int width, int height, char *name);
00054 template int show_image< short >(short *image, int width, int height, char *name);
00055 template int show_image< int >(int *image, int width, int height, char *name);
00056 template int show_image< float >(float *image, int width, int height, char *name);
00057 template int show_image< double >(double *image, int width, int height, char *name);
00058 
00059 int read_size(char *filename, int *width, int *height)
00060 {
00061   ImlibData *id;
00062   ImlibImage *im;
00063   Display *disp;
00064  
00065   disp = XOpenDisplay(NULL);
00066   if (disp == NULL) 
00067     {
00068     printf("\n Cannot open display; is the $DISPLAY set? \n");
00069     return(0);
00070     }
00071   id         = Imlib_init(disp);
00072   im         = Imlib_load_image(id, filename);
00073   if (im == NULL) 
00074     return(0);
00075   *width     = im->rgb_width;
00076   *height    = im->rgb_height;
00077   Imlib_kill_image(id,im);
00078   XCloseDisplay(disp);
00079   return(1);
00080 }
00081 
00082 template <class T> T *read_image(char *filename, int *width, int *height)
00083 {
00084   ImlibData *id;
00085   ImlibImage *im;
00086   Display *disp;
00087   int i;
00088   T *image = NULL;
00089 
00090   disp = XOpenDisplay(NULL);
00091   if (disp == NULL) 
00092   {
00093     printf("\n Cannot open display; is the $DISPLAY set? \n");
00094     return(0);
00095   }
00096   id         = Imlib_init(disp);
00097   im         = Imlib_load_image(id, filename);
00098   *width     = im->rgb_width;
00099   *height    = im->rgb_height;
00100 
00101   image = (T *) malloc((*width)*(*height)*sizeof(T));
00102   if (image == NULL) 
00103   {
00104     printf("\nCannot allocate memory for image buffer");
00105     return(NULL);
00106   }
00107   for(i=0;i<(*width)*(*height);i++)
00108     image[i] = (T) ((11*im->rgb_data[3*i] + 
00109                      16*im->rgb_data[3*i+1] + 
00110                       5*im->rgb_data[3*i+2])/32);
00111 
00112   Imlib_kill_image(id,im);
00113   XCloseDisplay(disp);
00114 
00115   return(image);
00116 }
00117 
00118 unsigned char *read_RGB_image(char *filename, int *width, int *height)
00119 {
00120   ImlibData *id;
00121   ImlibImage *im;
00122   Display *disp;
00123   int i;
00124   unsigned char *rgb_image = NULL;
00125 
00126   disp = XOpenDisplay(NULL);
00127   if (disp == NULL) 
00128     {
00129     printf("\n Cannot open display; is the $DISPLAY set? \n");
00130     return(0);
00131     }
00132   id         = Imlib_init(disp);
00133   im         = Imlib_load_image(id, filename);
00134   *width     = im->rgb_width;
00135   *height    = im->rgb_height;
00136 
00137   rgb_image = (unsigned char *) malloc(3*(*width)*(*height)*sizeof(unsigned char));
00138   if (rgb_image == NULL) 
00139   {
00140     printf("\nCannot allocate memory for image buffer");
00141     return(NULL);
00142   }
00143   for(i=0;i<(*width)*(*height);i++) 
00144   {
00145     rgb_image[3*i]   = im->rgb_data[3*i];
00146     rgb_image[3*i+1] = im->rgb_data[3*i+1];
00147     rgb_image[3*i+2] = im->rgb_data[3*i+2];
00148   }
00149 
00150   Imlib_kill_image(id,im);
00151   XCloseDisplay(disp);
00152 
00153   return(rgb_image);
00154 }
00155 
00156 template <class T> int write_image(char *filename, int width, int height, T *image)
00157 {
00158   ImlibData *id;
00159   ImlibImage *im;
00160   Display *disp;
00161   ImlibSaveInfo saveinfo;
00162   int i, result;
00163   T max, min;
00164   unsigned char *buf;
00165 
00166   disp = XOpenDisplay(NULL);
00167   if (disp == NULL) 
00168   {
00169     printf("\n Cannot open display; is the $DISPLAY set? \n");
00170     return(0);
00171   }
00172   id = Imlib_init(disp);
00173   if (id == NULL) return(0);
00174 
00175   max = image[0]; 
00176   min = image[0];
00177   for (i=0;i<width*height;i++) 
00178   {
00179     if(max < image[i]) max = image[i];
00180     if(min > image[i]) min = image[i];
00181   }
00182 
00183   buf = (unsigned char *) malloc(width*height*3*sizeof(unsigned char));
00184   if (buf == NULL) return(0);
00185 
00186   for(i=0;i<width*height;i++) 
00187   {
00188     buf[3*i]   = (unsigned char) (255*(image[i]-min)/(max-min));
00189     buf[3*i+1] = (unsigned char) (255*(image[i]-min)/(max-min));
00190     buf[3*i+2] = (unsigned char) (255*(image[i]-min)/(max-min));
00191   }
00192   saveinfo.color = 0;
00193 
00194   im = Imlib_create_image_from_data(id, buf, NULL, width, height);
00195   result = Imlib_save_image(id, im, filename, &saveinfo);
00196   Imlib_kill_image(id,im);
00197   XCloseDisplay(disp);
00198   free(buf);
00199   if (result == 0)
00200     return(0);
00201   else
00202     return(1);
00203 }
00204 
00205 
00206 int write_RGB_image(char *filename, int width, int height, unsigned char *image)
00207 {
00208   ImlibData *id;
00209   ImlibImage *im;
00210   Display *disp;
00211   ImlibSaveInfo saveinfo;
00212   int result;
00213 
00214   disp = XOpenDisplay(NULL);
00215   if (disp == NULL) 
00216   {
00217     printf("\n Cannot open display; is the $DISPLAY set? \n");
00218     return(0);
00219   }
00220   id = Imlib_init(disp);
00221   if (id == NULL) return(0);
00222 
00223   im = Imlib_create_image_from_data(id, image, NULL, width, height);
00224   result = Imlib_save_image(id, im, filename, &saveinfo);
00225   Imlib_kill_image(id,im);
00226   XCloseDisplay(disp);
00227   if (result == 0)
00228     return(0);
00229   else
00230     return(1);
00231 }
00232 
00233 Display *disp;
00234 ImlibData *id;  
00235 ImlibImage *im;
00236 unsigned char *buf;
00237 
00238 void handler(int i) {
00239     free(buf);
00240     Imlib_kill_image(id,im);
00241     XCloseDisplay(disp);
00242     exit(0);    
00243 }
00244 
00245 template <class T> int show_image(T *image, int width, int height, char *name)
00246 {
00247   // Display *disp;
00248   //ImlibData *id;
00249   XSetWindowAttributes attr;
00250   Window win;
00251   //ImlibImage *im;
00252   Pixmap p,m;
00253   XEvent ev;
00254 
00255   int i, pid;
00256   //  unsigned char *buf;
00257 
00258   T min, max;
00259   int w,h;
00260   int flag = 1;
00261  
00262   disp = XOpenDisplay(NULL);
00263   if (disp == NULL) 
00264   {
00265     printf("\n Cannot open display; is the $DISPLAY set? \n");
00266     return(0);
00267   }
00268   id=Imlib_init(disp);
00269 
00270   max = image[0]; 
00271   min = image[0];
00272   for (i=0;i<width*height;i++) 
00273   {
00274     if(max < image[i]) max = image[i];
00275     if(min > image[i]) min = image[i];
00276   }
00277 
00278   buf = (unsigned char *) malloc(width*height*3*sizeof(unsigned char));
00279   if (buf == NULL)
00280   {
00281     printf("\n Cannot allocate memory! \n");
00282     return(0);
00283   }
00284 
00285   for(i=0;i<width*height;i++) 
00286   {
00287     buf[3*i]   = (unsigned char) ((max-min != 0) ? (255*(image[i]-min)/(max-min)) : 0);
00288     buf[3*i+1] = (unsigned char) ((max-min != 0) ? (255*(image[i]-min)/(max-min)) : 0);
00289     buf[3*i+2] = (unsigned char) ((max-min != 0) ? (255*(image[i]-min)/(max-min)) : 0);
00290   }    
00291 
00292   im = Imlib_create_image_from_data(id, buf, NULL, width, height);
00293   win= XCreateWindow(disp,DefaultRootWindow(disp),0,0,width,height,
00294                     0,id->x.depth, InputOutput,id->x.visual,0,&attr);
00295   XStoreName(disp, win, name);    
00296   XSelectInput(disp,win,StructureNotifyMask|KeyPressMask);
00297   Imlib_render(id,im,width,height);
00298   p=Imlib_move_image(id,im);
00299   m=Imlib_move_mask(id,im);
00300   XSetWindowBackgroundPixmap(disp,win,p);
00301   if (m) XShapeCombineMask(disp,win,ShapeBounding,0,0,m,ShapeSet);
00302   XMapWindow(disp,win);
00303   XSync(disp,False);
00304 
00305   if(!(pid=fork())) {
00306     signal(SIGUSR1, handler);
00307     while (flag) {
00308       XNextEvent(disp,&ev);
00309       if (ev.type==ConfigureNotify) {
00310           w=ev.xconfigure.width;h=ev.xconfigure.height;
00311           Imlib_render(id,im,w,h);
00312           Imlib_free_pixmap(id,p);
00313           p=Imlib_move_image(id,im);
00314           m=Imlib_move_mask(id,im);
00315           XSetWindowBackgroundPixmap(disp,win,p);
00316           if (m) XShapeCombineMask(disp,win,ShapeBounding,0,0,m,ShapeSet);
00317           XClearWindow(disp,win);
00318           XSync(disp,False);
00319         }
00320       if (ev.type==KeyPress) {
00321         if ((ev.xkey.keycode == ESC) || (ev.xkey.keycode == ENTER)) {
00322           flag = 0;
00323         }
00324       }
00325     }
00326     
00327     free(buf);
00328     Imlib_kill_image(id,im);
00329     XCloseDisplay(disp);
00330     //return 0;
00331     exit(0);    
00332   }
00333 
00334   return pid;
00335 }
00336 
00337 int show_RGB_image(unsigned char *image, int width, int height, char *name)
00338 {
00339   Display *disp;
00340   ImlibData *id;
00341   XSetWindowAttributes attr;
00342   Window win;
00343   ImlibImage *im;
00344   Pixmap p,m;
00345   XEvent ev;
00346 
00347   int pid;
00348   int w,h;
00349   int flag = 1;
00350  
00351   disp = XOpenDisplay(NULL);
00352   if (disp == NULL) 
00353   {
00354     printf("\n Cannot open display; is the $DISPLAY set? \n");
00355     return(0);
00356   }
00357   id=Imlib_init(disp);
00358 
00359   im = Imlib_create_image_from_data(id, image, NULL, width, height);
00360   win= XCreateWindow(disp,DefaultRootWindow(disp),0,0,width,height,
00361                     0,id->x.depth, InputOutput,id->x.visual,0,&attr);
00362   XStoreName(disp, win, name);    
00363   XSelectInput(disp,win,StructureNotifyMask|KeyPressMask);
00364   Imlib_render(id,im,width,height);
00365   p=Imlib_move_image(id,im);
00366   m=Imlib_move_mask(id,im);
00367   XSetWindowBackgroundPixmap(disp,win,p);
00368   if (m) XShapeCombineMask(disp,win,ShapeBounding,0,0,m,ShapeSet);
00369   XMapWindow(disp,win);
00370   XSync(disp,False);
00371 
00372   if(!(pid=fork())) {
00373     while (flag) {
00374       XNextEvent(disp,&ev);
00375       if (ev.type==ConfigureNotify) {
00376           w=ev.xconfigure.width;h=ev.xconfigure.height;
00377           Imlib_render(id,im,w,h);
00378           Imlib_free_pixmap(id,p);
00379           p=Imlib_move_image(id,im);
00380           m=Imlib_move_mask(id,im);
00381           XSetWindowBackgroundPixmap(disp,win,p);
00382           if (m) XShapeCombineMask(disp,win,ShapeBounding,0,0,m,ShapeSet);
00383           XClearWindow(disp,win);
00384           XSync(disp,False);
00385         }
00386       if (ev.type==KeyPress) {
00387         if ((ev.xkey.keycode == ESC) || (ev.xkey.keycode == ENTER)) {
00388           flag = 0;
00389         }
00390       }
00391     }
00392     
00393     Imlib_kill_image(id,im);
00394     XCloseDisplay(disp);
00395     return 0;
00396   }
00397   return pid;
00398 }
00399 
00400 
00401 
00402 
00403 
00404 
00405 
00406 
00407 
00408