1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
Modules/imageop.c
/* imageopmodule - Various operations on pictures */

#ifdef sun
#define signed
#endif

#include "Python.h"

#if SIZEOF_INT == 4
typedef int Py_Int32;
typedef unsigned int Py_UInt32;
#else
#if SIZEOF_LONG == 4
typedef long Py_Int32;
typedef unsigned long Py_UInt32;
#else
#error "No 4-byte integral type"
#endif
#endif

#define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x))
#define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x)))
#define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x)))

static PyObject *ImageopError;
static PyObject *ImageopDict;

/**
 * Check a coordonnate, make sure that (0 < value).
 * Return 0 on error.
 */
static int
check_coordonnate(int value, const char* name)
{
    if ( 0 < value)
        return 1;
    PyErr_Format(PyExc_ValueError, "%s value is negative or nul", name);
    return 0;
}

/**
 * Check integer overflow to make sure that product == x*y*size.
 * Return 0 on error.
 */
static int
check_multiply_size(int product, int x, const char* xname, int y, const char* yname, int size)
{
    if ( !check_coordonnate(x, xname) )
        return 0;
    if ( !check_coordonnate(y, yname) )
        return 0;
    if ( product % y == 0 ) {
        product /= y;
        if ( product % x == 0 && size == product / x )
            return 1;
    }
    PyErr_SetString(ImageopError, "String has incorrect length");
    return 0;
}

/**
 * Check integer overflow to make sure that product == x*y.
 * Return 0 on error.
 */
static int
check_multiply(int product, int x, int y)
{
    return check_multiply_size(product, x, "x", y, "y", 1);
}

/* If this function returns true (the default if anything goes wrong), we're
   behaving in a backward-compatible way with respect to how multi-byte pixels
   are stored in the strings.  The code in this module was originally written
   for an SGI which is a big-endian system, and so the old code assumed that
   4-byte integers hold the R, G, and B values in a particular order.
   However, on little-endian systems the order is reversed, and so not
   actually compatible with what gl.lrectwrite and imgfile expect.
   (gl.lrectwrite and imgfile are also SGI-specific, however, it is
   conceivable that the data handled here comes from or goes to an SGI or that
   it is otherwise used in the expectation that the byte order in the strings
   is as specified.)

   The function returns the value of the module variable
   "backward_compatible", or 1 if the variable does not exist or is not an
   int.
 */

static int
imageop_backward_compatible(void)
{
    static PyObject *bcos;
    PyObject *bco;
    long rc;

    if (ImageopDict == NULL) /* "cannot happen" */
        return 1;
    if (bcos == NULL) {
        /* cache string object for future use */
        bcos = PyString_FromString("backward_compatible");
        if (bcos == NULL)
            return 1;
    }
    bco = PyDict_GetItem(ImageopDict, bcos);
    if (bco == NULL)
        return 1;
    if (!PyInt_Check(bco))
        return 1;
    rc = PyInt_AsLong(bco);
    if (PyErr_Occurred()) {
        /* not an integer, or too large, or something */
        PyErr_Clear();
        rc = 1;
    }
    return rc != 0;             /* convert to values 0, 1 */
}

static PyObject *
imageop_crop(PyObject *self, PyObject *args)
{
    char *cp, *ncp;
    short *nsp;
    Py_Int32 *nlp;
    int len, size, x, y, newx1, newx2, newy1, newy2, nlen;
    int ix, iy, xstep, ystep;
    PyObject *rv;

    if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y,
                      &newx1, &newy1, &newx2, &newy2) )
        return 0;

    if ( size != 1 && size != 2 && size != 4 ) {
        PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
        return 0;
    }
    if ( !check_multiply_size(len, x, "x", y, "y", size) )
        return 0;

    xstep = (newx1 < newx2)? 1 : -1;
    ystep = (newy1 < newy2)? 1 : -1;

    nlen = (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size;
    if ( !check_multiply_size(nlen, abs(newx2-newx1)+1, "abs(newx2-newx1)+1", abs(newy2-newy1)+1, "abs(newy2-newy1)+1", size) )
        return 0;
    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (char *)PyString_AsString(rv);
    nsp = (short *)ncp;
    nlp = (Py_Int32 *)ncp;
    newy2 += ystep;
    newx2 += xstep;
    for( iy = newy1; iy != newy2; iy+=ystep ) {
        for ( ix = newx1; ix != newx2; ix+=xstep ) {
            if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
                if ( size == 1 )
                    *ncp++ = 0;
                else
                    *nlp++ = 0;
            } else {
                if ( size == 1 )
                    *ncp++ = *CHARP(cp, x, ix, iy);
                else if ( size == 2 )
                    *nsp++ = *SHORTP(cp, x, ix, iy);
                else
                    *nlp++ = *LONGP(cp, x, ix, iy);
            }
        }
    }
    return rv;
}

static PyObject *
imageop_scale(PyObject *self, PyObject *args)
{
    char *cp, *ncp;
    short *nsp;
    Py_Int32 *nlp;
    int len, size, x, y, newx, newy, nlen;
    int ix, iy;
    int oix, oiy;
    PyObject *rv;

    if ( !PyArg_ParseTuple(args, "s#iiiii",
                      &cp, &len, &size, &x, &y, &newx, &newy) )
        return 0;

    if ( size != 1 && size != 2 && size != 4 ) {
        PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
        return 0;
    }
    if ( !check_multiply_size(len, x, "x", y, "y", size) )
        return 0;
    nlen = newx*newy*size;
    if ( !check_multiply_size(nlen, newx, "newx", newy, "newy", size) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (char *)PyString_AsString(rv);
    nsp = (short *)ncp;
    nlp = (Py_Int32 *)ncp;
    for( iy = 0; iy < newy; iy++ ) {
        for ( ix = 0; ix < newx; ix++ ) {
            oix = ix * x / newx;
            oiy = iy * y / newy;
            if ( size == 1 )
                *ncp++ = *CHARP(cp, x, oix, oiy);
            else if ( size == 2 )
                *nsp++ = *SHORTP(cp, x, oix, oiy);
            else
                *nlp++ = *LONGP(cp, x, oix, oiy);
        }
    }
    return rv;
}

/* Note: this routine can use a bit of optimizing */

static PyObject *
imageop_tovideo(PyObject *self, PyObject *args)
{
    int maxx, maxy, x, y, len;
    int i;
    unsigned char *cp, *ncp;
    int width;
    PyObject *rv;


    if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) )
        return 0;

    if ( width != 1 && width != 4 ) {
        PyErr_SetString(ImageopError, "Size should be 1 or 4");
        return 0;
    }
    if ( !check_multiply_size(len, maxx, "max", maxy, "maxy", width) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, len);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    if ( width == 1 ) {
        memcpy(ncp, cp, maxx);                  /* Copy first line */
        ncp += maxx;
        for (y=1; y<maxy; y++) {                /* Interpolate other lines */
            for(x=0; x<maxx; x++) {
                i = y*maxx + x;
                *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
            }
        }
    } else {
        memcpy(ncp, cp, maxx*4);                        /* Copy first line */
        ncp += maxx*4;
        for (y=1; y<maxy; y++) {                /* Interpolate other lines */
            for(x=0; x<maxx; x++) {
                i = (y*maxx + x)*4 + 1;
                *ncp++ = 0;                     /* Skip alfa comp */
                *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
                i++;
                *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
                i++;
                *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
            }
        }
    }
    return rv;
}

static PyObject *
imageop_grey2mono(PyObject *self, PyObject *args)
{
    int tres, x, y, len;
    unsigned char *cp, *ncp;
    unsigned char ovalue;
    PyObject *rv;
    int i, bit;


    if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) )
        return 0;

    if ( !check_multiply(len, x, y) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, (len+7)/8);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    bit = 0x80;
    ovalue = 0;
    for ( i=0; i < len; i++ ) {
        if ( (int)cp[i] > tres )
            ovalue |= bit;
        bit >>= 1;
        if ( bit == 0 ) {
            *ncp++ = ovalue;
            bit = 0x80;
            ovalue = 0;
        }
    }
    if ( bit != 0x80 )
        *ncp++ = ovalue;
    return rv;
}

static PyObject *
imageop_grey2grey4(PyObject *self, PyObject *args)
{
    int x, y, len;
    unsigned char *cp, *ncp;
    unsigned char ovalue;
    PyObject *rv;
    int i;
    int pos;


    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply(len, x, y) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, (len+1)/2);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);
    pos = 0;
    ovalue = 0;
    for ( i=0; i < len; i++ ) {
        ovalue |= ((int)cp[i] & 0xf0) >> pos;
        pos += 4;
        if ( pos == 8 ) {
            *ncp++ = ovalue;
            ovalue = 0;
            pos = 0;
        }
    }
    if ( pos != 0 )
        *ncp++ = ovalue;
    return rv;
}

static PyObject *
imageop_grey2grey2(PyObject *self, PyObject *args)
{
    int x, y, len;
    unsigned char *cp, *ncp;
    unsigned char ovalue;
    PyObject *rv;
    int i;
    int pos;


    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply(len, x, y) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, (len+3)/4);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);
    pos = 0;
    ovalue = 0;
    for ( i=0; i < len; i++ ) {
        ovalue |= ((int)cp[i] & 0xc0) >> pos;
        pos += 2;
        if ( pos == 8 ) {
            *ncp++ = ovalue;
            ovalue = 0;
            pos = 0;
        }
    }
    if ( pos != 0 )
        *ncp++ = ovalue;
    return rv;
}

static PyObject *
imageop_dither2mono(PyObject *self, PyObject *args)
{
    int sum, x, y, len;
    unsigned char *cp, *ncp;
    unsigned char ovalue;
    PyObject *rv;
    int i, bit;


    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply(len, x, y) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, (len+7)/8);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    bit = 0x80;
    ovalue = 0;
    sum = 0;
    for ( i=0; i < len; i++ ) {
        sum += cp[i];
        if ( sum >= 256 ) {
            sum -= 256;
            ovalue |= bit;
        }
        bit >>= 1;
        if ( bit == 0 ) {
            *ncp++ = ovalue;
            bit = 0x80;
            ovalue = 0;
        }
    }
    if ( bit != 0x80 )
        *ncp++ = ovalue;
    return rv;
}

static PyObject *
imageop_dither2grey2(PyObject *self, PyObject *args)
{
    int x, y, len;
    unsigned char *cp, *ncp;
    unsigned char ovalue;
    PyObject *rv;
    int i;
    int pos;
    int sum = 0, nvalue;


    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply(len, x, y) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, (len+3)/4);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);
    pos = 1;
    ovalue = 0;
    for ( i=0; i < len; i++ ) {
        sum += cp[i];
        nvalue = sum & 0x180;
        sum -= nvalue;
        ovalue |= nvalue >> pos;
        pos += 2;
        if ( pos == 9 ) {
            *ncp++ = ovalue;
            ovalue = 0;
            pos = 1;
        }
    }
    if ( pos != 0 )
        *ncp++ = ovalue;
    return rv;
}

static PyObject *
imageop_mono2grey(PyObject *self, PyObject *args)
{
    int v0, v1, x, y, len, nlen;
    unsigned char *cp, *ncp;
    PyObject *rv;
    int i, bit;

    if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) )
        return 0;

    nlen = x*y;
    if ( !check_multiply(nlen, x, y) )
        return 0;
    if ( (nlen+7)/8 != len ) {
        PyErr_SetString(ImageopError, "String has incorrect length");
        return 0;
    }

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    bit = 0x80;
    for ( i=0; i < nlen; i++ ) {
        if ( *cp & bit )
            *ncp++ = v1;
        else
            *ncp++ = v0;
        bit >>= 1;
        if ( bit == 0 ) {
            bit = 0x80;
            cp++;
        }
    }
    return rv;
}

static PyObject *
imageop_grey22grey(PyObject *self, PyObject *args)
{
    int x, y, len, nlen;
    unsigned char *cp, *ncp;
    PyObject *rv;
    int i, pos, value = 0, nvalue;

    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    nlen = x*y;
    if ( !check_multiply(nlen, x, y) ) {
        return 0;
    }
    if ( (nlen+3)/4 != len ) {
        PyErr_SetString(ImageopError, "String has incorrect length");
        return 0;
    }

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    pos = 0;
    for ( i=0; i < nlen; i++ ) {
        if ( pos == 0 ) {
            value = *cp++;
            pos = 8;
        }
        pos -= 2;
        nvalue = (value >> pos) & 0x03;
        *ncp++ = nvalue | (nvalue << 2) |
                 (nvalue << 4) | (nvalue << 6);
    }
    return rv;
}

static PyObject *
imageop_grey42grey(PyObject *self, PyObject *args)
{
    int x, y, len, nlen;
    unsigned char *cp, *ncp;
    PyObject *rv;
    int i, pos, value = 0, nvalue;

    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    nlen = x*y;
    if ( !check_multiply(nlen, x, y) )
        return 0;
    if ( (nlen+1)/2 != len ) {
        PyErr_SetString(ImageopError, "String has incorrect length");
        return 0;
    }

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    pos = 0;
    for ( i=0; i < nlen; i++ ) {
        if ( pos == 0 ) {
            value = *cp++;
            pos = 8;
        }
        pos -= 4;
        nvalue = (value >> pos) & 0x0f;
        *ncp++ = nvalue | (nvalue << 4);
    }
    return rv;
}

static PyObject *
imageop_rgb2rgb8(PyObject *self, PyObject *args)
{
    int x, y, len, nlen;
    unsigned char *cp;
    unsigned char *ncp;
    PyObject *rv;
    int i, r, g, b;
    int backward_compatible = imageop_backward_compatible();

    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply_size(len, x, "x", y, "y", 4) )
        return 0;
    nlen = x*y;
    if ( !check_multiply(nlen, x, y) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    for ( i=0; i < nlen; i++ ) {
        /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
        if (backward_compatible) {
            Py_UInt32 value = * (Py_UInt32 *) cp;
            cp += 4;
            r = (int) ((value & 0xff) / 255. * 7. + .5);
            g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
            b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
        } else {
            cp++;                       /* skip alpha channel */
            b = (int) (*cp++ / 255. * 3. + .5);
            g = (int) (*cp++ / 255. * 7. + .5);
            r = (int) (*cp++ / 255. * 7. + .5);
        }
        *ncp++ = (unsigned char)((r<<5) | (b<<3) | g);
    }
    return rv;
}

static PyObject *
imageop_rgb82rgb(PyObject *self, PyObject *args)
{
    int x, y, len, nlen;
    unsigned char *cp;
    unsigned char *ncp;
    PyObject *rv;
    int i, r, g, b;
    unsigned char value;
    int backward_compatible = imageop_backward_compatible();

    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply(len, x, y) )
        return 0;
    nlen = x*y*4;
    if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    for ( i=0; i < len; i++ ) {
        /* Bits in source: RRRBBGGG
        ** Red and Green are multiplied by 36.5, Blue by 85
        */
        value = *cp++;
        r = (value >> 5) & 7;
        g = (value     ) & 7;
        b = (value >> 3) & 3;
        r = (r<<5) | (r<<3) | (r>>1);
        g = (g<<5) | (g<<3) | (g>>1);
        b = (b<<6) | (b<<4) | (b<<2) | b;
        if (backward_compatible) {
            Py_UInt32 nvalue = r | (g<<8) | (b<<16);
            * (Py_UInt32 *) ncp = nvalue;
            ncp += 4;
        } else {
            *ncp++ = 0;
            *ncp++ = b;
            *ncp++ = g;
            *ncp++ = r;
        }
    }
    return rv;
}

static PyObject *
imageop_rgb2grey(PyObject *self, PyObject *args)
{
    int x, y, len, nlen;
    unsigned char *cp;
    unsigned char *ncp;
    PyObject *rv;
    int i, r, g, b;
    int nvalue;
    int backward_compatible = imageop_backward_compatible();

    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply_size(len, x, "x", y, "y", 4) )
        return 0;
    nlen = x*y;
    if ( !check_multiply(nlen, x, y) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    for ( i=0; i < nlen; i++ ) {
        if (backward_compatible) {
            Py_UInt32 value = * (Py_UInt32 *) cp;
            cp += 4;
            r = (int) ((value & 0xff) / 255. * 7. + .5);
            g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
            b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
        } else {
            cp++;                       /* skip alpha channel */
            b = *cp++;
            g = *cp++;
            r = *cp++;
        }
        nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
        if ( nvalue > 255 ) nvalue = 255;
        *ncp++ = (unsigned char)nvalue;
    }
    return rv;
}

static PyObject *
imageop_grey2rgb(PyObject *self, PyObject *args)
{
    int x, y, len, nlen;
    unsigned char *cp;
    unsigned char *ncp;
    PyObject *rv;
    int i;
    unsigned char value;
    int backward_compatible = imageop_backward_compatible();

    if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
        return 0;

    if ( !check_multiply(len, x, y) )
        return 0;
    nlen = x*y*4;
    if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
        return 0;

    rv = PyString_FromStringAndSize(NULL, nlen);
    if ( rv == 0 )
        return 0;
    ncp = (unsigned char *)PyString_AsString(rv);

    for ( i=0; i < len; i++ ) {
        value = *cp++;
        if (backward_compatible) {
            * (Py_UInt32 *) ncp = (Py_UInt32) value | ((Py_UInt32) value << 8 ) | ((Py_UInt32) value << 16);
            ncp += 4;
        } else {
            *ncp++ = 0;
            *ncp++ = value;
            *ncp++ = value;
            *ncp++ = value;
        }
    }
    return rv;
}

static PyMethodDef imageop_methods[] = {
    { "crop",                   imageop_crop, METH_VARARGS },
    { "scale",                  imageop_scale, METH_VARARGS },
    { "grey2mono",              imageop_grey2mono, METH_VARARGS },
    { "grey2grey2",             imageop_grey2grey2, METH_VARARGS },
    { "grey2grey4",             imageop_grey2grey4, METH_VARARGS },
    { "dither2mono",            imageop_dither2mono, METH_VARARGS },
    { "dither2grey2",           imageop_dither2grey2, METH_VARARGS },
    { "mono2grey",              imageop_mono2grey, METH_VARARGS },
    { "grey22grey",             imageop_grey22grey, METH_VARARGS },
    { "grey42grey",             imageop_grey42grey, METH_VARARGS },
    { "tovideo",                imageop_tovideo, METH_VARARGS },
    { "rgb2rgb8",               imageop_rgb2rgb8, METH_VARARGS },
    { "rgb82rgb",               imageop_rgb82rgb, METH_VARARGS },
    { "rgb2grey",               imageop_rgb2grey, METH_VARARGS },
    { "grey2rgb",               imageop_grey2rgb, METH_VARARGS },
    { 0,                    0 }
};


PyMODINIT_FUNC
initimageop(void)
{
    PyObject *m;

    if (PyErr_WarnPy3k("the imageop module has been removed in "
                       "Python 3.0", 2) < 0)
        return;

    m = Py_InitModule("imageop", imageop_methods);
    if (m == NULL)
        return;
    ImageopDict = PyModule_GetDict(m);
    ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
    if (ImageopError != NULL)
        PyDict_SetItemString(ImageopDict, "error", ImageopError);
}