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