blob: dd24021349dfc116a97fb5211e7eca2f18171f77 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% OOO TTTTT BBBB %
7% O O T B B %
8% O O T BBBB %
9% O O T B B %
10% OOO T BBBB %
11% %
12% %
13% Read/Write On-The-Air Image Format %
14% %
15% Software Design %
16% John Cristy %
17% January 2000 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38/*
39 Include declarations.
40*/
41#include "magick/studio.h"
42#include "magick/blob.h"
43#include "magick/blob-private.h"
44#include "magick/cache.h"
45#include "magick/color-private.h"
cristye7e40552010-04-24 21:34:22 +000046#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000047#include "magick/colorspace.h"
48#include "magick/exception.h"
49#include "magick/exception-private.h"
50#include "magick/image.h"
51#include "magick/image-private.h"
52#include "magick/list.h"
53#include "magick/magick.h"
54#include "magick/memory_.h"
55#include "magick/monitor.h"
56#include "magick/monitor-private.h"
57#include "magick/quantum-private.h"
58#include "magick/static.h"
59#include "magick/string_.h"
60#include "magick/module.h"
61
62/*
63 Forward declarations.
64*/
65static MagickBooleanType
66 WriteOTBImage(const ImageInfo *,Image *);
67
68/*
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70% %
71% %
72% %
73% R e a d O T B I m a g e %
74% %
75% %
76% %
77%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78%
79% ReadOTBImage() reads a on-the-air (level 0) bitmap and returns it. It
80% allocates the memory necessary for the new Image structure and returns a
81% pointer to the new image.
82%
83% The format of the ReadOTBImage method is:
84%
85% Image *ReadOTBImage(const ImageInfo *image_info,ExceptionInfo *exception)
86%
87% A description of each parameter follows:
88%
89% o image_info: the image info.
90%
91% o exception: return any errors or warnings in this structure.
92%
93%
94*/
95static Image *ReadOTBImage(const ImageInfo *image_info,ExceptionInfo *exception)
96{
97#define GetBit(a,i) (((a) >> (i)) & 1L)
98
99 Image
100 *image;
101
102 int
103 byte;
104
cristy3ed852e2009-09-05 21:47:34 +0000105 MagickBooleanType
106 status;
107
108 register IndexPacket
109 *indexes;
110
cristybb503372010-05-27 20:51:26 +0000111 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000112 x;
113
114 register PixelPacket
115 *q;
116
cristyaff6d802011-04-26 01:46:31 +0000117 ssize_t
118 y;
119
cristy3ed852e2009-09-05 21:47:34 +0000120 unsigned char
121 bit,
122 info,
123 depth;
124
125 /*
126 Open image file.
127 */
128 assert(image_info != (const ImageInfo *) NULL);
129 assert(image_info->signature == MagickSignature);
130 if (image_info->debug != MagickFalse)
131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
132 image_info->filename);
133 assert(exception != (ExceptionInfo *) NULL);
134 assert(exception->signature == MagickSignature);
135 image=AcquireImage(image_info);
136 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
137 if (status == MagickFalse)
138 {
139 image=DestroyImageList(image);
140 return((Image *) NULL);
141 }
142 /*
143 Initialize image structure.
144 */
145 info=(unsigned char) ReadBlobByte(image);
146 if (GetBit(info,4) == 0)
147 {
cristybb503372010-05-27 20:51:26 +0000148 image->columns=(size_t) ReadBlobByte(image);
149 image->rows=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000150 }
151 else
152 {
cristybb503372010-05-27 20:51:26 +0000153 image->columns=(size_t) ReadBlobMSBShort(image);
154 image->rows=(size_t) ReadBlobMSBShort(image);
cristy3ed852e2009-09-05 21:47:34 +0000155 }
156 if ((image->columns == 0) || (image->rows == 0))
157 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
158 depth=(unsigned char) ReadBlobByte(image);
159 if (depth != 1)
160 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
161 if (AcquireImageColormap(image,2) == MagickFalse)
162 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
163 if (image_info->ping != MagickFalse)
164 {
165 (void) CloseBlob(image);
166 return(GetFirstImageInList(image));
167 }
168 /*
169 Convert bi-level image to pixel packets.
170 */
cristybb503372010-05-27 20:51:26 +0000171 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000172 {
173 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
174 if (q == (PixelPacket *) NULL)
175 break;
176 indexes=GetAuthenticIndexQueue(image);
177 bit=0;
178 byte=0;
cristybb503372010-05-27 20:51:26 +0000179 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000180 {
181 if (bit == 0)
182 {
183 byte=ReadBlobByte(image);
184 if (byte == EOF)
185 ThrowReaderException(CorruptImageError,"CorruptImage");
186 }
cristyef618312011-06-25 12:26:44 +0000187 SetPixelIndex(indexes+x,(byte & (0x01 << (7-bit))) ?
cristyaff6d802011-04-26 01:46:31 +0000188 0x00 : 0x01);
cristy3ed852e2009-09-05 21:47:34 +0000189 bit++;
190 if (bit == 8)
191 bit=0;
192 }
193 if (SyncAuthenticPixels(image,exception) == MagickFalse)
194 break;
195 if (image->previous == (Image *) NULL)
196 {
cristycee97112010-05-28 00:44:52 +0000197 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
198 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000199 if (status == MagickFalse)
200 break;
201 }
202 }
203 (void) SyncImage(image);
204 if (EOFBlob(image) != MagickFalse)
205 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
206 image->filename);
207 (void) CloseBlob(image);
208 return(GetFirstImageInList(image));
209}
210
211/*
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213% %
214% %
215% %
216% R e g i s t e r O T B I m a g e %
217% %
218% %
219% %
220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221%
222% RegisterOTBImage() adds attributes for the OTB image format to
223% the list of supported formats. The attributes include the image format
224% tag, a method to read and/or write the format, whether the format
225% supports the saving of more than one frame to the same file or blob,
226% whether the format supports native in-memory I/O, and a brief
227% description of the format.
228%
229% The format of the RegisterOTBImage method is:
230%
cristybb503372010-05-27 20:51:26 +0000231% size_t RegisterOTBImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000232%
233*/
cristybb503372010-05-27 20:51:26 +0000234ModuleExport size_t RegisterOTBImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000235{
236 MagickInfo
237 *entry;
238
239 entry=SetMagickInfo("OTB");
240 entry->decoder=(DecodeImageHandler *) ReadOTBImage;
241 entry->encoder=(EncodeImageHandler *) WriteOTBImage;
242 entry->adjoin=MagickFalse;
243 entry->description=ConstantString("On-the-air bitmap");
244 entry->module=ConstantString("OTB");
245 (void) RegisterMagickInfo(entry);
246 return(MagickImageCoderSignature);
247}
248
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
254% U n r e g i s t e r O T B I m a g e %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% UnregisterOTBImage() removes format registrations made by the
261% OTB module from the list of supported formats.
262%
263% The format of the UnregisterOTBImage method is:
264%
265% UnregisterOTBImage(void)
266%
267*/
268ModuleExport void UnregisterOTBImage(void)
269{
270 (void) UnregisterMagickInfo("OTB");
271}
272
273/*
274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275% %
276% %
277% %
278% W r i t e O T B I m a g e %
279% %
280% %
281% %
282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283%
284% WriteOTBImage() writes an image to a file in the On-the-air Bitmap
285% (level 0) image format.
286%
287% The format of the WriteOTBImage method is:
288%
289% MagickBooleanType WriteOTBImage(const ImageInfo *image_info,Image *image)
290%
291% A description of each parameter follows.
292%
293% o image_info: the image info.
294%
295% o image: The image.
296%
297%
298*/
299static MagickBooleanType WriteOTBImage(const ImageInfo *image_info,Image *image)
300{
301#define SetBit(a,i,set) \
302 a=(unsigned char) ((set) ? (a) | (1L << (i)) : (a) & ~(1L << (i)))
303
cristy3ed852e2009-09-05 21:47:34 +0000304 MagickBooleanType
305 status;
306
307 register const PixelPacket
308 *p;
309
cristybb503372010-05-27 20:51:26 +0000310 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000311 x;
312
cristyaff6d802011-04-26 01:46:31 +0000313 ssize_t
314 y;
315
cristy3ed852e2009-09-05 21:47:34 +0000316 unsigned char
317 bit,
318 byte,
319 info;
320
321 /*
322 Open output image file.
323 */
324 assert(image_info != (const ImageInfo *) NULL);
325 assert(image_info->signature == MagickSignature);
326 assert(image != (Image *) NULL);
327 assert(image->signature == MagickSignature);
328 if (image->debug != MagickFalse)
329 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
330 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
331 if (status == MagickFalse)
332 return(status);
333 if (image->colorspace != RGBColorspace)
334 (void) TransformImageColorspace(image,RGBColorspace);
335 /*
336 Convert image to a bi-level image.
337 */
338 (void) SetImageType(image,BilevelType);
339 info=0;
340 if ((image->columns >= 256) || (image->rows >= 256))
341 SetBit(info,4,1);
342 (void) WriteBlobByte(image,info);
343 if ((image->columns >= 256) || (image->rows >= 256))
344 {
345 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
346 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
347 }
348 else
349 {
350 (void) WriteBlobByte(image,(unsigned char) image->columns);
351 (void) WriteBlobByte(image,(unsigned char) image->rows);
352 }
353 (void) WriteBlobByte(image,1); /* depth */
cristybb503372010-05-27 20:51:26 +0000354 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000355 {
356 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
357 if (p == (const PixelPacket *) NULL)
358 break;
359 bit=0;
360 byte=0;
cristybb503372010-05-27 20:51:26 +0000361 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000362 {
363 if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0))
364 byte|=0x1 << (7-bit);
365 bit++;
366 if (bit == 8)
367 {
368 (void) WriteBlobByte(image,byte);
369 bit=0;
370 byte=0;
371 }
372 p++;
373 }
374 if (bit != 0)
375 (void) WriteBlobByte(image,byte);
376 if (image->previous == (Image *) NULL)
377 {
cristycee97112010-05-28 00:44:52 +0000378 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000379 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000380 if (status == MagickFalse)
381 break;
382 }
383 }
384 (void) CloseBlob(image);
385 return(MagickTrue);
386}