cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % JJJJJ BBBB IIIII GGGG % |
| 7 | % J B B I G % |
| 8 | % J BBBB I G GG % |
| 9 | % J J B B I G G % |
| 10 | % JJJ BBBB IIIII GGG % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write JBIG Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % 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 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
cristy | 597d3d8 | 2012-06-17 18:44:49 +0000 | [diff] [blame] | 43 | #include "MagickCore/attribute.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/color-private.h" |
| 48 | #include "MagickCore/colormap.h" |
| 49 | #include "MagickCore/colorspace.h" |
cristy | d15d8e0 | 2011-07-07 00:17:15 +0000 | [diff] [blame] | 50 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 51 | #include "MagickCore/constitute.h" |
| 52 | #include "MagickCore/exception.h" |
| 53 | #include "MagickCore/exception-private.h" |
| 54 | #include "MagickCore/geometry.h" |
| 55 | #include "MagickCore/image.h" |
| 56 | #include "MagickCore/image-private.h" |
| 57 | #include "MagickCore/list.h" |
| 58 | #include "MagickCore/magick.h" |
| 59 | #include "MagickCore/memory_.h" |
| 60 | #include "MagickCore/monitor.h" |
| 61 | #include "MagickCore/monitor-private.h" |
cristy | 2c5fc27 | 2012-02-22 01:27:46 +0000 | [diff] [blame] | 62 | #include "MagickCore/nt-base-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 63 | #include "MagickCore/pixel-accessor.h" |
| 64 | #include "MagickCore/quantum-private.h" |
| 65 | #include "MagickCore/static.h" |
| 66 | #include "MagickCore/string_.h" |
cristy | 196372d | 2011-11-05 00:07:32 +0000 | [diff] [blame] | 67 | #include "MagickCore/string-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 68 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 69 | #if defined(MAGICKCORE_JBIG_DELEGATE) |
| 70 | #include "jbig.h" |
| 71 | #endif |
| 72 | |
| 73 | /* |
| 74 | Forward declarations. |
| 75 | */ |
| 76 | #if defined(MAGICKCORE_JBIG_DELEGATE) |
| 77 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 78 | WriteJBIGImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | #if defined(MAGICKCORE_JBIG_DELEGATE) |
| 82 | /* |
| 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 84 | % % |
| 85 | % % |
| 86 | % % |
| 87 | % R e a d J B I G I m a g e % |
| 88 | % % |
| 89 | % % |
| 90 | % % |
| 91 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 92 | % |
| 93 | % ReadJBIGImage() reads a JBIG image file and returns it. It |
| 94 | % allocates the memory necessary for the new Image structure and returns a |
| 95 | % pointer to the new image. |
| 96 | % |
| 97 | % The format of the ReadJBIGImage method is: |
| 98 | % |
| 99 | % Image *ReadJBIGImage(const ImageInfo *image_info, |
| 100 | % ExceptionInfo *exception) |
| 101 | % |
| 102 | % A description of each parameter follows: |
| 103 | % |
| 104 | % o image_info: the image info. |
| 105 | % |
| 106 | % o exception: return any errors or warnings in this structure. |
| 107 | % |
| 108 | */ |
| 109 | static Image *ReadJBIGImage(const ImageInfo *image_info, |
| 110 | ExceptionInfo *exception) |
| 111 | { |
| 112 | Image |
| 113 | *image; |
| 114 | |
cristy | 50f1ebc | 2011-06-05 01:35:34 +0000 | [diff] [blame] | 115 | MagickStatusType |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 116 | status; |
| 117 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 118 | Quantum |
| 119 | index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 120 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 121 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 122 | x; |
| 123 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 124 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 125 | *q; |
| 126 | |
| 127 | register unsigned char |
| 128 | *p; |
| 129 | |
| 130 | ssize_t |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 131 | length, |
| 132 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 133 | |
| 134 | struct jbg_dec_state |
| 135 | jbig_info; |
| 136 | |
| 137 | unsigned char |
| 138 | bit, |
| 139 | *buffer, |
| 140 | byte; |
| 141 | |
| 142 | /* |
| 143 | Open image file. |
| 144 | */ |
| 145 | assert(image_info != (const ImageInfo *) NULL); |
| 146 | assert(image_info->signature == MagickSignature); |
| 147 | if (image_info->debug != MagickFalse) |
| 148 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 149 | image_info->filename); |
| 150 | assert(exception != (ExceptionInfo *) NULL); |
| 151 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 152 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 153 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 154 | if (status == MagickFalse) |
| 155 | { |
| 156 | image=DestroyImageList(image); |
| 157 | return((Image *) NULL); |
| 158 | } |
| 159 | /* |
| 160 | Initialize JBIG toolkit. |
| 161 | */ |
| 162 | jbg_dec_init(&jbig_info); |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 163 | jbg_dec_maxsize(&jbig_info,(unsigned long) image->columns,(unsigned long) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 164 | image->rows); |
| 165 | image->columns=jbg_dec_getwidth(&jbig_info); |
| 166 | image->rows=jbg_dec_getheight(&jbig_info); |
| 167 | image->depth=8; |
| 168 | image->storage_class=PseudoClass; |
| 169 | image->colors=2; |
| 170 | /* |
| 171 | Read JBIG file. |
| 172 | */ |
| 173 | buffer=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent, |
| 174 | sizeof(*buffer)); |
| 175 | if (buffer == (unsigned char *) NULL) |
| 176 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 177 | status=JBG_EAGAIN; |
| 178 | do |
| 179 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 180 | length=(ssize_t) ReadBlob(image,MagickMaxBufferExtent,buffer); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | if (length == 0) |
| 182 | break; |
| 183 | p=buffer; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | while ((length > 0) && ((status == JBG_EAGAIN) || (status == JBG_EOK))) |
| 185 | { |
| 186 | size_t |
| 187 | count; |
| 188 | |
| 189 | status=jbg_dec_in(&jbig_info,p,length,&count); |
| 190 | p+=count; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 191 | length-=(ssize_t) count; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 192 | } |
| 193 | } while ((status == JBG_EAGAIN) || (status == JBG_EOK)); |
| 194 | /* |
| 195 | Create colormap. |
| 196 | */ |
| 197 | image->columns=jbg_dec_getwidth(&jbig_info); |
| 198 | image->rows=jbg_dec_getheight(&jbig_info); |
cristy | 6d5e20f | 2011-04-25 13:48:54 +0000 | [diff] [blame] | 199 | image->compression=JBIG2Compression; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 200 | if (AcquireImageColormap(image,2,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | { |
| 202 | buffer=(unsigned char *) RelinquishMagickMemory(buffer); |
| 203 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 204 | } |
| 205 | image->colormap[0].red=0; |
| 206 | image->colormap[0].green=0; |
| 207 | image->colormap[0].blue=0; |
| 208 | image->colormap[1].red=QuantumRange; |
| 209 | image->colormap[1].green=QuantumRange; |
| 210 | image->colormap[1].blue=QuantumRange; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 211 | image->resolution.x=300; |
| 212 | image->resolution.y=300; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 213 | if (image_info->ping != MagickFalse) |
| 214 | { |
| 215 | (void) CloseBlob(image); |
| 216 | return(GetFirstImageInList(image)); |
| 217 | } |
| 218 | /* |
| 219 | Convert X bitmap image to pixel packets. |
| 220 | */ |
| 221 | p=jbg_dec_getimage(&jbig_info,0); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 222 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 223 | { |
| 224 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 225 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 226 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 227 | bit=0; |
| 228 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 229 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 230 | { |
| 231 | if (bit == 0) |
| 232 | byte=(*p++); |
| 233 | index=(byte & 0x80) ? 0 : 1; |
| 234 | bit++; |
| 235 | byte<<=1; |
| 236 | if (bit == 8) |
| 237 | bit=0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 238 | SetPixelIndex(image,index,q); |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 239 | SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 240 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 241 | } |
| 242 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 243 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 244 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 245 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 246 | if (status == MagickFalse) |
| 247 | break; |
| 248 | } |
| 249 | /* |
| 250 | Free scale resource. |
| 251 | */ |
| 252 | jbg_dec_free(&jbig_info); |
| 253 | buffer=(unsigned char *) RelinquishMagickMemory(buffer); |
| 254 | (void) CloseBlob(image); |
| 255 | return(GetFirstImageInList(image)); |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | /* |
| 260 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 261 | % % |
| 262 | % % |
| 263 | % % |
| 264 | % R e g i s t e r J B I G I m a g e % |
| 265 | % % |
| 266 | % % |
| 267 | % % |
| 268 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 269 | % |
| 270 | % RegisterJBIGImage() adds attributes for the JBIG image format to |
| 271 | % the list of supported formats. The attributes include the image format |
| 272 | % tag, a method to read and/or write the format, whether the format |
| 273 | % supports the saving of more than one frame to the same file or blob, |
| 274 | % whether the format supports native in-memory I/O, and a brief |
| 275 | % description of the format. |
| 276 | % |
| 277 | % The format of the RegisterJBIGImage method is: |
| 278 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 279 | % size_t RegisterJBIGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | % |
| 281 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 282 | ModuleExport size_t RegisterJBIGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 283 | { |
| 284 | #define JBIGDescription "Joint Bi-level Image experts Group interchange format" |
| 285 | |
| 286 | char |
| 287 | version[MaxTextExtent]; |
| 288 | |
| 289 | MagickInfo |
| 290 | *entry; |
| 291 | |
| 292 | *version='\0'; |
| 293 | #if defined(JBG_VERSION) |
| 294 | (void) CopyMagickString(version,JBG_VERSION,MaxTextExtent); |
| 295 | #endif |
| 296 | entry=SetMagickInfo("BIE"); |
| 297 | #if defined(MAGICKCORE_JBIG_DELEGATE) |
| 298 | entry->decoder=(DecodeImageHandler *) ReadJBIGImage; |
| 299 | entry->encoder=(EncodeImageHandler *) WriteJBIGImage; |
| 300 | #endif |
| 301 | entry->adjoin=MagickFalse; |
| 302 | entry->description=ConstantString(JBIGDescription); |
| 303 | if (*version != '\0') |
| 304 | entry->version=ConstantString(version); |
| 305 | entry->module=ConstantString("JBIG"); |
| 306 | (void) RegisterMagickInfo(entry); |
| 307 | entry=SetMagickInfo("JBG"); |
| 308 | #if defined(MAGICKCORE_JBIG_DELEGATE) |
| 309 | entry->decoder=(DecodeImageHandler *) ReadJBIGImage; |
| 310 | entry->encoder=(EncodeImageHandler *) WriteJBIGImage; |
| 311 | #endif |
| 312 | entry->description=ConstantString(JBIGDescription); |
| 313 | if (*version != '\0') |
| 314 | entry->version=ConstantString(version); |
| 315 | entry->module=ConstantString("JBIG"); |
| 316 | (void) RegisterMagickInfo(entry); |
| 317 | entry=SetMagickInfo("JBIG"); |
| 318 | #if defined(MAGICKCORE_JBIG_DELEGATE) |
| 319 | entry->decoder=(DecodeImageHandler *) ReadJBIGImage; |
| 320 | entry->encoder=(EncodeImageHandler *) WriteJBIGImage; |
| 321 | #endif |
| 322 | entry->description=ConstantString(JBIGDescription); |
| 323 | if (*version != '\0') |
| 324 | entry->version=ConstantString(version); |
| 325 | entry->module=ConstantString("JBIG"); |
| 326 | (void) RegisterMagickInfo(entry); |
| 327 | return(MagickImageCoderSignature); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 332 | % % |
| 333 | % % |
| 334 | % % |
| 335 | % U n r e g i s t e r J B I G I m a g e % |
| 336 | % % |
| 337 | % % |
| 338 | % % |
| 339 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 340 | % |
| 341 | % UnregisterJBIGImage() removes format registrations made by the |
| 342 | % JBIG module from the list of supported formats. |
| 343 | % |
| 344 | % The format of the UnregisterJBIGImage method is: |
| 345 | % |
| 346 | % UnregisterJBIGImage(void) |
| 347 | % |
| 348 | */ |
| 349 | ModuleExport void UnregisterJBIGImage(void) |
| 350 | { |
| 351 | (void) UnregisterMagickInfo("BIE"); |
| 352 | (void) UnregisterMagickInfo("JBG"); |
| 353 | (void) UnregisterMagickInfo("JBIG"); |
| 354 | } |
| 355 | |
| 356 | #if defined(MAGICKCORE_JBIG_DELEGATE) |
| 357 | /* |
| 358 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 359 | % % |
| 360 | % % |
| 361 | % % |
| 362 | % W r i t e J B I G I m a g e % |
| 363 | % % |
| 364 | % % |
| 365 | % % |
| 366 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 367 | % |
| 368 | % WriteJBIGImage() writes an image in the JBIG encoded image format. |
| 369 | % |
| 370 | % The format of the WriteJBIGImage method is: |
| 371 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 372 | % MagickBooleanType WriteJBIGImage(const ImageInfo *image_info, |
| 373 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 374 | % |
| 375 | % A description of each parameter follows. |
| 376 | % |
| 377 | % o image_info: the image info. |
| 378 | % |
| 379 | % o image: The image. |
| 380 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 381 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 382 | % |
| 383 | */ |
| 384 | |
| 385 | static void JBIGEncode(unsigned char *pixels,size_t length,void *data) |
| 386 | { |
| 387 | Image |
| 388 | *image; |
| 389 | |
| 390 | image=(Image *) data; |
| 391 | (void) WriteBlob(image,length,pixels); |
| 392 | } |
| 393 | |
| 394 | static MagickBooleanType WriteJBIGImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 395 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 396 | { |
| 397 | double |
| 398 | version; |
| 399 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | MagickBooleanType |
| 401 | status; |
| 402 | |
| 403 | MagickOffsetType |
| 404 | scene; |
| 405 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 406 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 407 | *p; |
| 408 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 409 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 410 | x; |
| 411 | |
| 412 | register unsigned char |
| 413 | *q; |
| 414 | |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 415 | size_t |
| 416 | number_packets; |
| 417 | |
| 418 | ssize_t |
| 419 | y; |
| 420 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 421 | struct jbg_enc_state |
| 422 | jbig_info; |
| 423 | |
| 424 | unsigned char |
| 425 | bit, |
| 426 | byte, |
| 427 | *pixels; |
| 428 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 429 | /* |
| 430 | Open image file. |
| 431 | */ |
| 432 | assert(image_info != (const ImageInfo *) NULL); |
| 433 | assert(image_info->signature == MagickSignature); |
| 434 | assert(image != (Image *) NULL); |
| 435 | assert(image->signature == MagickSignature); |
| 436 | if (image->debug != MagickFalse) |
| 437 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 438 | assert(exception != (ExceptionInfo *) NULL); |
| 439 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 440 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | if (status == MagickFalse) |
| 442 | return(status); |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 443 | version=StringToDouble(JBG_VERSION,(char **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 444 | scene=0; |
| 445 | do |
| 446 | { |
| 447 | /* |
| 448 | Allocate pixel data. |
| 449 | */ |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 450 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame] | 451 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 452 | number_packets=(image->columns+7)/8; |
| 453 | pixels=(unsigned char *) AcquireQuantumMemory(number_packets, |
| 454 | image->rows*sizeof(*pixels)); |
| 455 | if (pixels == (unsigned char *) NULL) |
| 456 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 457 | /* |
| 458 | Convert pixels to a bitmap. |
| 459 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 460 | (void) SetImageType(image,BilevelType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 461 | q=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 462 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 463 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 464 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 465 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 466 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 467 | bit=0; |
| 468 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 469 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 470 | { |
| 471 | byte<<=1; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 472 | if (GetPixelIntensity(image,p) < (QuantumRange/2.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 473 | byte|=0x01; |
| 474 | bit++; |
| 475 | if (bit == 8) |
| 476 | { |
| 477 | *q++=byte; |
| 478 | bit=0; |
| 479 | byte=0; |
| 480 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 481 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 482 | } |
| 483 | if (bit != 0) |
| 484 | *q++=byte << (8-bit); |
| 485 | if (image->previous == (Image *) NULL) |
| 486 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 487 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 488 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | if (status == MagickFalse) |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | /* |
| 494 | Initialize JBIG info structure. |
| 495 | */ |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 496 | jbg_enc_init(&jbig_info,(unsigned long) image->columns,(unsigned long) |
| 497 | image->rows,1,&pixels,(void (*)(unsigned char *,size_t,void *)) |
| 498 | JBIGEncode,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | if (image_info->scene != 0) |
| 500 | jbg_enc_layers(&jbig_info,(int) image_info->scene); |
| 501 | else |
| 502 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 503 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 504 | x_resolution, |
| 505 | y_resolution; |
| 506 | |
| 507 | x_resolution=640; |
| 508 | y_resolution=480; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | if (image_info->density != (char *) NULL) |
| 510 | { |
| 511 | GeometryInfo |
| 512 | geometry_info; |
| 513 | |
| 514 | MagickStatusType |
| 515 | flags; |
| 516 | |
| 517 | flags=ParseGeometry(image_info->density,&geometry_info); |
| 518 | x_resolution=geometry_info.rho; |
| 519 | y_resolution=geometry_info.sigma; |
| 520 | if ((flags & SigmaValue) == 0) |
| 521 | y_resolution=x_resolution; |
| 522 | } |
| 523 | if (image->units == PixelsPerCentimeterResolution) |
| 524 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 525 | x_resolution=(size_t) (100.0*2.54*x_resolution+0.5)/100.0; |
| 526 | y_resolution=(size_t) (100.0*2.54*y_resolution+0.5)/100.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 527 | } |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 528 | (void) jbg_enc_lrlmax(&jbig_info,(unsigned long) x_resolution, |
| 529 | (unsigned long) y_resolution); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 530 | } |
| 531 | (void) jbg_enc_lrange(&jbig_info,-1,-1); |
| 532 | jbg_enc_options(&jbig_info,JBG_ILEAVE | JBG_SMID,JBG_TPDON | JBG_TPBON | |
| 533 | JBG_DPON,version < 1.6 ? -1 : 0,-1,-1); |
| 534 | /* |
| 535 | Write JBIG image. |
| 536 | */ |
| 537 | jbg_enc_out(&jbig_info); |
| 538 | jbg_enc_free(&jbig_info); |
| 539 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 540 | if (GetNextImageInList(image) == (Image *) NULL) |
| 541 | break; |
| 542 | image=SyncNextImageInList(image); |
| 543 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 544 | GetImageListLength(image)); |
| 545 | if (status == MagickFalse) |
| 546 | break; |
| 547 | } while (image_info->adjoin != MagickFalse); |
| 548 | (void) CloseBlob(image); |
| 549 | return(MagickTrue); |
| 550 | } |
| 551 | #endif |