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