Powered by Pair ImageMagick logo
Image Magick
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

ImageMagick-6.1.1/coders/mono.c

Go to the documentation of this file.
00001 /* 00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00003 % % 00004 % % 00005 % % 00006 % M M OOO N N OOO % 00007 % MM MM O O NN N O O % 00008 % M M M O O N N N O O % 00009 % M M O O N NN O O % 00010 % M M OOO N N OOO % 00011 % % 00012 % % 00013 % Read/Write Raw Bi-Level Bitmap Format. % 00014 % % 00015 % Software Design % 00016 % John Cristy % 00017 % July 1992 % 00018 % % 00019 % % 00020 % Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization % 00021 % dedicated to making software imaging solutions freely available. % 00022 % % 00023 % You may not use this file except in compliance with the License. You may % 00024 % obtain a copy of the License at % 00025 % % 00026 % http://www.imagemagick.org/www/Copyright.html % 00027 % % 00028 % Unless required by applicable law or agreed to in writing, software % 00029 % distributed under the License is distributed on an "AS IS" BASIS, % 00030 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % 00031 % See the License for the specific language governing permissions and % 00032 % limitations under the License. % 00033 % % 00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00035 % 00036 % 00037 */ 00038 00039 /* 00040 Include declarations. 00041 */ 00042 #include "magick/studio.h" 00043 #include "magick/blob.h" 00044 #include "magick/blob_private.h" 00045 #include "magick/colorspace.h" 00046 #include "magick/error.h" 00047 #include "magick/error_private.h" 00048 #include "magick/image.h" 00049 #include "magick/image_private.h" 00050 #include "magick/list.h" 00051 #include "magick/magick.h" 00052 #include "magick/memory_.h" 00053 #include "magick/monitor.h" 00054 #include "magick/static.h" 00055 #include "magick/string_.h" 00056 00057 /* 00058 Forward declarations. 00059 */ 00060 static MagickBooleanType 00061 WriteMONOImage(const ImageInfo *,Image *); 00062 00063 /* 00064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00065 % % 00066 % % 00067 % % 00068 % R e a d M O N O I m a g e % 00069 % % 00070 % % 00071 % % 00072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00073 % 00074 % ReadMONOImage() reads an image of raw bites in LSB order and returns 00075 % it. It allocates the memory necessary for the new Image structure and 00076 % returns a pointer to the new image. 00077 % 00078 % The format of the ReadMONOImage method is: 00079 % 00080 % Image *ReadMONOImage(const ImageInfo *image_info, 00081 % ExceptionInfo *exception) 00082 % 00083 % A description of each parameter follows: 00084 % 00085 % o image_info: The image info. 00086 % 00087 % o exception: return any errors or warnings in this structure. 00088 % 00089 % 00090 */ 00091 static Image *ReadMONOImage(const ImageInfo *image_info, 00092 ExceptionInfo *exception) 00093 { 00094 Image 00095 *image; 00096 00097 long 00098 y; 00099 00100 MagickBooleanType 00101 status; 00102 00103 register IndexPacket 00104 *indexes; 00105 00106 register long 00107 x; 00108 00109 register PixelPacket 00110 *q; 00111 00112 register long 00113 i; 00114 00115 unsigned long 00116 bit, 00117 byte; 00118 00119 /* 00120 Open image file. 00121 */ 00122 assert(image_info != (const ImageInfo *) NULL); 00123 assert(image_info->signature == MagickSignature); 00124 if (image_info->debug != MagickFalse) 00125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),image_info->filename); 00126 assert(exception != (ExceptionInfo *) NULL); 00127 assert(exception->signature == MagickSignature); 00128 image=AllocateImage(image_info); 00129 if ((image->columns == 0) || (image->rows == 0)) 00130 ThrowReaderException(OptionError,"MustSpecifyImageSize"); 00131 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); 00132 if (status == MagickFalse) 00133 { 00134 DestroyImageList(image); 00135 return((Image *) NULL); 00136 } 00137 for (i=0; i < image->offset; i++) 00138 (void) ReadBlobByte(image); 00139 /* 00140 Initialize image colormap. 00141 */ 00142 if (AllocateImageColormap(image,2) == MagickFalse) 00143 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); 00144 if (image_info->ping != MagickFalse) 00145 { 00146 CloseBlob(image); 00147 return(GetFirstImageInList(image)); 00148 } 00149 /* 00150 Convert bi-level image to pixel packets. 00151 */ 00152 for (y=0; y < (long) image->rows; y++) 00153 { 00154 q=SetImagePixels(image,0,y,image->columns,1); 00155 if (q == (PixelPacket *) NULL) 00156 break; 00157 indexes=GetIndexes(image); 00158 bit=0; 00159 byte=0; 00160 for (x=0; x < (long) image->columns; x++) 00161 { 00162 if (bit == 0) 00163 byte=(unsigned long) ReadBlobByte(image); 00164 indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x01 : 0x00); 00165 bit++; 00166 if (bit == 8) 00167 bit=0; 00168 byte>>=1; 00169 } 00170 if (SyncImagePixels(image) == MagickFalse) 00171 break; 00172 if ((image->progress_monitor != (MagickProgressMonitor) NULL) && 00173 (QuantumTick(y,image->rows) != MagickFalse)) 00174 { 00175 status=image->progress_monitor(LoadImageTag,y,image->rows, 00176 image->client_data); 00177 if (status == MagickFalse) 00178 break; 00179 } 00180 } 00181 SyncImage(image); 00182 if (EOFBlob(image) != MagickFalse) 00183 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", 00184 image->filename); 00185 CloseBlob(image); 00186 return(GetFirstImageInList(image)); 00187 } 00188 00189 /* 00190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00191 % % 00192 % % 00193 % % 00194 % R e g i s t e r M O N O I m a g e % 00195 % % 00196 % % 00197 % % 00198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00199 % 00200 % RegisterMONOImage() adds attributes for the MONO image format to 00201 % the list of supported formats. The attributes include the image format 00202 % tag, a method to read and/or write the format, whether the format 00203 % supports the saving of more than one frame to the same file or blob, 00204 % whether the format supports native in-memory I/O, and a brief 00205 % description of the format. 00206 % 00207 % The format of the RegisterMONOImage method is: 00208 % 00209 % RegisterMONOImage(void) 00210 % 00211 */ 00212 ModuleExport void RegisterMONOImage(void) 00213 { 00214 MagickInfo 00215 *entry; 00216 00217 entry=SetMagickInfo("MONO"); 00218 entry->decoder=(DecoderHandler *) ReadMONOImage; 00219 entry->encoder=(EncoderHandler *) WriteMONOImage; 00220 entry->raw=MagickTrue; 00221 entry->endian_support=MagickTrue; 00222 entry->adjoin=MagickFalse; 00223 entry->description= 00224 AcquireString("Bi-level bitmap in least-significant-byte first order"); 00225 entry->module=AcquireString("MONO"); 00226 (void) RegisterMagickInfo(entry); 00227 } 00228 00229 /* 00230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00231 % % 00232 % % 00233 % % 00234 % U n r e g i s t e r M O N O I m a g e % 00235 % % 00236 % % 00237 % % 00238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00239 % 00240 % UnregisterMONOImage() removes format registrations made by the 00241 % MONO module from the list of supported formats. 00242 % 00243 % The format of the UnregisterMONOImage method is: 00244 % 00245 % UnregisterMONOImage(void) 00246 % 00247 */ 00248 ModuleExport void UnregisterMONOImage(void) 00249 { 00250 (void) UnregisterMagickInfo("MONO"); 00251 } 00252 00253 /* 00254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00255 % % 00256 % % 00257 % % 00258 % W r i t e M O N O I m a g e % 00259 % % 00260 % % 00261 % % 00262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00263 % 00264 % WriteMONOImage() writes an image of raw bits in LSB order to a file. 00265 % 00266 % The format of the WriteMONOImage method is: 00267 % 00268 % MagickBooleanType WriteMONOImage(const ImageInfo *image_info,Image *image) 00269 % 00270 % A description of each parameter follows. 00271 % 00272 % o image_info: The image info. 00273 % 00274 % o image: The image. 00275 % 00276 % 00277 */ 00278 static MagickBooleanType WriteMONOImage(const ImageInfo *image_info, 00279 Image *image) 00280 { 00281 IndexPacket 00282 polarity; 00283 00284 long 00285 y; 00286 00287 MagickBooleanType 00288 status; 00289 00290 register IndexPacket 00291 *indexes; 00292 00293 register const PixelPacket 00294 *p; 00295 00296 register long 00297 x; 00298 00299 unsigned long 00300 bit, 00301 byte; 00302 00303 /* 00304 Open output image file. 00305 */ 00306 assert(image_info != (const ImageInfo *) NULL); 00307 assert(image_info->signature == MagickSignature); 00308 assert(image != (Image *) NULL); 00309 assert(image->signature == MagickSignature); 00310 if (image->debug != MagickFalse) 00311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),image->filename); 00312 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); 00313 if (status == MagickFalse) 00314 return(status); 00315 (void) SetImageColorspace(image,RGBColorspace); 00316 /* 00317 Convert image to a bi-level image. 00318 */ 00319 (void) SetImageType(image,BilevelType); 00320 polarity=(IndexPacket) 00321 (PixelIntensityToQuantum(&image->colormap[0]) < (MaxRGB/2)); 00322 if (image->colors == 2) 00323 polarity=(IndexPacket) (PixelIntensityToQuantum(&image->colormap[0]) < 00324 PixelIntensityToQuantum(&image->colormap[1])); 00325 for (y=0; y < (long) image->rows; y++) 00326 { 00327 p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); 00328 if (p == (const PixelPacket *) NULL) 00329 break; 00330 indexes=GetIndexes(image); 00331 bit=0; 00332 byte=0; 00333 for (x=0; x < (long) image->columns; x++) 00334 { 00335 byte>>=1; 00336 if (indexes[x] == polarity) 00337 byte|=0x80; 00338 bit++; 00339 if (bit == 8) 00340 { 00341 (void) WriteBlobByte(image,(unsigned char) byte); 00342 bit=0; 00343 byte=0; 00344 } 00345 } 00346 if (bit != 0) 00347 (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit))); 00348 if ((image->progress_monitor != (MagickProgressMonitor) NULL) && 00349 (QuantumTick(y,image->rows) != MagickFalse)) 00350 { 00351 status=image->progress_monitor(SaveImageTag,y,image->rows, 00352 image->client_data); 00353 if (status == MagickFalse) 00354 break; 00355 } 00356 } 00357 CloseBlob(image); 00358 return(MagickTrue); 00359 }

Generated on Mon Oct 25 13:40:38 2004 for ImageMagick by doxygen 1.3.7
ImageMagick Copyright © 2004, ImageMagick Studio LLC