home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Modules / imageop.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  17KB  |  771 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. 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 or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* imageopmodule - Various operations on pictures */
  33.  
  34. #ifdef sun
  35. #define signed
  36. #endif
  37.  
  38. #include "Python.h"
  39.  
  40. #if SIZEOF_INT == 4
  41. typedef int Py_Int32;
  42. typedef unsigned int Py_UInt32;
  43. #else
  44. #if SIZEOF_LONG == 4
  45. typedef long Py_Int32;
  46. typedef unsigned long Py_UInt32;
  47. #else
  48. #error "No 4-byte integral type"
  49. #endif
  50. #endif
  51.  
  52. #define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x))
  53. #define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x)))
  54. #define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x)))
  55.  
  56. static PyObject *ImageopError;
  57.  
  58. static PyObject *
  59. imageop_crop(self, args)
  60.     PyObject *self;
  61. PyObject *args;
  62. {
  63.     char *cp, *ncp;
  64.     short *nsp;
  65.     Py_Int32 *nlp;
  66.     int len, size, x, y, newx1, newx2, newy1, newy2;
  67.     int ix, iy, xstep, ystep;
  68.     PyObject *rv;
  69.  
  70.     if ( !PyArg_Parse(args, "(s#iiiiiii)", &cp, &len, &size, &x, &y,
  71.               &newx1, &newy1, &newx2, &newy2) )
  72.         return 0;
  73.     
  74.     if ( size != 1 && size != 2 && size != 4 ) {
  75.         PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
  76.         return 0;
  77.     }
  78.     if ( len != size*x*y ) {
  79.         PyErr_SetString(ImageopError, "String has incorrect length");
  80.         return 0;
  81.     }
  82.     xstep = (newx1 < newx2)? 1 : -1;
  83.     ystep = (newy1 < newy2)? 1 : -1;
  84.     
  85.     rv = PyString_FromStringAndSize(NULL,
  86.                  (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
  87.     if ( rv == 0 )
  88.         return 0;
  89.     ncp = (char *)PyString_AsString(rv);
  90.     nsp = (short *)ncp;
  91.     nlp = (Py_Int32 *)ncp;
  92.     newy2 += ystep;
  93.     newx2 += xstep;
  94.     for( iy = newy1; iy != newy2; iy+=ystep ) {
  95.         for ( ix = newx1; ix != newx2; ix+=xstep ) {
  96.             if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
  97.                 if ( size == 1 )
  98.                     *ncp++ = 0;
  99.                 else
  100.                     *nlp++ = 0;
  101.             } else {
  102.                 if ( size == 1 )
  103.                     *ncp++ = *CHARP(cp, x, ix, iy);
  104.                 else if ( size == 2 )
  105.                     *nsp++ = *SHORTP(cp, x, ix, iy);
  106.                 else
  107.                     *nlp++ = *LONGP(cp, x, ix, iy);
  108.             }
  109.         }
  110.     }
  111.     return rv;
  112. }
  113.  
  114. static PyObject *
  115. imageop_scale(self, args)
  116.     PyObject *self;
  117. PyObject *args;
  118. {
  119.     char *cp, *ncp;
  120.     short *nsp;
  121.     Py_Int32 *nlp;
  122.     int len, size, x, y, newx, newy;
  123.     int ix, iy;
  124.     int oix, oiy;
  125.     PyObject *rv;
  126.  
  127.     if ( !PyArg_Parse(args, "(s#iiiii)",
  128.               &cp, &len, &size, &x, &y, &newx, &newy) )
  129.         return 0;
  130.     
  131.     if ( size != 1 && size != 2 && size != 4 ) {
  132.         PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
  133.         return 0;
  134.     }
  135.     if ( len != size*x*y ) {
  136.         PyErr_SetString(ImageopError, "String has incorrect length");
  137.         return 0;
  138.     }
  139.     
  140.     rv = PyString_FromStringAndSize(NULL, newx*newy*size);
  141.     if ( rv == 0 )
  142.         return 0;
  143.     ncp = (char *)PyString_AsString(rv);
  144.     nsp = (short *)ncp;
  145.     nlp = (Py_Int32 *)ncp;
  146.     for( iy = 0; iy < newy; iy++ ) {
  147.         for ( ix = 0; ix < newx; ix++ ) {
  148.             oix = ix * x / newx;
  149.             oiy = iy * y / newy;
  150.             if ( size == 1 )
  151.                 *ncp++ = *CHARP(cp, x, oix, oiy);
  152.             else if ( size == 2 )
  153.                 *nsp++ = *SHORTP(cp, x, oix, oiy);
  154.             else
  155.                 *nlp++ = *LONGP(cp, x, oix, oiy);
  156.         }
  157.     }
  158.     return rv;
  159. }
  160.  
  161. /* Note: this routine can use a bit of optimizing */
  162.  
  163. static PyObject *
  164. imageop_tovideo(self, args)
  165.     PyObject *self;
  166. PyObject *args;
  167. {
  168.     int maxx, maxy, x, y, len;
  169.     int i;
  170.     unsigned char *cp, *ncp;
  171.     int width;
  172.     PyObject *rv;
  173.    
  174.     
  175.     if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &width, &maxx, &maxy) )
  176.         return 0;
  177.  
  178.     if ( width != 1 && width != 4 ) {
  179.         PyErr_SetString(ImageopError, "Size should be 1 or 4");
  180.         return 0;
  181.     }
  182.     if ( maxx*maxy*width != len ) {
  183.         PyErr_SetString(ImageopError, "String has incorrect length");
  184.         return 0;
  185.     }
  186.     
  187.     rv = PyString_FromStringAndSize(NULL, len);
  188.     if ( rv == 0 )
  189.         return 0;
  190.     ncp = (unsigned char *)PyString_AsString(rv);
  191.  
  192.     if ( width == 1 ) {
  193.         memcpy(ncp, cp, maxx);        /* Copy first line */
  194.         ncp += maxx;
  195.         for (y=1; y<maxy; y++) {    /* Interpolate other lines */
  196.             for(x=0; x<maxx; x++) {
  197.                 i = y*maxx + x;
  198.                 *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
  199.             }
  200.         }
  201.     } else {
  202.         memcpy(ncp, cp, maxx*4);        /* Copy first line */
  203.         ncp += maxx*4;
  204.         for (y=1; y<maxy; y++) {    /* Interpolate other lines */
  205.             for(x=0; x<maxx; x++) {
  206.                 i = (y*maxx + x)*4 + 1;
  207.                 *ncp++ = 0;    /* Skip alfa comp */
  208.                 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
  209.                 i++;
  210.                 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
  211.                 i++;
  212.                 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
  213.             }
  214.         }
  215.     }
  216.     return rv;
  217. }
  218.  
  219. static PyObject *
  220. imageop_grey2mono(self, args)
  221.     PyObject *self;
  222. PyObject *args;
  223. {
  224.     int tres, x, y, len;
  225.     unsigned char *cp, *ncp;
  226.     unsigned char ovalue;
  227.     PyObject *rv;
  228.     int i, bit;
  229.    
  230.     
  231.     if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &x, &y, &tres) )
  232.         return 0;
  233.  
  234.     if ( x*y != len ) {
  235.         PyErr_SetString(ImageopError, "String has incorrect length");
  236.         return 0;
  237.     }
  238.     
  239.     rv = PyString_FromStringAndSize(NULL, (len+7)/8);
  240.     if ( rv == 0 )
  241.         return 0;
  242.     ncp = (unsigned char *)PyString_AsString(rv);
  243.  
  244.     bit = 0x80;
  245.     ovalue = 0;
  246.     for ( i=0; i < len; i++ ) {
  247.         if ( (int)cp[i] > tres )
  248.             ovalue |= bit;
  249.         bit >>= 1;
  250.         if ( bit == 0 ) {
  251.             *ncp++ = ovalue;
  252.             bit = 0x80;
  253.             ovalue = 0;
  254.         }
  255.     }
  256.     if ( bit != 0x80 )
  257.         *ncp++ = ovalue;
  258.     return rv;
  259. }
  260.  
  261. static PyObject *
  262. imageop_grey2grey4(self, args)
  263.     PyObject *self;
  264. PyObject *args;
  265. {
  266.     int x, y, len;
  267.     unsigned char *cp, *ncp;
  268.     unsigned char ovalue;
  269.     PyObject *rv;
  270.     int i;
  271.     int pos;
  272.    
  273.     
  274.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  275.         return 0;
  276.  
  277.     if ( x*y != len ) {
  278.         PyErr_SetString(ImageopError, "String has incorrect length");
  279.         return 0;
  280.     }
  281.     
  282.     rv = PyString_FromStringAndSize(NULL, (len+1)/2);
  283.     if ( rv == 0 )
  284.         return 0;
  285.     ncp = (unsigned char *)PyString_AsString(rv);
  286.     pos = 0;
  287.     ovalue = 0;
  288.     for ( i=0; i < len; i++ ) {
  289.         ovalue |= ((int)cp[i] & 0xf0) >> pos;
  290.         pos += 4;
  291.         if ( pos == 8 ) {
  292.             *ncp++ = ovalue;
  293.             ovalue = 0;
  294.             pos = 0;
  295.         }
  296.     }
  297.     if ( pos != 0 )
  298.         *ncp++ = ovalue;
  299.     return rv;
  300. }
  301.  
  302. static PyObject *
  303. imageop_grey2grey2(self, args)
  304.     PyObject *self;
  305. PyObject *args;
  306. {
  307.     int x, y, len;
  308.     unsigned char *cp, *ncp;
  309.     unsigned char ovalue;
  310.     PyObject *rv;
  311.     int i;
  312.     int pos;
  313.    
  314.     
  315.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  316.         return 0;
  317.  
  318.     if ( x*y != len ) {
  319.         PyErr_SetString(ImageopError, "String has incorrect length");
  320.         return 0;
  321.     }
  322.     
  323.     rv = PyString_FromStringAndSize(NULL, (len+3)/4);
  324.     if ( rv == 0 )
  325.         return 0;
  326.     ncp = (unsigned char *)PyString_AsString(rv);
  327.     pos = 0;
  328.     ovalue = 0;
  329.     for ( i=0; i < len; i++ ) {
  330.         ovalue |= ((int)cp[i] & 0xc0) >> pos;
  331.         pos += 2;
  332.         if ( pos == 8 ) {
  333.             *ncp++ = ovalue;
  334.             ovalue = 0;
  335.             pos = 0;
  336.         }
  337.     }
  338.     if ( pos != 0 )
  339.         *ncp++ = ovalue;
  340.     return rv;
  341. }
  342.  
  343. static PyObject *
  344. imageop_dither2mono(self, args)
  345.     PyObject *self;
  346. PyObject *args;
  347. {
  348.     int sum, x, y, len;
  349.     unsigned char *cp, *ncp;
  350.     unsigned char ovalue;
  351.     PyObject *rv;
  352.     int i, bit;
  353.    
  354.     
  355.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  356.         return 0;
  357.  
  358.     if ( x*y != len ) {
  359.         PyErr_SetString(ImageopError, "String has incorrect length");
  360.         return 0;
  361.     }
  362.     
  363.     rv = PyString_FromStringAndSize(NULL, (len+7)/8);
  364.     if ( rv == 0 )
  365.         return 0;
  366.     ncp = (unsigned char *)PyString_AsString(rv);
  367.  
  368.     bit = 0x80;
  369.     ovalue = 0;
  370.     sum = 0;
  371.     for ( i=0; i < len; i++ ) {
  372.         sum += cp[i];
  373.         if ( sum >= 256 ) {
  374.             sum -= 256;
  375.             ovalue |= bit;
  376.         }
  377.         bit >>= 1;
  378.         if ( bit == 0 ) {
  379.             *ncp++ = ovalue;
  380.             bit = 0x80;
  381.             ovalue = 0;
  382.         }
  383.     }
  384.     if ( bit != 0x80 )
  385.         *ncp++ = ovalue;
  386.     return rv;
  387. }
  388.  
  389. static PyObject *
  390. imageop_dither2grey2(self, args)
  391.     PyObject *self;
  392. PyObject *args;
  393. {
  394.     int x, y, len;
  395.     unsigned char *cp, *ncp;
  396.     unsigned char ovalue;
  397.     PyObject *rv;
  398.     int i;
  399.     int pos;
  400.     int sum = 0, nvalue;
  401.    
  402.     
  403.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  404.         return 0;
  405.  
  406.     if ( x*y != len ) {
  407.         PyErr_SetString(ImageopError, "String has incorrect length");
  408.         return 0;
  409.     }
  410.     
  411.     rv = PyString_FromStringAndSize(NULL, (len+3)/4);
  412.     if ( rv == 0 )
  413.         return 0;
  414.     ncp = (unsigned char *)PyString_AsString(rv);
  415.     pos = 1;
  416.     ovalue = 0;
  417.     for ( i=0; i < len; i++ ) {
  418.         sum += cp[i];
  419.         nvalue = sum & 0x180;
  420.         sum -= nvalue;
  421.         ovalue |= nvalue >> pos;
  422.         pos += 2;
  423.         if ( pos == 9 ) {
  424.             *ncp++ = ovalue;
  425.             ovalue = 0;
  426.             pos = 1;
  427.         }
  428.     }
  429.     if ( pos != 0 )
  430.         *ncp++ = ovalue;
  431.     return rv;
  432. }
  433.  
  434. static PyObject *
  435. imageop_mono2grey(self, args)
  436.     PyObject *self;
  437. PyObject *args;
  438. {
  439.     int v0, v1, x, y, len, nlen;
  440.     unsigned char *cp, *ncp;
  441.     PyObject *rv;
  442.     int i, bit;
  443.     
  444.     if ( !PyArg_Parse(args, "(s#iiii)", &cp, &len, &x, &y, &v0, &v1) )
  445.         return 0;
  446.  
  447.     nlen = x*y;
  448.     if ( (nlen+7)/8 != len ) {
  449.         PyErr_SetString(ImageopError, "String has incorrect length");
  450.         return 0;
  451.     }
  452.     
  453.     rv = PyString_FromStringAndSize(NULL, nlen);
  454.     if ( rv == 0 )
  455.         return 0;
  456.     ncp = (unsigned char *)PyString_AsString(rv);
  457.  
  458.     bit = 0x80;
  459.     for ( i=0; i < nlen; i++ ) {
  460.         if ( *cp & bit )
  461.             *ncp++ = v1;
  462.         else
  463.             *ncp++ = v0;
  464.         bit >>= 1;
  465.         if ( bit == 0 ) {
  466.             bit = 0x80;
  467.             cp++;
  468.         }
  469.     }
  470.     return rv;
  471. }
  472.  
  473. static PyObject *
  474. imageop_grey22grey(self, args)
  475.     PyObject *self;
  476. PyObject *args;
  477. {
  478.     int x, y, len, nlen;
  479.     unsigned char *cp, *ncp;
  480.     PyObject *rv;
  481.     int i, pos, value = 0, nvalue;
  482.     
  483.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  484.         return 0;
  485.  
  486.     nlen = x*y;
  487.     if ( (nlen+3)/4 != len ) {
  488.         PyErr_SetString(ImageopError, "String has incorrect length");
  489.         return 0;
  490.     }
  491.     
  492.     rv = PyString_FromStringAndSize(NULL, nlen);
  493.     if ( rv == 0 )
  494.         return 0;
  495.     ncp = (unsigned char *)PyString_AsString(rv);
  496.  
  497.     pos = 0;
  498.     for ( i=0; i < nlen; i++ ) {
  499.         if ( pos == 0 ) {
  500.             value = *cp++;
  501.             pos = 8;
  502.         }
  503.         pos -= 2;
  504.         nvalue = (value >> pos) & 0x03;
  505.         *ncp++ = nvalue | (nvalue << 2) |
  506.              (nvalue << 4) | (nvalue << 6);
  507.     }
  508.     return rv;
  509. }
  510.  
  511. static PyObject *
  512. imageop_grey42grey(self, args)
  513.     PyObject *self;
  514. PyObject *args;
  515. {
  516.     int x, y, len, nlen;
  517.     unsigned char *cp, *ncp;
  518.     PyObject *rv;
  519.     int i, pos, value = 0, nvalue;
  520.     
  521.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  522.         return 0;
  523.  
  524.     nlen = x*y;
  525.     if ( (nlen+1)/2 != len ) {
  526.         PyErr_SetString(ImageopError, "String has incorrect length");
  527.         return 0;
  528.     }
  529.     
  530.     rv = PyString_FromStringAndSize(NULL, nlen);
  531.     if ( rv == 0 )
  532.         return 0;
  533.     ncp = (unsigned char *)PyString_AsString(rv);
  534.  
  535.     pos = 0;
  536.     for ( i=0; i < nlen; i++ ) {
  537.         if ( pos == 0 ) {
  538.             value = *cp++;
  539.             pos = 8;
  540.         }
  541.         pos -= 4;
  542.         nvalue = (value >> pos) & 0x0f;
  543.         *ncp++ = nvalue | (nvalue << 4);
  544.     }
  545.     return rv;
  546. }
  547.  
  548. static PyObject *
  549. imageop_rgb2rgb8(self, args)
  550.     PyObject *self;
  551. PyObject *args;
  552. {
  553.     int x, y, len, nlen;
  554.     Py_UInt32 *cp;
  555.     unsigned char *ncp;
  556.     PyObject *rv;
  557.     int i, r, g, b;
  558.     Py_UInt32 value, nvalue;
  559.     
  560.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  561.         return 0;
  562.  
  563.     nlen = x*y;
  564.     if ( nlen*4 != len ) {
  565.         PyErr_SetString(ImageopError, "String has incorrect length");
  566.         return 0;
  567.     }
  568.     
  569.     rv = PyString_FromStringAndSize(NULL, nlen);
  570.     if ( rv == 0 )
  571.         return 0;
  572.     ncp = (unsigned char *)PyString_AsString(rv);
  573.  
  574.     for ( i=0; i < nlen; i++ ) {
  575.         /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
  576.         value = *cp++;
  577. #if 0
  578.         r = (value >>  5) & 7;
  579.         g = (value >> 13) & 7;
  580.         b = (value >> 22) & 3;
  581. #else
  582.         r = (int) ((value & 0xff) / 255. * 7. + .5);
  583.         g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
  584.         b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
  585. #endif
  586.         nvalue = (r<<5) | (b<<3) | g;
  587.         *ncp++ = (unsigned char)nvalue;
  588.     }
  589.     return rv;
  590. }
  591.  
  592. static PyObject *
  593. imageop_rgb82rgb(self, args)
  594.     PyObject *self;
  595. PyObject *args;
  596. {
  597.     int x, y, len, nlen;
  598.     unsigned char *cp;
  599.     Py_UInt32 *ncp;
  600.     PyObject *rv;
  601.     int i, r, g, b;
  602.     Py_UInt32 value, nvalue;
  603.     
  604.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  605.         return 0;
  606.  
  607.     nlen = x*y;
  608.     if ( nlen != len ) {
  609.         PyErr_SetString(ImageopError, "String has incorrect length");
  610.         return 0;
  611.     }
  612.     
  613.     rv = PyString_FromStringAndSize(NULL, nlen*4);
  614.     if ( rv == 0 )
  615.         return 0;
  616.     ncp = (Py_UInt32 *)PyString_AsString(rv);
  617.  
  618.     for ( i=0; i < nlen; i++ ) {
  619.         /* Bits in source: RRRBBGGG
  620.         ** Red and Green are multiplied by 36.5, Blue by 85
  621.         */
  622.         value = *cp++;
  623.         r = (value >> 5) & 7;
  624.         g = (value     ) & 7;
  625.         b = (value >> 3) & 3;
  626.         r = (r<<5) | (r<<3) | (r>>1);
  627.         g = (g<<5) | (g<<3) | (g>>1);
  628.         b = (b<<6) | (b<<4) | (b<<2) | b;
  629.         nvalue = r | (g<<8) | (b<<16);
  630.         *ncp++ = nvalue;
  631.     }
  632.     return rv;
  633. }
  634.  
  635. static PyObject *
  636. imageop_rgb2grey(self, args)
  637.     PyObject *self;
  638. PyObject *args;
  639. {
  640.     int x, y, len, nlen;
  641.     Py_UInt32 *cp;
  642.     unsigned char *ncp;
  643.     PyObject *rv;
  644.     int i, r, g, b;
  645.     Py_UInt32 value, nvalue;
  646.     
  647.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  648.         return 0;
  649.  
  650.     nlen = x*y;
  651.     if ( nlen*4 != len ) {
  652.         PyErr_SetString(ImageopError, "String has incorrect length");
  653.         return 0;
  654.     }
  655.     
  656.     rv = PyString_FromStringAndSize(NULL, nlen);
  657.     if ( rv == 0 )
  658.         return 0;
  659.     ncp = (unsigned char *)PyString_AsString(rv);
  660.  
  661.     for ( i=0; i < nlen; i++ ) {
  662.         value = *cp++;
  663.         r = (value      ) & 0xff;
  664.         g = (value >>  8) & 0xff;
  665.         b = (value >> 16) & 0xff;
  666.         nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
  667.         if ( nvalue > 255 ) nvalue = 255;
  668.         *ncp++ = (unsigned char)nvalue;
  669.     }
  670.     return rv;
  671. }
  672.  
  673. static PyObject *
  674. imageop_grey2rgb(self, args)
  675.     PyObject *self;
  676. PyObject *args;
  677. {
  678.     int x, y, len, nlen;
  679.     unsigned char *cp;
  680.     Py_UInt32 *ncp;
  681.     PyObject *rv;
  682.     int i;
  683.     Py_UInt32 value;
  684.     
  685.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  686.         return 0;
  687.  
  688.     nlen = x*y;
  689.     if ( nlen != len ) {
  690.         PyErr_SetString(ImageopError, "String has incorrect length");
  691.         return 0;
  692.     }
  693.     
  694.     rv = PyString_FromStringAndSize(NULL, nlen*4);
  695.     if ( rv == 0 )
  696.         return 0;
  697.     ncp = (Py_UInt32 *)PyString_AsString(rv);
  698.  
  699.     for ( i=0; i < nlen; i++ ) {
  700.         value = *cp++;
  701.         *ncp++ = value | (value << 8 ) | (value << 16);
  702.     }
  703.     return rv;
  704. }
  705.  
  706. /*
  707. static object *
  708. imageop_mul(self, args)
  709.     object *self;
  710. object *args;
  711. {
  712.     char *cp, *ncp;
  713.     int len, size, x, y;
  714.     object *rv;
  715.     int i;
  716.  
  717.     if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) )
  718.         return 0;
  719.     
  720.     if ( size != 1 && size != 4 ) {
  721.         err_setstr(ImageopError, "Size should be 1 or 4");
  722.         return 0;
  723.     }
  724.     if ( len != size*x*y ) {
  725.         err_setstr(ImageopError, "String has incorrect length");
  726.         return 0;
  727.     }
  728.     
  729.     rv = newsizedstringobject(NULL, XXXX);
  730.     if ( rv == 0 )
  731.         return 0;
  732.     ncp = (char *)getstringvalue(rv);
  733.     
  734.     
  735.     for ( i=0; i < len; i += size ) {
  736.     }
  737.     return rv;
  738. }
  739. */
  740.  
  741. static PyMethodDef imageop_methods[] = {
  742.     { "crop",        imageop_crop },
  743.     { "scale",        imageop_scale },
  744.     { "grey2mono",            imageop_grey2mono },
  745.     { "grey2grey2",            imageop_grey2grey2 },
  746.     { "grey2grey4",            imageop_grey2grey4 },
  747.     { "dither2mono",    imageop_dither2mono },
  748.     { "dither2grey2",    imageop_dither2grey2 },
  749.     { "mono2grey",            imageop_mono2grey },
  750.     { "grey22grey",            imageop_grey22grey },
  751.     { "grey42grey",            imageop_grey42grey },
  752.     { "tovideo",            imageop_tovideo },
  753.     { "rgb2rgb8",            imageop_rgb2rgb8 },
  754.     { "rgb82rgb",            imageop_rgb82rgb },
  755.     { "rgb2grey",            imageop_rgb2grey },
  756.     { "grey2rgb",            imageop_grey2rgb },
  757.     { 0,                    0 }
  758. };
  759.  
  760.  
  761. DL_EXPORT(void)
  762. initimageop()
  763. {
  764.     PyObject *m, *d;
  765.     m = Py_InitModule("imageop", imageop_methods);
  766.     d = PyModule_GetDict(m);
  767.     ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
  768.     if (ImageopError != NULL)
  769.         PyDict_SetItemString(d, "error", ImageopError);
  770. }
  771.