2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % JJJJJ BBBB IIIII GGGG %
10 % JJJ BBBB IIIII GGG %
13 % Read/Write JBIG Image Format %
20 % Copyright 1999-2017 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 % https://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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
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"
50 #include "MagickCore/colorspace-private.h"
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"
62 #include "MagickCore/nt-base-private.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/quantum-private.h"
65 #include "MagickCore/static.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
68 #include "MagickCore/module.h"
69 #if defined(MAGICKCORE_JBIG_DELEGATE)
70 #if defined(__cplusplus) || defined(c_plusplus)
74 #if defined(__cplusplus) || defined(c_plusplus)
82 #if defined(MAGICKCORE_JBIG_DELEGATE)
83 static MagickBooleanType
84 WriteJBIGImage(const ImageInfo *,Image *,ExceptionInfo *);
87 #if defined(MAGICKCORE_JBIG_DELEGATE)
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % R e a d J B I G I m a g e %
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
103 % The format of the ReadJBIGImage method is:
105 % Image *ReadJBIGImage(const ImageInfo *image_info,
106 % ExceptionInfo *exception)
108 % A description of each parameter follows:
110 % o image_info: the image info.
112 % o exception: return any errors or warnings in this structure.
115 static Image *ReadJBIGImage(const ImageInfo *image_info,
116 ExceptionInfo *exception)
133 register unsigned char
151 assert(image_info != (const ImageInfo *) NULL);
152 assert(image_info->signature == MagickCoreSignature);
153 if (image_info->debug != MagickFalse)
154 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
155 image_info->filename);
156 assert(exception != (ExceptionInfo *) NULL);
157 assert(exception->signature == MagickCoreSignature);
158 image=AcquireImage(image_info,exception);
159 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
160 if (status == MagickFalse)
162 image=DestroyImageList(image);
163 return((Image *) NULL);
166 Initialize JBIG toolkit.
168 jbg_dec_init(&jbig_info);
169 jbg_dec_maxsize(&jbig_info,(unsigned long) image->columns,(unsigned long)
171 image->columns=jbg_dec_getwidth(&jbig_info);
172 image->rows=jbg_dec_getheight(&jbig_info);
174 image->storage_class=PseudoClass;
179 buffer=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
181 if (buffer == (unsigned char *) NULL)
183 jbg_dec_free(&jbig_info);
184 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
189 length=(ssize_t) ReadBlob(image,MagickMaxBufferExtent,buffer);
193 while ((length > 0) && ((status == JBG_EAGAIN) || (status == JBG_EOK)))
198 status=jbg_dec_in(&jbig_info,p,length,&count);
200 length-=(ssize_t) count;
202 } while ((status == JBG_EAGAIN) || (status == JBG_EOK));
206 image->columns=jbg_dec_getwidth(&jbig_info);
207 image->rows=jbg_dec_getheight(&jbig_info);
208 image->compression=JBIG2Compression;
209 if (AcquireImageColormap(image,2,exception) == MagickFalse)
211 jbg_dec_free(&jbig_info);
212 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
213 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
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;
221 image->resolution.x=300;
222 image->resolution.y=300;
223 if (image_info->ping != MagickFalse)
225 jbg_dec_free(&jbig_info);
226 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
227 (void) CloseBlob(image);
228 return(GetFirstImageInList(image));
230 status=SetImageExtent(image,image->columns,image->rows,exception);
231 if (status == MagickFalse)
233 jbg_dec_free(&jbig_info);
234 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
235 return(DestroyImageList(image));
238 Convert X bitmap image to pixel packets.
240 p=jbg_dec_getimage(&jbig_info,0);
241 for (y=0; y < (ssize_t) image->rows; y++)
243 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
244 if (q == (Quantum *) NULL)
248 for (x=0; x < (ssize_t) image->columns; x++)
252 index=(byte & 0x80) ? 0 : 1;
257 SetPixelIndex(image,index,q);
258 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
259 q+=GetPixelChannels(image);
261 if (SyncAuthenticPixels(image,exception) == MagickFalse)
263 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
265 if (status == MagickFalse)
271 jbg_dec_free(&jbig_info);
272 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
273 (void) CloseBlob(image);
274 return(GetFirstImageInList(image));
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % R e g i s t e r J B I G I m a g e %
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
296 % The format of the RegisterJBIGImage method is:
298 % size_t RegisterJBIGImage(void)
301 ModuleExport size_t RegisterJBIGImage(void)
303 #define JBIGDescription "Joint Bi-level Image experts Group interchange format"
306 version[MagickPathExtent];
312 #if defined(JBG_VERSION)
313 (void) CopyMagickString(version,JBG_VERSION,MagickPathExtent);
315 entry=AcquireMagickInfo("JBIG","BIE",JBIGDescription);
316 #if defined(MAGICKCORE_JBIG_DELEGATE)
317 entry->decoder=(DecodeImageHandler *) ReadJBIGImage;
318 entry->encoder=(EncodeImageHandler *) WriteJBIGImage;
320 entry->flags^=CoderAdjoinFlag;
321 if (*version != '\0')
322 entry->version=ConstantString(version);
323 (void) RegisterMagickInfo(entry);
324 entry=AcquireMagickInfo("JBIG","JBG",JBIGDescription);
325 #if defined(MAGICKCORE_JBIG_DELEGATE)
326 entry->decoder=(DecodeImageHandler *) ReadJBIGImage;
327 entry->encoder=(EncodeImageHandler *) WriteJBIGImage;
329 if (*version != '\0')
330 entry->version=ConstantString(version);
331 (void) RegisterMagickInfo(entry);
332 entry=AcquireMagickInfo("JBIG","JBIG",JBIGDescription);
333 #if defined(MAGICKCORE_JBIG_DELEGATE)
334 entry->decoder=(DecodeImageHandler *) ReadJBIGImage;
335 entry->encoder=(EncodeImageHandler *) WriteJBIGImage;
337 if (*version != '\0')
338 entry->version=ConstantString(version);
339 (void) RegisterMagickInfo(entry);
340 return(MagickImageCoderSignature);
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % U n r e g i s t e r J B I G I m a g e %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % UnregisterJBIGImage() removes format registrations made by the
355 % JBIG module from the list of supported formats.
357 % The format of the UnregisterJBIGImage method is:
359 % UnregisterJBIGImage(void)
362 ModuleExport void UnregisterJBIGImage(void)
364 (void) UnregisterMagickInfo("BIE");
365 (void) UnregisterMagickInfo("JBG");
366 (void) UnregisterMagickInfo("JBIG");
369 #if defined(MAGICKCORE_JBIG_DELEGATE)
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 % W r i t e J B I G I m a g e %
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 % WriteJBIGImage() writes an image in the JBIG encoded image format.
383 % The format of the WriteJBIGImage method is:
385 % MagickBooleanType WriteJBIGImage(const ImageInfo *image_info,
386 % Image *image,ExceptionInfo *exception)
388 % A description of each parameter follows.
390 % o image_info: the image info.
392 % o image: The image.
394 % o exception: return any errors or warnings in this structure.
398 static void JBIGEncode(unsigned char *pixels,size_t length,void *data)
403 image=(Image *) data;
404 (void) WriteBlob(image,length,pixels);
407 static MagickBooleanType WriteJBIGImage(const ImageInfo *image_info,
408 Image *image,ExceptionInfo *exception)
422 register const Quantum
428 register unsigned char
448 assert(image_info != (const ImageInfo *) NULL);
449 assert(image_info->signature == MagickCoreSignature);
450 assert(image != (Image *) NULL);
451 assert(image->signature == MagickCoreSignature);
452 if (image->debug != MagickFalse)
453 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
454 assert(exception != (ExceptionInfo *) NULL);
455 assert(exception->signature == MagickCoreSignature);
456 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
457 if (status == MagickFalse)
459 version=StringToDouble(JBG_VERSION,(char **) NULL);
466 (void) TransformImageColorspace(image,sRGBColorspace,exception);
467 number_packets=(image->columns+7)/8;
468 pixel_info=AcquireVirtualMemory(number_packets,image->rows*sizeof(*pixels));
469 if (pixel_info == (MemoryInfo *) NULL)
470 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
471 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
473 Convert pixels to a bitmap.
475 (void) SetImageType(image,BilevelType,exception);
477 for (y=0; y < (ssize_t) image->rows; y++)
479 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
480 if (p == (const Quantum *) NULL)
484 for (x=0; x < (ssize_t) image->columns; x++)
487 if (GetPixelLuma(image,p) < (QuantumRange/2.0))
496 p+=GetPixelChannels(image);
499 *q++=byte << (8-bit);
500 if (image->previous == (Image *) NULL)
502 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
504 if (status == MagickFalse)
509 Initialize JBIG info structure.
511 jbg_enc_init(&jbig_info,(unsigned long) image->columns,(unsigned long)
512 image->rows,1,&pixels,(void (*)(unsigned char *,size_t,void *))
514 if (image_info->scene != 0)
515 jbg_enc_layers(&jbig_info,(int) image_info->scene);
524 if (image_info->density != (char *) NULL)
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;
538 if (image->units == PixelsPerCentimeterResolution)
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;
543 (void) jbg_enc_lrlmax(&jbig_info,(unsigned long) x_resolution,
544 (unsigned long) y_resolution);
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);
552 jbg_enc_out(&jbig_info);
553 jbg_enc_free(&jbig_info);
554 pixel_info=RelinquishVirtualMemory(pixel_info);
555 if (GetNextImageInList(image) == (Image *) NULL)
557 image=SyncNextImageInList(image);
558 status=SetImageProgress(image,SaveImagesTag,scene++,
559 GetImageListLength(image));
560 if (status == MagickFalse)
562 } while (image_info->adjoin != MagickFalse);
563 (void) CloseBlob(image);