root / ImageMagick / trunk / coders / jbig.c

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