2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write On-The-Air Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 #include "MagickCore/studio.h"
42 #include "MagickCore/blob.h"
43 #include "MagickCore/blob-private.h"
44 #include "MagickCore/cache.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/colorspace.h"
48 #include "MagickCore/colorspace-private.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
67 static MagickBooleanType
68 WriteOTBImage(const ImageInfo *,Image *,ExceptionInfo *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e a d O T B I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % ReadOTBImage() reads a on-the-air (level 0) bitmap and returns it. It
82 % allocates the memory necessary for the new Image structure and returns a
83 % pointer to the new image.
85 % The format of the ReadOTBImage method is:
87 % Image *ReadOTBImage(const ImageInfo *image_info,ExceptionInfo *exception)
89 % A description of each parameter follows:
91 % o image_info: the image info.
93 % o exception: return any errors or warnings in this structure.
97 static Image *ReadOTBImage(const ImageInfo *image_info,ExceptionInfo *exception)
99 #define GetBit(a,i) (((a) >> (i)) & 1L)
127 assert(image_info != (const ImageInfo *) NULL);
128 assert(image_info->signature == MagickSignature);
129 if (image_info->debug != MagickFalse)
130 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
131 image_info->filename);
132 assert(exception != (ExceptionInfo *) NULL);
133 assert(exception->signature == MagickSignature);
134 image=AcquireImage(image_info,exception);
135 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
136 if (status == MagickFalse)
138 image=DestroyImageList(image);
139 return((Image *) NULL);
142 Initialize image structure.
144 info=(unsigned char) ReadBlobByte(image);
145 if (GetBit(info,4) == 0)
147 image->columns=(size_t) ReadBlobByte(image);
148 image->rows=(size_t) ReadBlobByte(image);
152 image->columns=(size_t) ReadBlobMSBShort(image);
153 image->rows=(size_t) ReadBlobMSBShort(image);
155 if ((image->columns == 0) || (image->rows == 0))
156 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
157 depth=(unsigned char) ReadBlobByte(image);
159 ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
160 if (AcquireImageColormap(image,2,exception) == MagickFalse)
161 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
162 if (image_info->ping != MagickFalse)
164 (void) CloseBlob(image);
165 return(GetFirstImageInList(image));
168 Convert bi-level image to pixel packets.
170 for (y=0; y < (ssize_t) image->rows; y++)
172 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
173 if (q == (Quantum *) NULL)
177 for (x=0; x < (ssize_t) image->columns; x++)
181 byte=ReadBlobByte(image);
183 ThrowReaderException(CorruptImageError,"CorruptImage");
185 SetPixelIndex(image,(byte & (0x01 << (7-bit))) ? 0x00 : 0x01,q);
189 q+=GetPixelChannels(image);
191 if (SyncAuthenticPixels(image,exception) == MagickFalse)
193 if (image->previous == (Image *) NULL)
195 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
197 if (status == MagickFalse)
201 (void) SyncImage(image,exception);
202 if (EOFBlob(image) != MagickFalse)
203 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
205 (void) CloseBlob(image);
206 return(GetFirstImageInList(image));
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 % R e g i s t e r O T B I m a g e %
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 % RegisterOTBImage() adds attributes for the OTB image format to
221 % the list of supported formats. The attributes include the image format
222 % tag, a method to read and/or write the format, whether the format
223 % supports the saving of more than one frame to the same file or blob,
224 % whether the format supports native in-memory I/O, and a brief
225 % description of the format.
227 % The format of the RegisterOTBImage method is:
229 % size_t RegisterOTBImage(void)
232 ModuleExport size_t RegisterOTBImage(void)
237 entry=SetMagickInfo("OTB");
238 entry->decoder=(DecodeImageHandler *) ReadOTBImage;
239 entry->encoder=(EncodeImageHandler *) WriteOTBImage;
240 entry->adjoin=MagickFalse;
241 entry->description=ConstantString("On-the-air bitmap");
242 entry->module=ConstantString("OTB");
243 (void) RegisterMagickInfo(entry);
244 return(MagickImageCoderSignature);
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % U n r e g i s t e r O T B I m a g e %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % UnregisterOTBImage() removes format registrations made by the
259 % OTB module from the list of supported formats.
261 % The format of the UnregisterOTBImage method is:
263 % UnregisterOTBImage(void)
266 ModuleExport void UnregisterOTBImage(void)
268 (void) UnregisterMagickInfo("OTB");
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % W r i t e O T B I m a g e %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % WriteOTBImage() writes an image to a file in the On-the-air Bitmap
283 % (level 0) image format.
285 % The format of the WriteOTBImage method is:
287 % MagickBooleanType WriteOTBImage(const ImageInfo *image_info,
288 % Image *image,ExceptionInfo *exception)
290 % A description of each parameter follows.
292 % o image_info: the image info.
294 % o image: The image.
296 % o exception: return any errors or warnings in this structure.
299 static MagickBooleanType WriteOTBImage(const ImageInfo *image_info,Image *image,
300 ExceptionInfo *exception)
302 #define SetBit(a,i,set) \
303 a=(unsigned char) ((set) ? (a) | (1L << (i)) : (a) & ~(1L << (i)))
308 register const Quantum
323 Open output image file.
325 assert(image_info != (const ImageInfo *) NULL);
326 assert(image_info->signature == MagickSignature);
327 assert(image != (Image *) NULL);
328 assert(image->signature == MagickSignature);
329 if (image->debug != MagickFalse)
330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
331 assert(exception != (ExceptionInfo *) NULL);
332 assert(exception->signature == MagickSignature);
333 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
334 if (status == MagickFalse)
336 if (IssRGBColorspace(image->colorspace) == MagickFalse)
337 (void) TransformImageColorspace(image,sRGBColorspace,exception);
339 Convert image to a bi-level image.
341 (void) SetImageType(image,BilevelType,exception);
343 if ((image->columns >= 256) || (image->rows >= 256))
345 (void) WriteBlobByte(image,info);
346 if ((image->columns >= 256) || (image->rows >= 256))
348 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
349 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
353 (void) WriteBlobByte(image,(unsigned char) image->columns);
354 (void) WriteBlobByte(image,(unsigned char) image->rows);
356 (void) WriteBlobByte(image,1); /* depth */
357 for (y=0; y < (ssize_t) image->rows; y++)
359 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
360 if (p == (const Quantum *) NULL)
364 for (x=0; x < (ssize_t) image->columns; x++)
366 if (GetPixelIntensity(image,p) < ((Quantum) QuantumRange/2.0))
367 byte|=0x1 << (7-bit);
371 (void) WriteBlobByte(image,byte);
375 p+=GetPixelChannels(image);
378 (void) WriteBlobByte(image,byte);
379 if (image->previous == (Image *) NULL)
381 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
383 if (status == MagickFalse)
387 (void) CloseBlob(image);