| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #include "magick/studio.h" |
|---|
| 43 | #include "magick/blob.h" |
|---|
| 44 | #include "magick/blob-private.h" |
|---|
| 45 | #include "magick/color-private.h" |
|---|
| 46 | #include "magick/colorspace.h" |
|---|
| 47 | #include "magick/exception.h" |
|---|
| 48 | #include "magick/exception-private.h" |
|---|
| 49 | #include "magick/image.h" |
|---|
| 50 | #include "magick/image-private.h" |
|---|
| 51 | #include "magick/list.h" |
|---|
| 52 | #include "magick/magick.h" |
|---|
| 53 | #include "magick/memory_.h" |
|---|
| 54 | #include "magick/monitor.h" |
|---|
| 55 | #include "magick/monitor-private.h" |
|---|
| 56 | #include "magick/quantum-private.h" |
|---|
| 57 | #include "magick/static.h" |
|---|
| 58 | #include "magick/string_.h" |
|---|
| 59 | #include "magick/module.h" |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | static MagickBooleanType |
|---|
| 65 | WriteMONOImage(const ImageInfo *,Image *); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | static Image *ReadMONOImage(const ImageInfo *image_info, |
|---|
| 95 | ExceptionInfo *exception) |
|---|
| 96 | { |
|---|
| 97 | Image |
|---|
| 98 | *image; |
|---|
| 99 | |
|---|
| 100 | long |
|---|
| 101 | y; |
|---|
| 102 | |
|---|
| 103 | MagickBooleanType |
|---|
| 104 | status; |
|---|
| 105 | |
|---|
| 106 | register IndexPacket |
|---|
| 107 | *indexes; |
|---|
| 108 | |
|---|
| 109 | register long |
|---|
| 110 | x; |
|---|
| 111 | |
|---|
| 112 | register PixelPacket |
|---|
| 113 | *q; |
|---|
| 114 | |
|---|
| 115 | register long |
|---|
| 116 | i; |
|---|
| 117 | |
|---|
| 118 | unsigned long |
|---|
| 119 | bit, |
|---|
| 120 | byte; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | assert(image_info != (const ImageInfo *) NULL); |
|---|
| 126 | assert(image_info->signature == MagickSignature); |
|---|
| 127 | if (image_info->debug != MagickFalse) |
|---|
| 128 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
|---|
| 129 | image_info->filename); |
|---|
| 130 | assert(exception != (ExceptionInfo *) NULL); |
|---|
| 131 | assert(exception->signature == MagickSignature); |
|---|
| 132 | image=AcquireImage(image_info); |
|---|
| 133 | if ((image->columns == 0) || (image->rows == 0)) |
|---|
| 134 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
|---|
| 135 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
|---|
| 136 | if (status == MagickFalse) |
|---|
| 137 | { |
|---|
| 138 | image=DestroyImageList(image); |
|---|
| 139 | return((Image *) NULL); |
|---|
| 140 | } |
|---|
| 141 | for (i=0; i < image->offset; i++) |
|---|
| 142 | if (ReadBlobByte(image) == EOF) |
|---|
| 143 | { |
|---|
| 144 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
|---|
| 145 | image->filename); |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | image->depth=1; |
|---|
| 152 | if (AcquireImageColormap(image,2) == MagickFalse) |
|---|
| 153 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
|---|
| 154 | if (image_info->ping != MagickFalse) |
|---|
| 155 | { |
|---|
| 156 | (void) CloseBlob(image); |
|---|
| 157 | return(GetFirstImageInList(image)); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | if (SetImageExtent(image,0,0) == MagickFalse) |
|---|
| 163 | { |
|---|
| 164 | InheritException(exception,&image->exception); |
|---|
| 165 | return(DestroyImageList(image)); |
|---|
| 166 | } |
|---|
| 167 | for (y=0; y < (long) image->rows; y++) |
|---|
| 168 | { |
|---|
| 169 | q=SetImagePixels(image,0,y,image->columns,1); |
|---|
| 170 | if (q == (PixelPacket *) NULL) |
|---|
| 171 | break; |
|---|
| 172 | indexes=GetIndexes(image); |
|---|
| 173 | bit=0; |
|---|
| 174 | byte=0; |
|---|
| 175 | for (x=0; x < (long) image->columns; x++) |
|---|
| 176 | { |
|---|
| 177 | if (bit == 0) |
|---|
| 178 | byte=(unsigned long) ReadBlobByte(image); |
|---|
| 179 | if (image_info->endian == LSBEndian) |
|---|
| 180 | indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01); |
|---|
| 181 | else |
|---|
| 182 | indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x01 : 0x00); |
|---|
| 183 | bit++; |
|---|
| 184 | if (bit == 8) |
|---|
| 185 | bit=0; |
|---|
| 186 | byte>>=1; |
|---|
| 187 | } |
|---|
| 188 | if (SyncImagePixels(image) == MagickFalse) |
|---|
| 189 | break; |
|---|
| 190 | status=SetImageProgress(image,LoadImageTag,y,image->rows); |
|---|
| 191 | if (status == MagickFalse) |
|---|
| 192 | break; |
|---|
| 193 | } |
|---|
| 194 | (void) SyncImage(image); |
|---|
| 195 | if (EOFBlob(image) != MagickFalse) |
|---|
| 196 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
|---|
| 197 | image->filename); |
|---|
| 198 | (void) CloseBlob(image); |
|---|
| 199 | return(GetFirstImageInList(image)); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | ModuleExport unsigned long RegisterMONOImage(void) |
|---|
| 226 | { |
|---|
| 227 | MagickInfo |
|---|
| 228 | *entry; |
|---|
| 229 | |
|---|
| 230 | entry=SetMagickInfo("MONO"); |
|---|
| 231 | entry->decoder=(DecodeImageHandler *) ReadMONOImage; |
|---|
| 232 | entry->encoder=(EncodeImageHandler *) WriteMONOImage; |
|---|
| 233 | entry->raw=MagickTrue; |
|---|
| 234 | entry->endian_support=MagickTrue; |
|---|
| 235 | entry->adjoin=MagickFalse; |
|---|
| 236 | entry->description=ConstantString("Raw bi-level bitmap"); |
|---|
| 237 | entry->module=ConstantString("MONO"); |
|---|
| 238 | (void) RegisterMagickInfo(entry); |
|---|
| 239 | return(MagickImageCoderSignature); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | ModuleExport void UnregisterMONOImage(void) |
|---|
| 262 | { |
|---|
| 263 | (void) UnregisterMagickInfo("MONO"); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | static MagickBooleanType WriteMONOImage(const ImageInfo *image_info, |
|---|
| 292 | Image *image) |
|---|
| 293 | { |
|---|
| 294 | long |
|---|
| 295 | y; |
|---|
| 296 | |
|---|
| 297 | MagickBooleanType |
|---|
| 298 | status; |
|---|
| 299 | |
|---|
| 300 | register IndexPacket |
|---|
| 301 | *indexes; |
|---|
| 302 | |
|---|
| 303 | register const PixelPacket |
|---|
| 304 | *p; |
|---|
| 305 | |
|---|
| 306 | register long |
|---|
| 307 | x; |
|---|
| 308 | |
|---|
| 309 | unsigned long |
|---|
| 310 | bit, |
|---|
| 311 | byte; |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | assert(image_info != (const ImageInfo *) NULL); |
|---|
| 317 | assert(image_info->signature == MagickSignature); |
|---|
| 318 | assert(image != (Image *) NULL); |
|---|
| 319 | assert(image->signature == MagickSignature); |
|---|
| 320 | if (image->debug != MagickFalse) |
|---|
| 321 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
|---|
| 322 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
|---|
| 323 | if (status == MagickFalse) |
|---|
| 324 | return(status); |
|---|
| 325 | if (image->colorspace != RGBColorspace) |
|---|
| 326 | (void) SetImageColorspace(image,RGBColorspace); |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | (void) SetImageType(image,BilevelType); |
|---|
| 331 | for (y=0; y < (long) image->rows; y++) |
|---|
| 332 | { |
|---|
| 333 | p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); |
|---|
| 334 | if (p == (const PixelPacket *) NULL) |
|---|
| 335 | break; |
|---|
| 336 | indexes=GetIndexes(image); |
|---|
| 337 | bit=0; |
|---|
| 338 | byte=0; |
|---|
| 339 | for (x=0; x < (long) image->columns; x++) |
|---|
| 340 | { |
|---|
| 341 | byte>>=1; |
|---|
| 342 | if (image->endian == LSBEndian) |
|---|
| 343 | { |
|---|
| 344 | if (PixelIntensity(p) < ((Quantum) QuantumRange/2.0)) |
|---|
| 345 | byte|=0x80; |
|---|
| 346 | } |
|---|
| 347 | else |
|---|
| 348 | if (PixelIntensity(p) >= ((Quantum) QuantumRange/2.0)) |
|---|
| 349 | byte|=0x80; |
|---|
| 350 | bit++; |
|---|
| 351 | if (bit == 8) |
|---|
| 352 | { |
|---|
| 353 | (void) WriteBlobByte(image,(unsigned char) byte); |
|---|
| 354 | bit=0; |
|---|
| 355 | byte=0; |
|---|
| 356 | } |
|---|
| 357 | p++; |
|---|
| 358 | } |
|---|
| 359 | if (bit != 0) |
|---|
| 360 | (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit))); |
|---|
| 361 | status=SetImageProgress(image,SaveImageTag,y,image->rows); |
|---|
| 362 | if (status == MagickFalse) |
|---|
| 363 | break; |
|---|
| 364 | } |
|---|
| 365 | (void) CloseBlob(image); |
|---|
| 366 | return(MagickTrue); |
|---|
| 367 | } |
|---|