]> granicus.if.org Git - imagemagick/blob - coders/jbig.c
(no commit message)
[imagemagick] / coders / jbig.c
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 %                                                                             %
20 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
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 \f
39 /*
40   Include declarations.
41 */
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 #include "jbig.h"
71 #endif
72 \f
73 /*
74   Forward declarations.
75 */
76 #if defined(MAGICKCORE_JBIG_DELEGATE)
77 static MagickBooleanType
78   WriteJBIGImage(const ImageInfo *,Image *,ExceptionInfo *);
79 #endif
80 \f
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
115   MagickStatusType
116     status;
117
118   Quantum
119     index;
120
121   register ssize_t
122     x;
123
124   register Quantum
125     *q;
126
127   register unsigned char
128     *p;
129
130   ssize_t
131     length,
132     y;
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);
152   image=AcquireImage(image_info,exception);
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);
163   jbg_dec_maxsize(&jbig_info,(unsigned long) image->columns,(unsigned long)
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   {
180     length=(ssize_t) ReadBlob(image,MagickMaxBufferExtent,buffer);
181     if (length == 0)
182       break;
183     p=buffer;
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;
191       length-=(ssize_t) count;
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);
199   image->compression=JBIG2Compression;
200   if (AcquireImageColormap(image,2,exception) == MagickFalse)
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;
211   image->resolution.x=300;
212   image->resolution.y=300;
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);
222   for (y=0; y < (ssize_t) image->rows; y++)
223   {
224     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
225     if (q == (Quantum *) NULL)
226       break;
227     bit=0;
228     byte=0;
229     for (x=0; x < (ssize_t) image->columns; x++)
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;
238       SetPixelIndex(image,index,q);
239       SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
240       q+=GetPixelChannels(image);
241     }
242     if (SyncAuthenticPixels(image,exception) == MagickFalse)
243       break;
244     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
245       image->rows);
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 \f
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 %
279 %      size_t RegisterJBIGImage(void)
280 %
281 */
282 ModuleExport size_t RegisterJBIGImage(void)
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 \f
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 \f
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 %
372 %      MagickBooleanType WriteJBIGImage(const ImageInfo *image_info,
373 %        Image *image,ExceptionInfo *exception)
374 %
375 %  A description of each parameter follows.
376 %
377 %    o image_info: the image info.
378 %
379 %    o image:  The image.
380 %
381 %    o exception: return any errors or warnings in this structure.
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,
395   Image *image,ExceptionInfo *exception)
396 {
397   double
398     version;
399
400   MagickBooleanType
401     status;
402
403   MagickOffsetType
404     scene;
405
406   register const Quantum
407     *p;
408
409   register ssize_t
410     x;
411
412   register unsigned char
413     *q;
414
415   size_t
416     number_packets;
417
418   ssize_t
419     y;
420
421   struct jbg_enc_state
422     jbig_info;
423
424   unsigned char
425     bit,
426     byte,
427     *pixels;
428
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);
438   assert(exception != (ExceptionInfo *) NULL);
439   assert(exception->signature == MagickSignature);
440   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
441   if (status == MagickFalse)
442     return(status);
443   version=StringToDouble(JBG_VERSION,(char **) NULL);
444   scene=0;
445   do
446   {
447     /*
448       Allocate pixel data.
449     */
450     if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
451       (void) TransformImageColorspace(image,sRGBColorspace,exception);
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     */
460     (void) SetImageType(image,BilevelType,exception);
461     q=pixels;
462     for (y=0; y < (ssize_t) image->rows; y++)
463     {
464       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
465       if (p == (const Quantum *) NULL)
466         break;
467       bit=0;
468       byte=0;
469       for (x=0; x < (ssize_t) image->columns; x++)
470       {
471         byte<<=1;
472         if (GetPixelIntensity(image,p) < (QuantumRange/2.0))
473           byte|=0x01;
474         bit++;
475         if (bit == 8)
476           {
477             *q++=byte;
478             bit=0;
479             byte=0;
480           }
481         p+=GetPixelChannels(image);
482       }
483       if (bit != 0)
484         *q++=byte << (8-bit);
485       if (image->previous == (Image *) NULL)
486         {
487           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
488             image->rows);
489           if (status == MagickFalse)
490             break;
491         }
492     }
493     /*
494       Initialize JBIG info structure.
495     */
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);
499     if (image_info->scene != 0)
500       jbg_enc_layers(&jbig_info,(int) image_info->scene);
501     else
502       {
503         size_t
504           x_resolution,
505           y_resolution;
506
507         x_resolution=640;
508         y_resolution=480;
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           {
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;
527           }
528         (void) jbg_enc_lrlmax(&jbig_info,(unsigned long) x_resolution,
529           (unsigned long) y_resolution);
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