~asdfghjkl-deactivatedaccount1/python-imaging/gif-fix

« back to all changes in this revision

Viewing changes to PIL/PalmImagePlugin.py

  • Committer: effbot
  • Date: 2006-03-01 19:16:19 UTC
  • Revision ID: svn-v4:be285980-f00d-0410-a9fe-d4747b46ecd0:pil:21
Load Imaging-1.1.4 into pil.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# The Python Imaging Library.
 
3
# $Id: //modules/pil/PIL/PalmImagePlugin.py#2 $
 
4
#
 
5
# Palm pixmap image handling
 
6
#
 
7
#
 
8
 
 
9
__version__ = "1.0"
 
10
 
 
11
import Image, ImageFile
 
12
import StringIO
 
13
 
 
14
_Palm8BitColormapValues = (
 
15
    ( 255, 255, 255 ), ( 255, 204, 255 ), ( 255, 153, 255 ), ( 255, 102, 255 ),
 
16
    ( 255,  51, 255 ), ( 255,   0, 255 ), ( 255, 255, 204 ), ( 255, 204, 204 ),
 
17
    ( 255, 153, 204 ), ( 255, 102, 204 ), ( 255,  51, 204 ), ( 255,   0, 204 ),
 
18
    ( 255, 255, 153 ), ( 255, 204, 153 ), ( 255, 153, 153 ), ( 255, 102, 153 ),
 
19
    ( 255,  51, 153 ), ( 255,   0, 153 ), ( 204, 255, 255 ), ( 204, 204, 255 ),
 
20
    ( 204, 153, 255 ), ( 204, 102, 255 ), ( 204,  51, 255 ), ( 204,   0, 255 ),
 
21
    ( 204, 255, 204 ), ( 204, 204, 204 ), ( 204, 153, 204 ), ( 204, 102, 204 ),
 
22
    ( 204,  51, 204 ), ( 204,   0, 204 ), ( 204, 255, 153 ), ( 204, 204, 153 ),
 
23
    ( 204, 153, 153 ), ( 204, 102, 153 ), ( 204,  51, 153 ), ( 204,   0, 153 ),
 
24
    ( 153, 255, 255 ), ( 153, 204, 255 ), ( 153, 153, 255 ), ( 153, 102, 255 ),
 
25
    ( 153,  51, 255 ), ( 153,   0, 255 ), ( 153, 255, 204 ), ( 153, 204, 204 ),
 
26
    ( 153, 153, 204 ), ( 153, 102, 204 ), ( 153,  51, 204 ), ( 153,   0, 204 ),
 
27
    ( 153, 255, 153 ), ( 153, 204, 153 ), ( 153, 153, 153 ), ( 153, 102, 153 ),
 
28
    ( 153,  51, 153 ), ( 153,   0, 153 ), ( 102, 255, 255 ), ( 102, 204, 255 ),
 
29
    ( 102, 153, 255 ), ( 102, 102, 255 ), ( 102,  51, 255 ), ( 102,   0, 255 ),
 
30
    ( 102, 255, 204 ), ( 102, 204, 204 ), ( 102, 153, 204 ), ( 102, 102, 204 ),
 
31
    ( 102,  51, 204 ), ( 102,   0, 204 ), ( 102, 255, 153 ), ( 102, 204, 153 ),
 
32
    ( 102, 153, 153 ), ( 102, 102, 153 ), ( 102,  51, 153 ), ( 102,   0, 153 ),
 
33
    (  51, 255, 255 ), (  51, 204, 255 ), (  51, 153, 255 ), (  51, 102, 255 ),
 
34
    (  51,  51, 255 ), (  51,   0, 255 ), (  51, 255, 204 ), (  51, 204, 204 ),
 
35
    (  51, 153, 204 ), (  51, 102, 204 ), (  51,  51, 204 ), (  51,   0, 204 ),
 
36
    (  51, 255, 153 ), (  51, 204, 153 ), (  51, 153, 153 ), (  51, 102, 153 ),
 
37
    (  51,  51, 153 ), (  51,   0, 153 ), (   0, 255, 255 ), (   0, 204, 255 ),
 
38
    (   0, 153, 255 ), (   0, 102, 255 ), (   0,  51, 255 ), (   0,   0, 255 ),
 
39
    (   0, 255, 204 ), (   0, 204, 204 ), (   0, 153, 204 ), (   0, 102, 204 ),
 
40
    (   0,  51, 204 ), (   0,   0, 204 ), (   0, 255, 153 ), (   0, 204, 153 ),
 
41
    (   0, 153, 153 ), (   0, 102, 153 ), (   0,  51, 153 ), (   0,   0, 153 ),
 
42
    ( 255, 255, 102 ), ( 255, 204, 102 ), ( 255, 153, 102 ), ( 255, 102, 102 ),
 
43
    ( 255,  51, 102 ), ( 255,   0, 102 ), ( 255, 255,  51 ), ( 255, 204,  51 ),
 
44
    ( 255, 153,  51 ), ( 255, 102,  51 ), ( 255,  51,  51 ), ( 255,   0,  51 ),
 
45
    ( 255, 255,   0 ), ( 255, 204,   0 ), ( 255, 153,   0 ), ( 255, 102,   0 ),
 
46
    ( 255,  51,   0 ), ( 255,   0,   0 ), ( 204, 255, 102 ), ( 204, 204, 102 ),
 
47
    ( 204, 153, 102 ), ( 204, 102, 102 ), ( 204,  51, 102 ), ( 204,   0, 102 ),
 
48
    ( 204, 255,  51 ), ( 204, 204,  51 ), ( 204, 153,  51 ), ( 204, 102,  51 ),
 
49
    ( 204,  51,  51 ), ( 204,   0,  51 ), ( 204, 255,   0 ), ( 204, 204,   0 ),
 
50
    ( 204, 153,   0 ), ( 204, 102,   0 ), ( 204,  51,   0 ), ( 204,   0,   0 ),
 
51
    ( 153, 255, 102 ), ( 153, 204, 102 ), ( 153, 153, 102 ), ( 153, 102, 102 ),
 
52
    ( 153,  51, 102 ), ( 153,   0, 102 ), ( 153, 255,  51 ), ( 153, 204,  51 ),
 
53
    ( 153, 153,  51 ), ( 153, 102,  51 ), ( 153,  51,  51 ), ( 153,   0,  51 ),
 
54
    ( 153, 255,   0 ), ( 153, 204,   0 ), ( 153, 153,   0 ), ( 153, 102,   0 ),
 
55
    ( 153,  51,   0 ), ( 153,   0,   0 ), ( 102, 255, 102 ), ( 102, 204, 102 ),
 
56
    ( 102, 153, 102 ), ( 102, 102, 102 ), ( 102,  51, 102 ), ( 102,   0, 102 ),
 
57
    ( 102, 255,  51 ), ( 102, 204,  51 ), ( 102, 153,  51 ), ( 102, 102,  51 ),
 
58
    ( 102,  51,  51 ), ( 102,   0,  51 ), ( 102, 255,   0 ), ( 102, 204,   0 ),
 
59
    ( 102, 153,   0 ), ( 102, 102,   0 ), ( 102,  51,   0 ), ( 102,   0,   0 ),
 
60
    (  51, 255, 102 ), (  51, 204, 102 ), (  51, 153, 102 ), (  51, 102, 102 ),
 
61
    (  51,  51, 102 ), (  51,   0, 102 ), (  51, 255,  51 ), (  51, 204,  51 ),
 
62
    (  51, 153,  51 ), (  51, 102,  51 ), (  51,  51,  51 ), (  51,   0,  51 ),
 
63
    (  51, 255,   0 ), (  51, 204,   0 ), (  51, 153,   0 ), (  51, 102,   0 ),
 
64
    (  51,  51,   0 ), (  51,   0,   0 ), (   0, 255, 102 ), (   0, 204, 102 ),
 
65
    (   0, 153, 102 ), (   0, 102, 102 ), (   0,  51, 102 ), (   0,   0, 102 ),
 
66
    (   0, 255,  51 ), (   0, 204,  51 ), (   0, 153,  51 ), (   0, 102,  51 ),
 
67
    (   0,  51,  51 ), (   0,   0,  51 ), (   0, 255,   0 ), (   0, 204,   0 ),
 
68
    (   0, 153,   0 ), (   0, 102,   0 ), (   0,  51,   0 ), (  17,  17,  17 ),
 
69
    (  34,  34,  34 ), (  68,  68,  68 ), (  85,  85,  85 ), ( 119, 119, 119 ),
 
70
    ( 136, 136, 136 ), ( 170, 170, 170 ), ( 187, 187, 187 ), ( 221, 221, 221 ),
 
71
    ( 238, 238, 238 ), ( 192, 192, 192 ), ( 128,   0,   0 ), ( 128,   0, 128 ),
 
72
    (   0, 128,   0 ), (   0, 128, 128 ), (   0,   0,   0 ), (   0,   0,   0 ),
 
73
    (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ),
 
74
    (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ),
 
75
    (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ),
 
76
    (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ),
 
77
    (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ),
 
78
    (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ), (   0,   0,   0 ))
 
79
 
 
80
# so build a prototype image to be used for palette resampling
 
81
def build_prototype_image():
 
82
    image = Image.new("L", (1,len(_Palm8BitColormapValues),))
 
83
    image.putdata(range(len(_Palm8BitColormapValues)))
 
84
    palettedata = ()
 
85
    for i in range(len(_Palm8BitColormapValues)):
 
86
        palettedata = palettedata + _Palm8BitColormapValues[i]
 
87
    for i in range(256 - len(_Palm8BitColormapValues)):
 
88
        palettedata = palettedata + (0, 0, 0)
 
89
    image.putpalette(palettedata)
 
90
    return image
 
91
 
 
92
Palm8BitColormapImage = build_prototype_image()
 
93
 
 
94
# OK, we now have in Palm8BitColormapImage, a "P"-mode image with the right palette
 
95
#
 
96
# --------------------------------------------------------------------
 
97
 
 
98
_FLAGS = {
 
99
    "custom-colormap": 0x4000,
 
100
    "is-compressed":   0x8000,
 
101
    "has-transparent": 0x2000,
 
102
    }
 
103
 
 
104
_COMPRESSION_TYPES = {
 
105
    "none":     0xFF,
 
106
    "rle":      0x01,
 
107
    "scanline": 0x00,
 
108
    }
 
109
 
 
110
def o16b(i):
 
111
    return chr(i>>8&255) + chr(i&255)
 
112
 
 
113
#
 
114
# --------------------------------------------------------------------
 
115
 
 
116
def _save(im, fp, filename, check=0):
 
117
 
 
118
    if im.mode == "P":
 
119
 
 
120
        # we assume this is a color Palm image with the standard colormap,
 
121
        # unless the "info" dict has a "custom-colormap" field
 
122
 
 
123
        rawmode = "P"
 
124
        bpp = 8
 
125
        version = 1
 
126
 
 
127
    elif im.mode == "L" and im.encoderinfo.has_key("bpp") and im.encoderinfo["bpp"] in (1, 2, 4):
 
128
 
 
129
        # this is 8-bit grayscale, so we shift it to get the high-order bits, and invert it because
 
130
        # Palm does greyscale from white (0) to black (1)
 
131
        bpp = im.encoderinfo["bpp"]
 
132
        im = im.point(lambda x, shift=8-bpp, maxval=(1 << bpp)-1: maxval - (x >> shift))
 
133
        # we ignore the palette here
 
134
        im.mode = "P"
 
135
        rawmode = "P;" + str(bpp)
 
136
        version = 1
 
137
 
 
138
    elif im.mode == "L" and im.info.has_key("bpp") and im.info["bpp"] in (1, 2, 4):
 
139
 
 
140
        # here we assume that even though the inherent mode is 8-bit grayscale, only
 
141
        # the lower bpp bits are significant.  We invert them to match the Palm.
 
142
        bpp = im.info["bpp"]
 
143
        im = im.point(lambda x, maxval=(1 << bpp)-1: maxval - (x & maxval))
 
144
        # we ignore the palette here
 
145
        im.mode = "P"
 
146
        rawmode = "P;" + str(bpp)
 
147
        version = 1
 
148
 
 
149
    elif im.mode == "1":
 
150
 
 
151
        # monochrome -- write it inverted, as is the Palm standard
 
152
        rawmode = "1;I"
 
153
        bpp = 1
 
154
        version = 0
 
155
 
 
156
    else:
 
157
 
 
158
        raise IOError, "cannot write mode %s as Palm" % im.mode
 
159
 
 
160
    if check:
 
161
        return check
 
162
 
 
163
    #
 
164
    # make sure image data is available
 
165
    im.load()
 
166
 
 
167
    # write header
 
168
 
 
169
    cols = im.size[0]
 
170
    rows = im.size[1]
 
171
 
 
172
    rowbytes = ((cols + (16/bpp - 1)) / (16 / bpp)) * 2;
 
173
    transparent_index = 0
 
174
    compression_type = _COMPRESSION_TYPES["none"]
 
175
 
 
176
    flags = 0;
 
177
    if im.mode == "P" and im.info.has_key("custom-colormap"):
 
178
        flags = flags & _FLAGS["custom-colormap"]
 
179
        colormapsize = 4 * 256 + 2;
 
180
        colormapmode = im.palette.mode
 
181
        colormap = im.getdata().getpalette()
 
182
    else:
 
183
        colormapsize = 0
 
184
 
 
185
    if im.info.has_key("offset"):
 
186
        offset = (rowbytes * rows + 16 + 3 + colormapsize) / 4;
 
187
    else:
 
188
        offset = 0
 
189
 
 
190
    fp.write(o16b(cols) + o16b(rows) + o16b(rowbytes) + o16b(flags))
 
191
    fp.write(chr(bpp))
 
192
    fp.write(chr(version))
 
193
    fp.write(o16b(offset))
 
194
    fp.write(chr(transparent_index))
 
195
    fp.write(chr(compression_type))
 
196
    fp.write(o16b(0))   # reserved by Palm
 
197
 
 
198
    # now write colormap if necessary
 
199
 
 
200
    if colormapsize > 0:
 
201
        fp.write(o16b(256))
 
202
        for i in range(256):
 
203
            fp.write(chr(i))
 
204
            if colormapmode == 'RGB':
 
205
                fp.write(chr(colormap[3 * i]) + chr(colormap[3 * i + 1]) + chr(colormap[3 * i + 2]))
 
206
            elif colormapmode == 'RGBA':
 
207
                fp.write(chr(colormap[4 * i]) + chr(colormap[4 * i + 1]) + chr(colormap[4 * i + 2]))
 
208
 
 
209
    # now convert data to raw form
 
210
    ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, rowbytes, 1))])
 
211
 
 
212
    fp.flush()
 
213
 
 
214
 
 
215
#
 
216
# --------------------------------------------------------------------
 
217
 
 
218
Image.register_save("Palm", _save)
 
219
 
 
220
Image.register_extension("Palm", ".palm")
 
221
 
 
222
Image.register_mime("Palm", "image/palm")