blob: 2d72ec8fb20c2a8fc8f9bb78608496d0871ff062 [file] [log] [blame]
Guido van Rossumaa9de671992-03-04 16:40:03 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumaa9de671992-03-04 16:40:03 +00004
5 All Rights Reserved
6
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossumaa9de671992-03-04 16:40:03 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumaa9de671992-03-04 16:40:03 +000014
15******************************************************************/
16
Guido van Rossuma5f61381992-09-03 20:41:22 +000017/* IMGFILE module - Interface to sgi libimage */
Guido van Rossumaa9de671992-03-04 16:40:03 +000018
Guido van Rossum2c4be641992-06-03 17:06:36 +000019/* XXX This modele should be done better at some point. It should return
Guido van Rossumaa9de671992-03-04 16:40:03 +000020** an object of image file class, and have routines to manipulate these
21** image files in a neater way (so you can get rgb images off a greyscale
22** file, for instance, or do a straight display without having to get the
23** image bits into python, etc).
Jack Jansen3c2eb5c1993-01-19 15:17:13 +000024**
25** Warning: this module is very non-reentrant (esp. the readscaled stuff)
Guido van Rossumaa9de671992-03-04 16:40:03 +000026*/
27
Roger E. Masse7f33e401996-12-20 21:56:08 +000028#include "Python.h"
Guido van Rossumaa9de671992-03-04 16:40:03 +000029
30#include <gl/image.h>
31
Jack Jansen09cbf9a1993-01-19 15:33:13 +000032#include "/usr/people/4Dgifts/iristools/include/izoom.h"
Jack Jansen3c2eb5c1993-01-19 15:17:13 +000033
Guido van Rossum665f9191996-12-09 18:49:42 +000034/* Bunch of missing extern decls; keep gcc -Wall happy... */
35extern void i_seterror();
36extern void iclose();
37extern void filterzoom();
38extern void putrow();
39extern void getrow();
40
Roger E. Masse7f33e401996-12-20 21:56:08 +000041static PyObject * ImgfileError; /* Exception we raise for various trouble */
Guido van Rossumaa9de671992-03-04 16:40:03 +000042
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000043static int top_to_bottom; /* True if we want top-to-bottom images */
Guido van Rossumaa9de671992-03-04 16:40:03 +000044
Guido van Rossuma5f61381992-09-03 20:41:22 +000045/* The image library does not always call the error hander :-(,
46 therefore we have a global variable indicating that it was called.
47 It is cleared by imgfile_open(). */
Guido van Rossumaa9de671992-03-04 16:40:03 +000048
Guido van Rossum3e941971992-03-23 18:21:32 +000049static int error_called;
50
Guido van Rossuma5f61381992-09-03 20:41:22 +000051
52/* The error handler */
53
Guido van Rossuma376cc51996-12-05 23:43:35 +000054static void
55imgfile_error(str)
Roger E. Masse7f33e401996-12-20 21:56:08 +000056 char *str;
Guido van Rossum3e941971992-03-23 18:21:32 +000057{
Roger E. Masse7f33e401996-12-20 21:56:08 +000058 PyErr_SetString(ImgfileError, str);
59 error_called = 1;
60 return; /* To imglib, which will return a failure indicator */
Guido van Rossum3e941971992-03-23 18:21:32 +000061}
Guido van Rossumaa9de671992-03-04 16:40:03 +000062
Guido van Rossuma5f61381992-09-03 20:41:22 +000063
64/* Open an image file and return a pointer to it.
65 Make sure we raise an exception if we fail. */
66
67static IMAGE *
Guido van Rossum2c4be641992-06-03 17:06:36 +000068imgfile_open(fname)
Roger E. Masse7f33e401996-12-20 21:56:08 +000069 char *fname;
Guido van Rossum2c4be641992-06-03 17:06:36 +000070{
Roger E. Masse7f33e401996-12-20 21:56:08 +000071 IMAGE *image;
72 i_seterror(imgfile_error);
73 error_called = 0;
74 errno = 0;
75 if ( (image = iopen(fname, "r")) == NULL ) {
76 /* Error may already be set by imgfile_error */
77 if ( !error_called ) {
78 if (errno)
79 PyErr_SetFromErrno(ImgfileError);
80 else
81 PyErr_SetString(ImgfileError,
82 "Can't open image file");
83 }
84 return NULL;
Guido van Rossumaa9de671992-03-04 16:40:03 +000085 }
Roger E. Masse7f33e401996-12-20 21:56:08 +000086 return image;
87}
88
89static PyObject *
90imgfile_ttob(self, args)
91 PyObject *self;
92PyObject *args;
93{
94 int newval;
95 PyObject *rv;
96
97 if (!PyArg_Parse(args, "i", &newval))
98 return NULL;
99 rv = PyInt_FromLong(top_to_bottom);
100 top_to_bottom = newval;
101 return rv;
102}
103
104static PyObject *
105imgfile_read(self, args)
106 PyObject *self;
107PyObject *args;
108{
109 char *fname;
110 PyObject *rv;
111 int xsize, ysize, zsize;
112 char *cdatap;
113 long *idatap;
114 static short rs[8192], gs[8192], bs[8192];
115 int x, y;
116 IMAGE *image;
117 int yfirst, ylast, ystep;
118
119 if ( !PyArg_Parse(args, "s", &fname) )
120 return NULL;
121
122 if ( (image = imgfile_open(fname)) == NULL )
123 return NULL;
124
125 if ( image->colormap != CM_NORMAL ) {
126 iclose(image);
127 PyErr_SetString(ImgfileError,
128 "Can only handle CM_NORMAL images");
129 return NULL;
130 }
131 if ( BPP(image->type) != 1 ) {
132 iclose(image);
133 PyErr_SetString(ImgfileError,
134 "Can't handle imgfiles with bpp!=1");
135 return NULL;
136 }
137 xsize = image->xsize;
138 ysize = image->ysize;
139 zsize = image->zsize;
140 if ( zsize != 1 && zsize != 3) {
141 iclose(image);
142 PyErr_SetString(ImgfileError,
143 "Can only handle 1 or 3 byte pixels");
144 return NULL;
145 }
146 if ( xsize > 8192 ) {
147 iclose(image);
148 PyErr_SetString(ImgfileError,
149 "Can't handle image with > 8192 columns");
150 return NULL;
151 }
152
153 if ( zsize == 3 ) zsize = 4;
154 rv = PyString_FromStringAndSize((char *)NULL, xsize*ysize*zsize);
155 if ( rv == NULL ) {
156 iclose(image);
157 return NULL;
158 }
159 cdatap = PyString_AsString(rv);
160 idatap = (long *)cdatap;
161
162 if (top_to_bottom) {
163 yfirst = ysize-1;
164 ylast = -1;
165 ystep = -1;
166 } else {
167 yfirst = 0;
168 ylast = ysize;
169 ystep = 1;
170 }
171 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
172 if ( zsize == 1 ) {
173 getrow(image, rs, y, 0);
174 for(x=0; x<xsize; x++ )
175 *cdatap++ = rs[x];
176 } else {
177 getrow(image, rs, y, 0);
178 getrow(image, gs, y, 1);
179 getrow(image, bs, y, 2);
180 for(x=0; x<xsize; x++ )
181 *idatap++ = (rs[x] & 0xff) |
182 ((gs[x] & 0xff)<<8) |
183 ((bs[x] & 0xff)<<16);
184 }
185 }
186 iclose(image);
187 if ( error_called ) {
188 Py_DECREF(rv);
189 return NULL;
190 }
191 return rv;
Guido van Rossumaa9de671992-03-04 16:40:03 +0000192}
193
Guido van Rossum234f9421993-06-17 12:35:49 +0000194static IMAGE *glob_image;
195static long *glob_datap;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000196static int glob_width, glob_z, glob_ysize;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000197
Guido van Rossuma376cc51996-12-05 23:43:35 +0000198static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000199xs_get(buf, y)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000200 short *buf;
201int y;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000202{
Roger E. Masse7f33e401996-12-20 21:56:08 +0000203 if (top_to_bottom)
204 getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
205 else
206 getrow(glob_image, buf, y, glob_z);
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000207}
208
Guido van Rossuma376cc51996-12-05 23:43:35 +0000209static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000210xs_put_c(buf, y)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000211 short *buf;
212int y;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000213{
Roger E. Masse7f33e401996-12-20 21:56:08 +0000214 char *datap = (char *)glob_datap + y*glob_width;
215 int width = glob_width;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000216
Roger E. Masse7f33e401996-12-20 21:56:08 +0000217 while ( width-- )
218 *datap++ = (*buf++) & 0xff;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000219}
220
Guido van Rossuma376cc51996-12-05 23:43:35 +0000221static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000222xs_put_0(buf, y)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000223 short *buf;
224int y;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000225{
Roger E. Masse7f33e401996-12-20 21:56:08 +0000226 long *datap = glob_datap + y*glob_width;
227 int width = glob_width;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000228
Roger E. Masse7f33e401996-12-20 21:56:08 +0000229 while ( width-- )
230 *datap++ = (*buf++) & 0xff;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000231}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000232static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000233xs_put_12(buf, y)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000234 short *buf;
235int y;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000236{
Roger E. Masse7f33e401996-12-20 21:56:08 +0000237 long *datap = glob_datap + y*glob_width;
238 int width = glob_width;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000239
Roger E. Masse7f33e401996-12-20 21:56:08 +0000240 while ( width-- )
241 *datap++ |= ((*buf++) & 0xff) << (glob_z*8);
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000242}
243
244static void
245xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000246 IMAGE *image;
247int xsize, ysize, zsize;
248long *datap;
249int xnew, ynew;
250int fmode;
251double blur;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000252{
Roger E. Masse7f33e401996-12-20 21:56:08 +0000253 glob_image = image;
254 glob_datap = datap;
255 glob_width = xnew;
256 glob_ysize = ysize;
257 if ( zsize == 1 ) {
258 glob_z = 0;
259 filterzoom(xs_get, xs_put_c, xsize, ysize,
260 xnew, ynew, fmode, blur);
261 } else {
262 glob_z = 0;
263 filterzoom(xs_get, xs_put_0, xsize, ysize,
264 xnew, ynew, fmode, blur);
265 glob_z = 1;
266 filterzoom(xs_get, xs_put_12, xsize, ysize,
267 xnew, ynew, fmode, blur);
268 glob_z = 2;
269 filterzoom(xs_get, xs_put_12, xsize, ysize,
270 xnew, ynew, fmode, blur);
271 }
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000272}
273
Guido van Rossuma5f61381992-09-03 20:41:22 +0000274
Roger E. Masse7f33e401996-12-20 21:56:08 +0000275static PyObject *
Guido van Rossum2c4be641992-06-03 17:06:36 +0000276imgfile_readscaled(self, args)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000277 PyObject *self;
278PyObject *args;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000279{
Roger E. Masse7f33e401996-12-20 21:56:08 +0000280 char *fname;
281 PyObject *rv;
282 int xsize, ysize, zsize;
283 char *cdatap;
284 long *idatap;
285 static short rs[8192], gs[8192], bs[8192];
286 int x, y;
287 int xwtd, ywtd, xorig, yorig;
288 float xfac, yfac;
289 int cnt;
290 IMAGE *image;
291 char *filter;
292 double blur;
293 int extended;
294 int fmode = 0;
295 int yfirst, ylast, ystep;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000296
Roger E. Masse7f33e401996-12-20 21:56:08 +0000297 /*
298 ** Parse args. Funny, since arg 4 and 5 are optional
299 ** (filter name and blur factor). Also, 4 or 5 arguments indicates
300 ** extended scale algorithm in stead of simple-minded pixel drop/dup.
301 */
302 extended = 0;
303 cnt = PyTuple_Size(args);
304 if ( cnt == 5 ) {
305 extended = 1;
306 if ( !PyArg_Parse(args, "(siisd)",
307 &fname, &xwtd, &ywtd, &filter, &blur) )
308 return NULL;
309 } else if ( cnt == 4 ) {
310 extended = 1;
311 if ( !PyArg_Parse(args, "(siis)",
312 &fname, &xwtd, &ywtd, &filter) )
313 return NULL;
314 blur = 1.0;
315 } else if ( !PyArg_Parse(args, "(sii)", &fname, &xwtd, &ywtd) )
316 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000317
Roger E. Masse7f33e401996-12-20 21:56:08 +0000318 /*
319 ** Check parameters, open file and check type, rows, etc.
320 */
321 if ( extended ) {
322 if ( strcmp(filter, "impulse") == 0 )
323 fmode = IMPULSE;
324 else if ( strcmp( filter, "box") == 0 )
325 fmode = BOX;
326 else if ( strcmp( filter, "triangle") == 0 )
327 fmode = TRIANGLE;
328 else if ( strcmp( filter, "quadratic") == 0 )
329 fmode = QUADRATIC;
330 else if ( strcmp( filter, "gaussian") == 0 )
331 fmode = GAUSSIAN;
332 else {
333 PyErr_SetString(ImgfileError, "Unknown filter type");
334 return NULL;
335 }
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000336 }
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000337
Roger E. Masse7f33e401996-12-20 21:56:08 +0000338 if ( (image = imgfile_open(fname)) == NULL )
339 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000340
Roger E. Masse7f33e401996-12-20 21:56:08 +0000341 if ( image->colormap != CM_NORMAL ) {
342 iclose(image);
343 PyErr_SetString(ImgfileError,
344 "Can only handle CM_NORMAL images");
345 return NULL;
346 }
347 if ( BPP(image->type) != 1 ) {
348 iclose(image);
349 PyErr_SetString(ImgfileError,
350 "Can't handle imgfiles with bpp!=1");
351 return NULL;
352 }
353 xsize = image->xsize;
354 ysize = image->ysize;
355 zsize = image->zsize;
356 if ( zsize != 1 && zsize != 3) {
357 iclose(image);
358 PyErr_SetString(ImgfileError,
359 "Can only handle 1 or 3 byte pixels");
360 return NULL;
361 }
362 if ( xsize > 8192 ) {
363 iclose(image);
364 PyErr_SetString(ImgfileError,
365 "Can't handle image with > 8192 columns");
366 return NULL;
367 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000368
Roger E. Masse7f33e401996-12-20 21:56:08 +0000369 if ( zsize == 3 ) zsize = 4;
370 rv = PyString_FromStringAndSize(NULL, xwtd*ywtd*zsize);
371 if ( rv == NULL ) {
372 iclose(image);
373 return NULL;
374 }
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000375 PyFPE_START_PROTECT("readscaled", return 0)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000376 xfac = (float)xsize/(float)xwtd;
377 yfac = (float)ysize/(float)ywtd;
Guido van Rossum45b83911997-03-14 04:32:50 +0000378 PyFPE_END_PROTECT(yfac)
Roger E. Masse7f33e401996-12-20 21:56:08 +0000379 cdatap = PyString_AsString(rv);
380 idatap = (long *)cdatap;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000381
Roger E. Masse7f33e401996-12-20 21:56:08 +0000382 if ( extended ) {
383 xscale(image, xsize, ysize, zsize,
384 idatap, xwtd, ywtd, fmode, blur);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000385 } else {
Roger E. Masse7f33e401996-12-20 21:56:08 +0000386 if (top_to_bottom) {
387 yfirst = ywtd-1;
388 ylast = -1;
389 ystep = -1;
390 } else {
391 yfirst = 0;
392 ylast = ywtd;
393 ystep = 1;
394 }
395 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
396 yorig = (int)(y*yfac);
397 if ( zsize == 1 ) {
398 getrow(image, rs, yorig, 0);
399 for(x=0; x<xwtd; x++ ) {
400 *cdatap++ = rs[(int)(x*xfac)];
401 }
402 } else {
403 getrow(image, rs, yorig, 0);
404 getrow(image, gs, yorig, 1);
405 getrow(image, bs, yorig, 2);
406 for(x=0; x<xwtd; x++ ) {
407 xorig = (int)(x*xfac);
408 *idatap++ = (rs[xorig] & 0xff) |
409 ((gs[xorig] & 0xff)<<8) |
410 ((bs[xorig] & 0xff)<<16);
411 }
412 }
413 }
414 }
415 iclose(image);
416 if ( error_called ) {
417 Py_DECREF(rv);
418 return NULL;
419 }
420 return rv;
421}
422
423static PyObject *
424imgfile_getsizes(self, args)
425 PyObject *self;
426PyObject *args;
427{
428 char *fname;
429 PyObject *rv;
430 IMAGE *image;
431
432 if ( !PyArg_Parse(args, "s", &fname) )
433 return NULL;
434
435 if ( (image = imgfile_open(fname)) == NULL )
436 return NULL;
437 rv = Py_BuildValue("(iii)", image->xsize, image->ysize, image->zsize);
438 iclose(image);
439 return rv;
440}
441
442static PyObject *
443imgfile_write(self, args)
444 PyObject *self;
445PyObject *args;
446{
447 IMAGE *image;
448 char *fname;
449 int xsize, ysize, zsize, len;
450 char *cdatap;
451 long *idatap;
452 short rs[8192], gs[8192], bs[8192];
453 short r, g, b;
454 long rgb;
455 int x, y;
456 int yfirst, ylast, ystep;
457
458
459 if ( !PyArg_Parse(args, "(ss#iii)",
460 &fname, &cdatap, &len, &xsize, &ysize, &zsize) )
461 return NULL;
462
463 if ( zsize != 1 && zsize != 3 ) {
464 PyErr_SetString(ImgfileError,
465 "Can only handle 1 or 3 byte pixels");
466 return NULL;
467 }
468 if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
469 PyErr_SetString(ImgfileError, "Data does not match sizes");
470 return NULL;
471 }
472 if ( xsize > 8192 ) {
473 PyErr_SetString(ImgfileError,
474 "Can't handle image with > 8192 columns");
475 return NULL;
476 }
477
478 error_called = 0;
479 errno = 0;
480 image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize);
481 if ( image == 0 ) {
482 if ( ! error_called ) {
483 if (errno)
484 PyErr_SetFromErrno(ImgfileError);
485 else
486 PyErr_SetString(ImgfileError,
487 "Can't create image file");
488 }
489 return NULL;
490 }
491
492 idatap = (long *)cdatap;
493
494 if (top_to_bottom) {
495 yfirst = ysize-1;
496 ylast = -1;
497 ystep = -1;
498 } else {
499 yfirst = 0;
500 ylast = ysize;
501 ystep = 1;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000502 }
503 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
Roger E. Masse7f33e401996-12-20 21:56:08 +0000504 if ( zsize == 1 ) {
505 for( x=0; x<xsize; x++ )
506 rs[x] = *cdatap++;
507 putrow(image, rs, y, 0);
508 } else {
509 for( x=0; x<xsize; x++ ) {
510 rgb = *idatap++;
511 r = rgb & 0xff;
512 g = (rgb >> 8 ) & 0xff;
513 b = (rgb >> 16 ) & 0xff;
514 rs[x] = r;
515 gs[x] = g;
516 bs[x] = b;
517 }
518 putrow(image, rs, y, 0);
519 putrow(image, gs, y, 1);
520 putrow(image, bs, y, 2);
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000521 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000522 }
Roger E. Masse7f33e401996-12-20 21:56:08 +0000523 iclose(image);
524 if ( error_called )
525 return NULL;
526 Py_INCREF(Py_None);
527 return Py_None;
Jack Jansen3accf981992-08-20 11:54:27 +0000528
529}
Guido van Rossumaa9de671992-03-04 16:40:03 +0000530
Guido van Rossuma5f61381992-09-03 20:41:22 +0000531
Roger E. Masse7f33e401996-12-20 21:56:08 +0000532static PyMethodDef imgfile_methods[] = {
533 { "getsizes", imgfile_getsizes },
534 { "read", imgfile_read },
535 { "readscaled", imgfile_readscaled, 1},
536 { "write", imgfile_write },
537 { "ttob", imgfile_ttob },
538 { NULL, NULL } /* Sentinel */
Guido van Rossumaa9de671992-03-04 16:40:03 +0000539};
540
541
542void
543initimgfile()
544{
Roger E. Masse7f33e401996-12-20 21:56:08 +0000545 PyObject *m, *d;
546 m = Py_InitModule("imgfile", imgfile_methods);
547 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000548 ImgfileError = PyErr_NewException("imgfile.error", NULL, NULL);
549 if (ImgfileError != NULL)
550 PyDict_SetItemString(d, "error", ImgfileError);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000551}
Roger E. Masse7f33e401996-12-20 21:56:08 +0000552
553
554