imgfile.c 13 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1

2
/* IMGFILE module - Interface to sgi libimage */
Guido van Rossum's avatar
Guido van Rossum committed
3

4
/* XXX This module should be done better at some point. It should return
Guido van Rossum's avatar
Guido van Rossum committed
5 6 7 8
** an object of image file class, and have routines to manipulate these
** image files in a neater way (so you can get rgb images off a greyscale
** file, for instance, or do a straight display without having to get the
** image bits into python, etc).
9 10
**
** Warning: this module is very non-reentrant (esp. the readscaled stuff)
Guido van Rossum's avatar
Guido van Rossum committed
11 12
*/

Roger E. Masse's avatar
Roger E. Masse committed
13
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
14 15 16

#include <gl/image.h>

17
#include "/usr/people/4Dgifts/iristools/include/izoom.h"
18

19 20 21 22 23 24 25
/* Bunch of missing extern decls; keep gcc -Wall happy... */
extern void i_seterror();
extern void iclose();
extern void filterzoom();
extern void putrow();
extern void getrow();

Roger E. Masse's avatar
Roger E. Masse committed
26
static PyObject * ImgfileError; /* Exception we raise for various trouble */
Guido van Rossum's avatar
Guido van Rossum committed
27

28
static int top_to_bottom;       /* True if we want top-to-bottom images */
Guido van Rossum's avatar
Guido van Rossum committed
29

30 31 32
/* The image library does not always call the error hander :-(,
   therefore we have a global variable indicating that it was called.
   It is cleared by imgfile_open(). */
Guido van Rossum's avatar
Guido van Rossum committed
33

34 35
static int error_called;

36 37 38

/* The error handler */

Guido van Rossum's avatar
Guido van Rossum committed
39
static void
40
imgfile_error(char *str)
41
{
42 43 44
    PyErr_SetString(ImgfileError, str);
    error_called = 1;
    return;     /* To imglib, which will return a failure indicator */
45
}
Guido van Rossum's avatar
Guido van Rossum committed
46

47 48 49 50 51

/* Open an image file and return a pointer to it.
   Make sure we raise an exception if we fail. */

static IMAGE *
52
imgfile_open(char *fname)
53
{
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    IMAGE *image;
    i_seterror(imgfile_error);
    error_called = 0;
    errno = 0;
    if ( (image = iopen(fname, "r")) == NULL ) {
        /* Error may already be set by imgfile_error */
        if ( !error_called ) {
            if (errno)
                PyErr_SetFromErrno(ImgfileError);
            else
                PyErr_SetString(ImgfileError,
                                "Can't open image file");
        }
        return NULL;
    }
    return image;
Guido van Rossum's avatar
Guido van Rossum committed
70 71
}

Roger E. Masse's avatar
Roger E. Masse committed
72
static PyObject *
73
imgfile_ttob(PyObject *self, PyObject *args)
74
{
75 76 77 78 79 80 81 82
    int newval;
    PyObject *rv;

    if (!PyArg_ParseTuple(args, "i:ttob", &newval))
        return NULL;
    rv = PyInt_FromLong(top_to_bottom);
    top_to_bottom = newval;
    return rv;
83
}
84

Roger E. Masse's avatar
Roger E. Masse committed
85
static PyObject *
86
imgfile_read(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
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
    char *fname;
    PyObject *rv;
    int xsize, ysize, zsize;
    char *cdatap;
    long *idatap;
    static short rs[8192], gs[8192], bs[8192];
    int x, y;
    IMAGE *image;
    int yfirst, ylast, ystep;

    if ( !PyArg_ParseTuple(args, "s:read", &fname) )
        return NULL;

    if ( (image = imgfile_open(fname)) == NULL )
        return NULL;

    if ( image->colormap != CM_NORMAL ) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can only handle CM_NORMAL images");
        return NULL;
    }
    if ( BPP(image->type) != 1 ) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can't handle imgfiles with bpp!=1");
        return NULL;
    }
    xsize = image->xsize;
    ysize = image->ysize;
    zsize = image->zsize;
    if ( zsize != 1 && zsize != 3) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can only handle 1 or 3 byte pixels");
        return NULL;
    }
    if ( xsize > 8192 ) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can't handle image with > 8192 columns");
        return NULL;
    }

    if ( zsize == 3 ) zsize = 4;
    rv = PyString_FromStringAndSize((char *)NULL, xsize*ysize*zsize);
    if ( rv == NULL ) {
        iclose(image);
        return NULL;
    }
    cdatap = PyString_AsString(rv);
    idatap = (long *)cdatap;

    if (top_to_bottom) {
        yfirst = ysize-1;
        ylast = -1;
        ystep = -1;
    } else {
        yfirst = 0;
        ylast = ysize;
        ystep = 1;
    }
    for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
        if ( zsize == 1 ) {
            getrow(image, rs, y, 0);
            for(x=0; x<xsize; x++ )
                *cdatap++ = rs[x];
        } else {
            getrow(image, rs, y, 0);
            getrow(image, gs, y, 1);
            getrow(image, bs, y, 2);
            for(x=0; x<xsize; x++ )
                *idatap++ = (rs[x] & 0xff)  |
                    ((gs[x] & 0xff)<<8) |
                    ((bs[x] & 0xff)<<16);
        }
    }
    iclose(image);
    if ( error_called ) {
        Py_DECREF(rv);
        return NULL;
    }
    return rv;
Guido van Rossum's avatar
Guido van Rossum committed
171 172
}

173 174
static IMAGE *glob_image;
static long *glob_datap;
175
static int glob_width, glob_z, glob_ysize;
176

Guido van Rossum's avatar
Guido van Rossum committed
177
static void
178
xs_get(short *buf, int y)
179
{
180 181 182 183
    if (top_to_bottom)
        getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
    else
        getrow(glob_image, buf, y, glob_z);
184 185
}

Guido van Rossum's avatar
Guido van Rossum committed
186
static void
187
xs_put_c(short *buf, int y)
188
{
189 190
    char *datap = (char *)glob_datap + y*glob_width;
    int width = glob_width;
191

192 193
    while ( width-- )
        *datap++ = (*buf++) & 0xff;
194 195
}

Guido van Rossum's avatar
Guido van Rossum committed
196
static void
197
xs_put_0(short *buf, int y)
198
{
199 200
    long *datap = glob_datap + y*glob_width;
    int width = glob_width;
201

202 203
    while ( width-- )
        *datap++ = (*buf++) & 0xff;
204
}
Guido van Rossum's avatar
Guido van Rossum committed
205
static void
206
xs_put_12(short *buf, int y)
207
{
208 209
    long *datap = glob_datap + y*glob_width;
    int width = glob_width;
210

211 212
    while ( width-- )
        *datap++ |= ((*buf++) & 0xff) << (glob_z*8);
213 214 215
}

static void
216 217
xscale(IMAGE *image, int xsize, int ysize, int zsize,
       long *datap, int xnew, int ynew, int fmode, double blur)
218
{
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    glob_image = image;
    glob_datap = datap;
    glob_width = xnew;
    glob_ysize = ysize;
    if ( zsize == 1 ) {
        glob_z = 0;
        filterzoom(xs_get, xs_put_c, xsize, ysize,
                   xnew, ynew, fmode, blur);
    } else {
        glob_z = 0;
        filterzoom(xs_get, xs_put_0, xsize, ysize,
                   xnew, ynew, fmode, blur);
        glob_z = 1;
        filterzoom(xs_get, xs_put_12, xsize, ysize,
                   xnew, ynew, fmode, blur);
        glob_z = 2;
        filterzoom(xs_get, xs_put_12, xsize, ysize,
                   xnew, ynew, fmode, blur);
    }
238 239
}

240

Roger E. Masse's avatar
Roger E. Masse committed
241
static PyObject *
242
imgfile_readscaled(PyObject *self, PyObject *args)
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
    char *fname;
    PyObject *rv;
    int xsize, ysize, zsize;
    char *cdatap;
    long *idatap;
    static short rs[8192], gs[8192], bs[8192];
    int x, y;
    int xwtd, ywtd, xorig, yorig;
    float xfac, yfac;
    IMAGE *image;
    char *filter;
    double blur = 1.0;
    int extended;
    int fmode = 0;
    int yfirst, ylast, ystep;

    /*
    ** Parse args. Funny, since arg 4 and 5 are optional
    ** (filter name and blur factor). Also, 4 or 5 arguments indicates
    ** extended scale algorithm in stead of simple-minded pixel drop/dup.
    */
    extended = PyTuple_Size(args) >= 4;
    if ( !PyArg_ParseTuple(args, "sii|sd",
                           &fname, &xwtd, &ywtd, &filter, &blur) )
        return NULL;

    /*
    ** Check parameters, open file and check type, rows, etc.
    */
    if ( extended ) {
        if ( strcmp(filter, "impulse") == 0 )
            fmode = IMPULSE;
        else if ( strcmp( filter, "box") == 0 )
            fmode = BOX;
        else if ( strcmp( filter, "triangle") == 0 )
            fmode = TRIANGLE;
        else if ( strcmp( filter, "quadratic") == 0 )
            fmode = QUADRATIC;
        else if ( strcmp( filter, "gaussian") == 0 )
            fmode = GAUSSIAN;
        else {
            PyErr_SetString(ImgfileError, "Unknown filter type");
            return NULL;
        }
    }

    if ( (image = imgfile_open(fname)) == NULL )
        return NULL;

    if ( image->colormap != CM_NORMAL ) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can only handle CM_NORMAL images");
        return NULL;
    }
    if ( BPP(image->type) != 1 ) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can't handle imgfiles with bpp!=1");
        return NULL;
    }
    xsize = image->xsize;
    ysize = image->ysize;
    zsize = image->zsize;
    if ( zsize != 1 && zsize != 3) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can only handle 1 or 3 byte pixels");
        return NULL;
    }
    if ( xsize > 8192 ) {
        iclose(image);
        PyErr_SetString(ImgfileError,
                        "Can't handle image with > 8192 columns");
        return NULL;
    }

    if ( zsize == 3 ) zsize = 4;
    rv = PyString_FromStringAndSize(NULL, xwtd*ywtd*zsize);
    if ( rv == NULL ) {
        iclose(image);
        return NULL;
    }
    PyFPE_START_PROTECT("readscaled", return 0)
    xfac = (float)xsize/(float)xwtd;
    yfac = (float)ysize/(float)ywtd;
    PyFPE_END_PROTECT(yfac)
    cdatap = PyString_AsString(rv);
    idatap = (long *)cdatap;

    if ( extended ) {
        xscale(image, xsize, ysize, zsize,
               idatap, xwtd, ywtd, fmode, blur);
    } else {
        if (top_to_bottom) {
            yfirst = ywtd-1;
            ylast = -1;
            ystep = -1;
        } else {
            yfirst = 0;
            ylast = ywtd;
            ystep = 1;
        }
        for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
            yorig = (int)(y*yfac);
            if ( zsize == 1 ) {
                getrow(image, rs, yorig, 0);
                for(x=0; x<xwtd; x++ ) {
                    *cdatap++ = rs[(int)(x*xfac)];
                }
            } else {
                getrow(image, rs, yorig, 0);
                getrow(image, gs, yorig, 1);
                getrow(image, bs, yorig, 2);
                for(x=0; x<xwtd; x++ ) {
                    xorig = (int)(x*xfac);
                    *idatap++ = (rs[xorig] & 0xff)  |
                        ((gs[xorig] & 0xff)<<8) |
                        ((bs[xorig] & 0xff)<<16);
                }
            }
        }
    }
    iclose(image);
    if ( error_called ) {
        Py_DECREF(rv);
        return NULL;
    }
    return rv;
373 374
}

Roger E. Masse's avatar
Roger E. Masse committed
375
static PyObject *
376
imgfile_getsizes(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
377
{
378 379 380 381 382 383 384 385 386 387 388 389
    char *fname;
    PyObject *rv;
    IMAGE *image;

    if ( !PyArg_ParseTuple(args, "s:getsizes", &fname) )
        return NULL;

    if ( (image = imgfile_open(fname)) == NULL )
        return NULL;
    rv = Py_BuildValue("(iii)", image->xsize, image->ysize, image->zsize);
    iclose(image);
    return rv;
Guido van Rossum's avatar
Guido van Rossum committed
390 391
}

Roger E. Masse's avatar
Roger E. Masse committed
392
static PyObject *
393
imgfile_write(PyObject *self, PyObject *args)
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
    IMAGE *image;
    char *fname;
    int xsize, ysize, zsize, len;
    char *cdatap;
    long *idatap;
    short rs[8192], gs[8192], bs[8192];
    short r, g, b;
    long rgb;
    int x, y;
    int yfirst, ylast, ystep;


    if ( !PyArg_ParseTuple(args, "ss#iii:write",
                      &fname, &cdatap, &len, &xsize, &ysize, &zsize) )
        return NULL;

    if ( zsize != 1 && zsize != 3 ) {
        PyErr_SetString(ImgfileError,
                        "Can only handle 1 or 3 byte pixels");
        return NULL;
    }
    if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
        PyErr_SetString(ImgfileError, "Data does not match sizes");
        return NULL;
    }
    if ( xsize > 8192 ) {
        PyErr_SetString(ImgfileError,
                        "Can't handle image with > 8192 columns");
        return NULL;
    }

    error_called = 0;
    errno = 0;
    image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize);
    if ( image == 0 ) {
        if ( ! error_called ) {
            if (errno)
                PyErr_SetFromErrno(ImgfileError);
            else
                PyErr_SetString(ImgfileError,
                                "Can't create image file");
        }
        return NULL;
    }

    idatap = (long *)cdatap;

    if (top_to_bottom) {
        yfirst = ysize-1;
        ylast = -1;
        ystep = -1;
    } else {
        yfirst = 0;
        ylast = ysize;
        ystep = 1;
    }
    for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
        if ( zsize == 1 ) {
            for( x=0; x<xsize; x++ )
                rs[x] = *cdatap++;
            putrow(image, rs, y, 0);
        } else {
            for( x=0; x<xsize; x++ ) {
                rgb = *idatap++;
                r = rgb & 0xff;
                g = (rgb >> 8 ) & 0xff;
                b = (rgb >> 16 ) & 0xff;
                rs[x] = r;
                gs[x] = g;
                bs[x] = b;
            }
            putrow(image, rs, y, 0);
            putrow(image, gs, y, 1);
            putrow(image, bs, y, 2);
        }
    }
    iclose(image);
    if ( error_called )
        return NULL;
    Py_INCREF(Py_None);
    return Py_None;

477
}
Guido van Rossum's avatar
Guido van Rossum committed
478

479

Roger E. Masse's avatar
Roger E. Masse committed
480
static PyMethodDef imgfile_methods[] = {
481 482 483 484 485 486
    { "getsizes",       imgfile_getsizes, METH_VARARGS },
    { "read",           imgfile_read, METH_VARARGS },
    { "readscaled",     imgfile_readscaled, METH_VARARGS},
    { "write",          imgfile_write, METH_VARARGS },
    { "ttob",           imgfile_ttob, METH_VARARGS },
    { NULL,             NULL } /* Sentinel */
Guido van Rossum's avatar
Guido van Rossum committed
487 488 489 490
};


void
491
initimgfile(void)
Guido van Rossum's avatar
Guido van Rossum committed
492
{
493 494 495 496 497 498 499 500 501 502 503 504 505
    PyObject *m, *d;

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

    m = Py_InitModule("imgfile", imgfile_methods);
    if (m == NULL)
        return;
    d = PyModule_GetDict(m);
    ImgfileError = PyErr_NewException("imgfile.error", NULL, NULL);
    if (ImgfileError != NULL)
        PyDict_SetItemString(d, "error", ImgfileError);
Guido van Rossum's avatar
Guido van Rossum committed
506
}
Roger E. Masse's avatar
Roger E. Masse committed
507 508 509