annotate Modules/imgfile.c @ 4566:6403c94de50d legacy-trunk

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