home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Modules / imgfilemodule.c < prev    next >
C/C++ Source or Header  |  1994-01-02  |  13KB  |  528 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* IMGFILE module - Interface to sgi libimage */
  26.  
  27. /* XXX This modele should be done better at some point. It should return
  28. ** an object of image file class, and have routines to manipulate these
  29. ** image files in a neater way (so you can get rgb images off a greyscale
  30. ** file, for instance, or do a straight display without having to get the
  31. ** image bits into python, etc).
  32. **
  33. ** Warning: this module is very non-reentrant (esp. the readscaled stuff)
  34. */
  35.  
  36. #include "allobjects.h"
  37. #include "modsupport.h"
  38.  
  39. #include <gl/image.h>
  40. #include <errno.h>
  41.  
  42. #include "/usr/people/4Dgifts/iristools/include/izoom.h"
  43.  
  44. static object * ImgfileError; /* Exception we raise for various trouble */
  45.  
  46. static int top_to_bottom;    /* True if we want top-to-bottom images */
  47.  
  48. /* The image library does not always call the error hander :-(,
  49.    therefore we have a global variable indicating that it was called.
  50.    It is cleared by imgfile_open(). */
  51.  
  52. static int error_called;
  53.  
  54.  
  55. /* The error handler */
  56.  
  57. static imgfile_error(str)
  58.     char *str;
  59. {
  60.     err_setstr(ImgfileError, str);
  61.     error_called = 1;
  62.     return;    /* To imglib, which will return a failure indicator */
  63. }
  64.  
  65.  
  66. /* Open an image file and return a pointer to it.
  67.    Make sure we raise an exception if we fail. */
  68.  
  69. static IMAGE *
  70. imgfile_open(fname)
  71.     char *fname;
  72. {
  73.     IMAGE *image;
  74.     i_seterror(imgfile_error);
  75.     error_called = 0;
  76.     errno = 0;
  77.     if ( (image = iopen(fname, "r")) == NULL ) {
  78.     /* Error may already be set by imgfile_error */
  79.     if ( !error_called ) {
  80.         if (errno)
  81.             err_errno(ImgfileError);
  82.         else
  83.             err_setstr(ImgfileError, "Can't open image file");
  84.         }
  85.     return NULL;
  86.     }
  87.     return image;
  88. }
  89.  
  90. static object *
  91. imgfile_ttob(self, args)
  92.     object *self;
  93.     object *args;
  94. {
  95.     int newval;
  96.     object *rv;
  97.     
  98.     if (!getargs(args, "i", &newval))
  99.       return NULL;
  100.     rv = newintobject(top_to_bottom);
  101.     top_to_bottom = newval;
  102.     return rv;
  103. }
  104.  
  105. static object *
  106. imgfile_read(self, args)
  107.     object *self;
  108.     object *args;
  109. {
  110.     char *fname;
  111.     object *rv;
  112.     int xsize, ysize, zsize;
  113.     char *cdatap;
  114.     long *idatap;
  115.     static short rs[8192], gs[8192], bs[8192];
  116.     int x, y;
  117.     IMAGE *image;
  118.     int yfirst, ylast, ystep;
  119.  
  120.     if ( !getargs(args, "s", &fname) )
  121.       return NULL;
  122.     
  123.     if ( (image = imgfile_open(fname)) == NULL )
  124.       return NULL;
  125.     
  126.     if ( image->colormap != CM_NORMAL ) {
  127.     iclose(image);
  128.     err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
  129.     return NULL;
  130.     }
  131.     if ( BPP(image->type) != 1 ) {
  132.     iclose(image);
  133.     err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
  134.     return NULL;
  135.     }
  136.     xsize = image->xsize;
  137.     ysize = image->ysize;
  138.     zsize = image->zsize;
  139.     if ( zsize != 1 && zsize != 3) {
  140.     iclose(image);
  141.     err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
  142.     return NULL;
  143.     }
  144.     if ( xsize > 8192 ) {
  145.     iclose(image);
  146.     err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
  147.     return NULL;
  148.     }
  149.  
  150.     if ( zsize == 3 ) zsize = 4;
  151.     rv = newsizedstringobject((char *)NULL, xsize*ysize*zsize);
  152.     if ( rv == NULL ) {
  153.       iclose(image);
  154.       return NULL;
  155.     }
  156.     cdatap = getstringvalue(rv);
  157.     idatap = (long *)cdatap;
  158.  
  159.     if (top_to_bottom) {
  160.     yfirst = ysize-1;
  161.     ylast = -1;
  162.     ystep = -1;
  163.     } else {
  164.     yfirst = 0;
  165.     ylast = ysize;
  166.     ystep = 1;
  167.     }
  168.     for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
  169.     if ( zsize == 1 ) {
  170.         getrow(image, rs, y, 0);
  171.         for(x=0; x<xsize; x++ )
  172.           *cdatap++ = rs[x];
  173.     } else {
  174.         getrow(image, rs, y, 0);
  175.         getrow(image, gs, y, 1);
  176.         getrow(image, bs, y, 2);
  177.         for(x=0; x<xsize; x++ )
  178.           *idatap++ = (rs[x] & 0xff)     |
  179.                  ((gs[x] & 0xff)<<8) |
  180.                  ((bs[x] & 0xff)<<16);
  181.     }
  182.     }
  183.     iclose(image);
  184.     if ( error_called ) {
  185.     DECREF(rv);
  186.     return NULL;
  187.     }
  188.     return rv;
  189. }
  190.  
  191. static IMAGE *glob_image;
  192. static long *glob_datap;
  193. static int glob_width, glob_z, glob_ysize;
  194.  
  195. static
  196. xs_get(buf, y)
  197.     short *buf;
  198.     int y;
  199. {
  200.     if (top_to_bottom)
  201.       getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
  202.     else
  203.       getrow(glob_image, buf, y, glob_z);
  204. }
  205.  
  206. static
  207. xs_put_c(buf, y)
  208.     short *buf;
  209.     int y;
  210. {
  211.     char *datap = (char *)glob_datap + y*glob_width;
  212.     int width = glob_width;
  213.  
  214.     while ( width-- )
  215.       *datap++ = (*buf++) & 0xff;
  216. }
  217.  
  218. static
  219. xs_put_0(buf, y)
  220.     short *buf;
  221.     int y;
  222. {
  223.     long *datap = glob_datap + y*glob_width;
  224.     int width = glob_width;
  225.  
  226.     while ( width-- )
  227.       *datap++ = (*buf++) & 0xff;
  228. }
  229. static
  230. xs_put_12(buf, y)
  231.     short *buf;
  232.     int y;
  233. {
  234.     long *datap = glob_datap + y*glob_width;
  235.     int width = glob_width;
  236.  
  237.     while ( width-- )
  238.       *datap++ |= ((*buf++) & 0xff) << (glob_z*8);
  239. }
  240.  
  241. static void
  242. xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
  243.     IMAGE *image;
  244.     int xsize, ysize, zsize;
  245.     long *datap;
  246.     int xnew, ynew;
  247.     int fmode;
  248.     double blur;
  249. {
  250.     glob_image = image;
  251.     glob_datap = datap;
  252.     glob_width = xnew;
  253.     glob_ysize = ysize;
  254.     if ( zsize == 1 ) {
  255.     glob_z = 0;
  256.     filterzoom(xs_get, xs_put_c, xsize, ysize, xnew, ynew, fmode, blur);
  257.     } else {
  258.     glob_z = 0;
  259.     filterzoom(xs_get, xs_put_0, xsize, ysize, xnew, ynew, fmode, blur);
  260.     glob_z = 1;
  261.     filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
  262.     glob_z = 2;
  263.     filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
  264.     }
  265. }
  266.  
  267.  
  268. static object *
  269. imgfile_readscaled(self, args)
  270.     object *self;
  271.     object *args;
  272. {
  273.     char *fname;
  274.     object *rv;
  275.     int xsize, ysize, zsize;
  276.     char *cdatap;
  277.     long *idatap;
  278.     static short rs[8192], gs[8192], bs[8192];
  279.     int x, y;
  280.     int xwtd, ywtd, xorig, yorig;
  281.     float xfac, yfac;
  282.     int cnt;
  283.     IMAGE *image;
  284.     char *filter;
  285.     double blur;
  286.     int extended;
  287.     int fmode;
  288.     int yfirst, ylast, ystep;
  289.  
  290.     /*
  291.      ** Parse args. Funny, since arg 4 and 5 are optional
  292.      ** (filter name and blur factor). Also, 4 or 5 arguments indicates
  293.      ** extended scale algorithm in stead of simple-minded pixel drop/dup.
  294.      */
  295.     extended = 0;
  296.     cnt = gettuplesize(args);
  297.     if ( cnt == 5 ) {
  298.     extended = 1;
  299.     if ( !getargs(args, "(siisd)", &fname, &xwtd, &ywtd, &filter, &blur) )
  300.       return NULL;
  301.     } else if ( cnt == 4 ) {
  302.     extended = 1;
  303.     if ( !getargs(args, "(siis)", &fname, &xwtd, &ywtd, &filter) )
  304.       return NULL;
  305.     blur = 1.0;
  306.     } else if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) )
  307.       return NULL;
  308.  
  309.     /*
  310.      ** Check parameters, open file and check type, rows, etc.
  311.      */
  312.     if ( extended ) {
  313.     if ( strcmp(filter, "impulse") == 0 ) fmode = IMPULSE;
  314.     else if ( strcmp( filter, "box") == 0 ) fmode = BOX;
  315.     else if ( strcmp( filter, "triangle") == 0 ) fmode = TRIANGLE;
  316.     else if ( strcmp( filter, "quadratic") == 0 ) fmode = QUADRATIC;
  317.     else if ( strcmp( filter, "gaussian") == 0 ) fmode = GAUSSIAN;
  318.     else {
  319.         err_setstr(ImgfileError, "Unknown filter type");
  320.         return NULL;
  321.     }
  322.     }
  323.     
  324.     if ( (image = imgfile_open(fname)) == NULL )
  325.       return NULL;
  326.     
  327.     if ( image->colormap != CM_NORMAL ) {
  328.     iclose(image);
  329.     err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
  330.     return NULL;
  331.     }
  332.     if ( BPP(image->type) != 1 ) {
  333.     iclose(image);
  334.     err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
  335.     return NULL;
  336.     }
  337.     xsize = image->xsize;
  338.     ysize = image->ysize;
  339.     zsize = image->zsize;
  340.     if ( zsize != 1 && zsize != 3) {
  341.     iclose(image);
  342.     err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
  343.     return NULL;
  344.     }
  345.     if ( xsize > 8192 ) {
  346.     iclose(image);
  347.     err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
  348.     return NULL;
  349.     }
  350.  
  351.     if ( zsize == 3 ) zsize = 4;
  352.     rv = newsizedstringobject(NULL, xwtd*ywtd*zsize);
  353.     if ( rv == NULL ) {
  354.     iclose(image);
  355.     return NULL;
  356.     }
  357.     xfac = (float)xsize/(float)xwtd;
  358.     yfac = (float)ysize/(float)ywtd;
  359.     cdatap = getstringvalue(rv);
  360.     idatap = (long *)cdatap;
  361.  
  362.     if ( extended ) {
  363.     xscale(image, xsize, ysize, zsize, idatap, xwtd, ywtd, fmode, blur);
  364.     } else {
  365.     if (top_to_bottom) {
  366.         yfirst = ywtd-1;
  367.         ylast = -1;
  368.         ystep = -1;
  369.     } else {
  370.         yfirst = 0;
  371.         ylast = ywtd;
  372.         ystep = 1;
  373.     }
  374.     for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
  375.         yorig = (int)(y*yfac);
  376.         if ( zsize == 1 ) {
  377.         getrow(image, rs, yorig, 0);
  378.         for(x=0; x<xwtd; x++ ) {
  379.             *cdatap++ = rs[(int)(x*xfac)];    
  380.         }
  381.         } else {
  382.         getrow(image, rs, yorig, 0);
  383.         getrow(image, gs, yorig, 1);
  384.         getrow(image, bs, yorig, 2);
  385.         for(x=0; x<xwtd; x++ ) {
  386.             xorig = (int)(x*xfac);
  387.             *idatap++ = (rs[xorig] & 0xff)     |
  388.               ((gs[xorig] & 0xff)<<8) |
  389.             ((bs[xorig] & 0xff)<<16);
  390.         }
  391.         }
  392.     }
  393.     }
  394.     iclose(image);
  395.     if ( error_called ) {
  396.     DECREF(rv);
  397.     return NULL;
  398.     }
  399.     return rv;
  400. }
  401.  
  402. static object *
  403. imgfile_getsizes(self, args)
  404.     object *self;
  405.     object *args;
  406. {
  407.     char *fname;
  408.     object *rv;
  409.     IMAGE *image;
  410.     
  411.     if ( !getargs(args, "s", &fname) )
  412.       return NULL;
  413.     
  414.     if ( (image = imgfile_open(fname)) == NULL )
  415.       return NULL;
  416.     rv = mkvalue("(iii)", image->xsize, image->ysize, image->zsize);
  417.     iclose(image);
  418.     return rv;
  419. }
  420.  
  421. static object *
  422. imgfile_write(self, args)
  423.     object *self;
  424.     object *args;
  425. {
  426.     IMAGE *image;
  427.     char *fname;
  428.     int xsize, ysize, zsize, len;
  429.     char *cdatap;
  430.     long *idatap;
  431.     short rs[8192], gs[8192], bs[8192];
  432.     short r, g, b;
  433.     long rgb;
  434.     int x, y;
  435.     int yfirst, ylast, ystep;
  436.  
  437.  
  438.     if ( !getargs(args, "(ss#iii)",
  439.           &fname, &cdatap, &len, &xsize, &ysize, &zsize) )
  440.       return NULL;
  441.     
  442.     if ( zsize != 1 && zsize != 3 ) {
  443.     err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
  444.     return NULL;
  445.     }
  446.     if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
  447.     err_setstr(ImgfileError, "Data does not match sizes");
  448.     return NULL;
  449.     }
  450.     if ( xsize > 8192 ) {
  451.     err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
  452.     return NULL;
  453.     }
  454.  
  455.     error_called = 0;
  456.     errno = 0;
  457.     image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize);
  458.     if ( image == 0 ) {
  459.     if ( ! error_called ) {
  460.         if (errno)
  461.         err_errno(ImgfileError);
  462.         else
  463.             err_setstr(ImgfileError, "Can't create image file");
  464.     }
  465.     return NULL;
  466.     }
  467.  
  468.     idatap = (long *)cdatap;
  469.     
  470.     if (top_to_bottom) {
  471.     yfirst = ysize-1;
  472.     ylast = -1;
  473.     ystep = -1;
  474.     } else {
  475.     yfirst = 0;
  476.     ylast = ysize;
  477.     ystep = 1;
  478.     }
  479.     for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
  480.     if ( zsize == 1 ) {
  481.         for( x=0; x<xsize; x++ )
  482.           rs[x] = *cdatap++;
  483.         putrow(image, rs, y, 0);
  484.     } else {
  485.         for( x=0; x<xsize; x++ ) {
  486.         rgb = *idatap++;
  487.         r = rgb & 0xff;
  488.         g = (rgb >> 8 ) & 0xff;
  489.         b = (rgb >> 16 ) & 0xff;
  490.         rs[x] = r;
  491.         gs[x] = g;
  492.         bs[x] = b;
  493.         }
  494.         putrow(image, rs, y, 0);
  495.         putrow(image, gs, y, 1);
  496.         putrow(image, bs, y, 2);
  497.     }
  498.     }
  499.     iclose(image);
  500.     if ( error_called )
  501.       return NULL;
  502.     INCREF(None);
  503.     return None;
  504.     
  505. }
  506.  
  507.  
  508. static struct methodlist imgfile_methods[] = {
  509.     { "getsizes",    imgfile_getsizes },
  510.     { "read",        imgfile_read },
  511.     { "readscaled",    imgfile_readscaled, 1},
  512.     { "write",        imgfile_write },
  513.     { "ttob",        imgfile_ttob },
  514.     { NULL,        NULL } /* Sentinel */
  515. };
  516.  
  517.  
  518. void
  519. initimgfile()
  520. {
  521.     object *m, *d;
  522.     m = initmodule("imgfile", imgfile_methods);
  523.     d = getmoduledict(m);
  524.     ImgfileError = newstringobject("imgfile.error");
  525.     if ( ImgfileError == NULL || dictinsert(d, "error", ImgfileError) )
  526.         fatal("can't define imgfile.error");
  527. }
  528.