~ubuntu-branches/ubuntu/intrepid/graphicsmagick/intrepid

« back to all changes in this revision

Viewing changes to coders/wbmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2006-05-06 16:28:08 UTC
  • Revision ID: james.westby@ubuntu.com-20060506162808-vt2ni3r5nytcszms
Tags: upstream-1.1.7
ImportĀ upstreamĀ versionĀ 1.1.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
% Copyright (C) 2003 GraphicsMagick Group
 
3
% Copyright (C) 2002 ImageMagick Studio
 
4
% Copyright 1991-1999 E. I. du Pont de Nemours and Company
 
5
%
 
6
% This program is covered by multiple licenses, which are described in
 
7
% Copyright.txt. You should have received a copy of Copyright.txt with this
 
8
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
 
9
%
 
10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
11
%                                                                             %
 
12
%                                                                             %
 
13
%                                                                             %
 
14
%                         W   W  BBBB   M   M  PPPP                           %
 
15
%                         W   W  B   B  MM MM  P   P                          %
 
16
%                         W W W  BBBB   M M M  PPPP                           %
 
17
%                         WW WW  B   B  M   M  P                              %
 
18
%                         W   W  BBBB   M   M  P                              %
 
19
%                                                                             %
 
20
%                                                                             %
 
21
%              Read/Write Wireless Bitmap (level 0) Image Format.             %
 
22
%                                                                             %
 
23
%                                                                             %
 
24
%                              Software Design                                %
 
25
%                                John Cristy                                  %
 
26
%                               January 2000                                  %
 
27
%                                                                             %
 
28
%                                                                             %
 
29
%                                                                             %
 
30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
31
%
 
32
%
 
33
*/
 
34
/*
 
35
  Include declarations.
 
36
*/
 
37
#include "magick/studio.h"
 
38
#include "magick/blob.h"
 
39
#include "magick/cache.h"
 
40
#include "magick/magick.h"
 
41
#include "magick/monitor.h"
 
42
#include "magick/utility.h"
 
43
 
 
44
/*
 
45
  Forward declarations.
 
46
*/
 
47
static unsigned int
 
48
  WriteWBMPImage(const ImageInfo *,Image *);
 
49
 
 
50
/*
 
51
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
52
%                                                                             %
 
53
%                                                                             %
 
54
%                                                                             %
 
55
%   R e a d W B M P I m a g e                                                 %
 
56
%                                                                             %
 
57
%                                                                             %
 
58
%                                                                             %
 
59
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
60
%
 
61
%  Method ReadWBMPImage reads a WBMP (level 0) image file and returns it.  It
 
62
%  allocates the memory necessary for the new Image structure and returns a
 
63
%  pointer to the new image.
 
64
%
 
65
%  ReadWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
 
66
%
 
67
%  The format of the ReadWBMPImage method is:
 
68
%
 
69
%      Image *ReadWBMPImage(const ImageInfo *image_info,
 
70
%        ExceptionInfo *exception)
 
71
%
 
72
%  A description of each parameter follows:
 
73
%
 
74
%    o image:  Method ReadWBMPImage returns a pointer to the image after
 
75
%      reading.  A null image is returned if there is a memory shortage or
 
76
%      if the image cannot be read.
 
77
%
 
78
%    o image_info: Specifies a pointer to a ImageInfo structure.
 
79
%
 
80
%    o exception: return any errors or warnings in this structure.
 
81
%
 
82
%
 
83
*/
 
84
 
 
85
static unsigned int WBMPReadInteger(Image *image,unsigned long *value)
 
86
{
 
87
  int
 
88
    byte;
 
89
 
 
90
  *value=0;
 
91
  do
 
92
  {
 
93
    byte=ReadBlobByte(image);
 
94
    if (byte == EOF)
 
95
      return(False);
 
96
    *value<<=7;
 
97
    *value|=(unsigned int) (byte & 0x7f);
 
98
  } while (byte & 0x80);
 
99
  return(True);
 
100
}
 
101
 
 
102
static Image *ReadWBMPImage(const ImageInfo *image_info,
 
103
  ExceptionInfo *exception)
 
104
{
 
105
  Image
 
106
    *image;
 
107
 
 
108
  int
 
109
    byte;
 
110
 
 
111
  long
 
112
    y;
 
113
 
 
114
  register IndexPacket
 
115
    *indexes;
 
116
 
 
117
  register long
 
118
    x;
 
119
 
 
120
  register PixelPacket
 
121
    *q;
 
122
 
 
123
  register long
 
124
    i;
 
125
 
 
126
  unsigned char
 
127
    bit;
 
128
 
 
129
  unsigned int
 
130
    status;
 
131
 
 
132
  unsigned short
 
133
    header;
 
134
 
 
135
  /*
 
136
    Open image file.
 
137
  */
 
138
  assert(image_info != (const ImageInfo *) NULL);
 
139
  assert(image_info->signature == MagickSignature);
 
140
  assert(exception != (ExceptionInfo *) NULL);
 
141
  assert(exception->signature == MagickSignature);
 
142
  image=AllocateImage(image_info);
 
143
  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
 
144
  if (status == False)
 
145
    ThrowReaderException(FileOpenError,UnableToOpenFile,image);
 
146
  if (!ReadBlob(image,2,(char *) &header)) 
 
147
    ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
 
148
  if (header != 0)
 
149
    ThrowReaderException(CoderError,OnlyLevelZerofilesSupported,image);
 
150
  /*
 
151
    Initialize image structure.
 
152
  */
 
153
  if (WBMPReadInteger(image,&image->columns) == False) 
 
154
    ThrowReaderException(CorruptImageError,CorruptImage,image);
 
155
  if (WBMPReadInteger(image,&image->rows) == False) 
 
156
    ThrowReaderException(CorruptImageError,CorruptImage,image);
 
157
  if ((image->columns == 0) || (image->rows == 0))
 
158
    ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
 
159
  for (i=0; i < image->offset; i++)
 
160
    (void) ReadBlobByte(image);
 
161
  if (!AllocateImageColormap(image,2))
 
162
    ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
 
163
  if (image_info->ping)
 
164
    {
 
165
      CloseBlob(image);
 
166
      return(image);
 
167
    }
 
168
  /*
 
169
    Convert bi-level image to pixel packets.
 
170
  */
 
171
  for (y=0; y < (long) image->rows; y++)
 
172
  {
 
173
    q=SetImagePixels(image,0,y,image->columns,1);
 
174
    if (q == (PixelPacket *) NULL)
 
175
      break;
 
176
    indexes=GetIndexes(image);
 
177
    bit=0;
 
178
    byte=0;
 
179
    for (x=0; x < (long) image->columns; x++)
 
180
    {
 
181
      if (bit == 0)
 
182
        {
 
183
          byte=ReadBlobByte(image);
 
184
          if (byte == EOF)
 
185
            ThrowReaderException(CorruptImageError,CorruptImage,image);
 
186
        }
 
187
      indexes[x]=(byte & (0x01 << (7-bit))) ? 1 : 0;
 
188
      bit++;
 
189
      if (bit == 8)
 
190
        bit=0;
 
191
    }
 
192
    if (!SyncImagePixels(image))
 
193
      break;
 
194
    if (QuantumTick(y,image->rows))
 
195
      if (!MagickMonitor(LoadImageText,y,image->rows,exception))
 
196
        break;
 
197
  }
 
198
  SyncImage(image);
 
199
  if (EOFBlob(image))
 
200
    ThrowException(exception,CorruptImageError,UnexpectedEndOfFile,
 
201
      image->filename);
 
202
  CloseBlob(image);
 
203
  return(image);
 
204
}
 
205
 
 
206
/*
 
207
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
208
%                                                                             %
 
209
%                                                                             %
 
210
%                                                                             %
 
211
%   R e g i s t e r W B M P I m a g e                                         %
 
212
%                                                                             %
 
213
%                                                                             %
 
214
%                                                                             %
 
215
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
216
%
 
217
%  Method RegisterWBMPImage adds attributes for the WBMP image format to
 
218
%  the list of supported formats.  The attributes include the image format
 
219
%  tag, a method to read and/or write the format, whether the format
 
220
%  supports the saving of more than one frame to the same file or blob,
 
221
%  whether the format supports native in-memory I/O, and a brief
 
222
%  description of the format.
 
223
%
 
224
%  The format of the RegisterWBMPImage method is:
 
225
%
 
226
%      RegisterWBMPImage(void)
 
227
%
 
228
*/
 
229
ModuleExport void RegisterWBMPImage(void)
 
230
{
 
231
  MagickInfo
 
232
    *entry;
 
233
 
 
234
  entry=SetMagickInfo("WBMP");
 
235
  entry->decoder=(DecoderHandler) ReadWBMPImage;
 
236
  entry->encoder=(EncoderHandler) WriteWBMPImage;
 
237
  entry->adjoin=False;
 
238
  entry->description=AcquireString("Wireless Bitmap (level 0) image");
 
239
  entry->module=AcquireString("WBMP");
 
240
  (void) RegisterMagickInfo(entry);
 
241
}
 
242
 
 
243
/*
 
244
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
245
%                                                                             %
 
246
%                                                                             %
 
247
%                                                                             %
 
248
%   U n r e g i s t e r W B M P I m a g e                                     %
 
249
%                                                                             %
 
250
%                                                                             %
 
251
%                                                                             %
 
252
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
253
%
 
254
%  Method UnregisterWBMPImage removes format registrations made by the
 
255
%  WBMP module from the list of supported formats.
 
256
%
 
257
%  The format of the UnregisterWBMPImage method is:
 
258
%
 
259
%      UnregisterWBMPImage(void)
 
260
%
 
261
*/
 
262
ModuleExport void UnregisterWBMPImage(void)
 
263
{
 
264
  (void) UnregisterMagickInfo("WBMP");
 
265
}
 
266
 
 
267
/*
 
268
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
269
%                                                                             %
 
270
%                                                                             %
 
271
%                                                                             %
 
272
%   W r i t e W B M P I m a g e                                               %
 
273
%                                                                             %
 
274
%                                                                             %
 
275
%                                                                             %
 
276
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
277
%
 
278
%  Method WriteWBMPImage writes an image to a file in the Wireless Bitmap
 
279
%  (level 0) image format.
 
280
%
 
281
%  WriteWBMPImage was contributed by Milan Votava <votava@mageo.cz>.
 
282
%
 
283
%  The format of the WriteWBMPImage method is:
 
284
%
 
285
%      unsigned int WriteWBMPImage(const ImageInfo *image_info,Image *image)
 
286
%
 
287
%  A description of each parameter follows.
 
288
%
 
289
%    o status: Method WriteWBMPImage return True if the image is written.
 
290
%      False is returned is there is a memory shortage or if the image file
 
291
%      fails to write.
 
292
%
 
293
%    o image_info: Specifies a pointer to a ImageInfo structure.
 
294
%
 
295
%    o image:  A pointer to an Image structure.
 
296
%
 
297
%
 
298
*/
 
299
 
 
300
static void WBMPWriteInteger(Image *image,const unsigned long value)
 
301
{
 
302
  int
 
303
    bits,
 
304
    flag,
 
305
    n;
 
306
 
 
307
  register long
 
308
    i;
 
309
 
 
310
  unsigned char
 
311
    buffer[5],
 
312
    octet;
 
313
 
 
314
  n=1;
 
315
  bits=28;
 
316
  flag=False;
 
317
  for(i=4; i >= 0; i--)
 
318
  {
 
319
    octet=(unsigned char) ((value >> bits) & 0x7f);
 
320
    if (!flag && octet)
 
321
      {
 
322
        flag=True;
 
323
        n=i+1;
 
324
      }
 
325
    buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
 
326
    bits-=7;
 
327
  }
 
328
  (void) WriteBlob(image,n,(char *) buffer+5-n);
 
329
}
 
330
 
 
331
static unsigned int WriteWBMPImage(const ImageInfo *image_info,Image *image)
 
332
{
 
333
  long
 
334
    y;
 
335
 
 
336
  register const PixelPacket
 
337
    *p;
 
338
 
 
339
  register IndexPacket
 
340
    *indexes;
 
341
 
 
342
  register long
 
343
    x;
 
344
 
 
345
  unsigned char
 
346
    bit,
 
347
    byte,
 
348
    polarity;
 
349
 
 
350
  unsigned int
 
351
    status;
 
352
 
 
353
  /*
 
354
    Open output image file.
 
355
  */
 
356
  assert(image_info != (const ImageInfo *) NULL);
 
357
  assert(image_info->signature == MagickSignature);
 
358
  assert(image != (Image *) NULL);
 
359
  assert(image->signature == MagickSignature);
 
360
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
 
361
  if (status == False)
 
362
    ThrowWriterException(FileOpenError,UnableToOpenFile,image);
 
363
  TransformColorspace(image,RGBColorspace);
 
364
  /*
 
365
    Convert image to a bi-level image.
 
366
  */
 
367
  SetImageType(image,BilevelType);
 
368
  polarity=PixelIntensityToQuantum(&image->colormap[0]) < (MaxRGB/2);
 
369
  if (image->colors == 2)
 
370
    polarity=PixelIntensityToQuantum(&image->colormap[0]) <
 
371
      PixelIntensityToQuantum(&image->colormap[1]);
 
372
  (void) WriteBlobMSBShort(image,0);
 
373
  WBMPWriteInteger(image,image->columns);
 
374
  WBMPWriteInteger(image,image->rows);
 
375
  for (y=0; y < (long) image->rows; y++)
 
376
  {
 
377
    p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
 
378
    if (p == (const PixelPacket *) NULL)
 
379
      break;
 
380
    indexes=GetIndexes(image);
 
381
    bit=0;
 
382
    byte=0;
 
383
    for (x=0; x < (long) image->columns; x++)
 
384
    {
 
385
      if (indexes[x] == polarity)
 
386
        byte|=0x1 << (7-bit);
 
387
      bit++;
 
388
      if (bit == 8)
 
389
        {
 
390
          (void) WriteBlobByte(image,byte);
 
391
          bit=0;
 
392
          byte=0;
 
393
        }
 
394
    }
 
395
    if (bit != 0)
 
396
      (void) WriteBlobByte(image,byte);
 
397
    if (QuantumTick(y,image->rows))
 
398
      if (!MagickMonitor(SaveImageText,y,image->rows,&image->exception))
 
399
        break;
 
400
  }
 
401
  CloseBlob(image);
 
402
  return(True);
 
403
}