2
% Copyright (C) 2003 GraphicsMagick Group
3
% Copyright (C) 2002 ImageMagick Studio
4
% Copyright 1991-1999 E. I. du Pont de Nemours and Company
6
% This program is covered by multiple licenses, which are described in
7
% Copyright.txt. You should have received a copy of Copyright.txt with this
8
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14
% JJJJJ BBBB IIIII GGGG %
18
% JJJ BBBB IIIII GGG %
21
% Read/Write JBIG Image Format. %
30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
#include "magick/studio.h"
39
#include "magick/blob.h"
40
#include "magick/cache.h"
41
#include "magick/magick.h"
42
#include "magick/monitor.h"
43
#include "magick/utility.h"
50
WriteJBIGImage(const ImageInfo *,Image *);
56
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60
% R e a d J B I G I m a g e %
64
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66
% Method ReadJBIGImage reads a JBIG image file and returns it. It
67
% allocates the memory necessary for the new Image structure and returns a
68
% pointer to the new image.
70
% The format of the ReadJBIGImage method is:
72
% Image *ReadJBIGImage(const ImageInfo *image_info,
73
% ExceptionInfo *exception)
75
% A description of each parameter follows:
77
% o image: Method ReadJBIGImage returns a pointer to the image after
78
% reading. A null image is returned if there is a memory shortage or
79
% if the image cannot be read.
81
% o image_info: Specifies a pointer to a ImageInfo structure.
83
% o exception: return any errors or warnings in this structure.
87
static Image *ReadJBIGImage(const ImageInfo *image_info,
88
ExceptionInfo *exception)
90
#define MaxBufferSize 8192
114
register unsigned char
133
assert(image_info != (const ImageInfo *) NULL);
134
assert(image_info->signature == MagickSignature);
135
assert(exception != (ExceptionInfo *) NULL);
136
assert(exception->signature == MagickSignature);
137
image=AllocateImage(image_info);
138
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
140
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
142
Initialize JBIG toolkit.
144
jbg_dec_init(&jbig_info);
145
jbg_dec_maxsize(&jbig_info,(unsigned long) image->columns,
146
(unsigned long) image->rows);
147
image->columns= jbg_dec_getwidth(&jbig_info);
148
image->rows= jbg_dec_getheight(&jbig_info);
150
image->storage_class=PseudoClass;
155
buffer=MagickAllocateMemory(unsigned char *,MaxBufferSize);
156
if (buffer == (unsigned char *) NULL)
157
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
161
length=(long) ReadBlob(image,MaxBufferSize,(char *) buffer);
166
while ((length > 0) && ((status == JBG_EAGAIN) || (status == JBG_EOK)))
168
status=jbg_dec_in(&jbig_info,p,length,&count);
172
} while ((status == JBG_EAGAIN) || (status == JBG_EOK));
176
image->columns=jbg_dec_getwidth(&jbig_info);
177
image->rows=jbg_dec_getheight(&jbig_info);
178
if (!AllocateImageColormap(image,2))
180
MagickFreeMemory(buffer);
181
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image)
183
image->colormap[0].red=0;
184
image->colormap[0].green=0;
185
image->colormap[0].blue=0;
186
image->colormap[1].red=MaxRGB;
187
image->colormap[1].green=MaxRGB;
188
image->colormap[1].blue=MaxRGB;
189
image->x_resolution=300;
190
image->y_resolution=300;
191
if (image_info->ping)
197
Convert X bitmap image to pixel packets.
199
p=jbg_dec_getimage(&jbig_info,0);
200
for (y=0; y < (long) image->rows; y++)
202
q=SetImagePixels(image,0,y,image->columns,1);
203
if (q == (PixelPacket *) NULL)
205
indexes=GetIndexes(image);
208
for (x=0; x < (long) image->columns; x++)
212
index=(byte & 0x80) ? 0 : 1;
218
*q++=image->colormap[index];
220
if (!SyncImagePixels(image))
222
if (QuantumTick(y,image->rows))
223
if (!MagickMonitor(LoadImageText,y,image->rows,exception))
229
jbg_dec_free(&jbig_info);
230
MagickFreeMemory(buffer);
237
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241
% R e g i s t e r J B I G I m a g e %
245
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
% Method RegisterJBIGImage adds attributes for the JBIG image format to
248
% the list of supported formats. The attributes include the image format
249
% tag, a method to read and/or write the format, whether the format
250
% supports the saving of more than one frame to the same file or blob,
251
% whether the format supports native in-memory I/O, and a brief
252
% description of the format.
254
% The format of the RegisterJBIGImage method is:
256
% RegisterJBIGImage(void)
259
ModuleExport void RegisterJBIGImage(void)
261
#define JBIGDescription "Joint Bi-level Image experts Group interchange format"
264
version[MaxTextExtent];
270
#if defined(JBG_VERSION)
271
(void) strncpy(version,JBG_VERSION,MaxTextExtent-1);
273
entry=SetMagickInfo("BIE");
275
entry->decoder=(DecoderHandler) ReadJBIGImage;
276
entry->encoder=(EncoderHandler) WriteJBIGImage;
279
entry->description=AcquireString(JBIGDescription);
280
if (*version != '\0')
281
entry->version=AcquireString(version);
282
entry->module=AcquireString("JBIG");
283
(void) RegisterMagickInfo(entry);
284
entry=SetMagickInfo("JBG");
286
entry->decoder=(DecoderHandler) ReadJBIGImage;
287
entry->encoder=(EncoderHandler) WriteJBIGImage;
289
entry->description=AcquireString(JBIGDescription);
290
if (*version != '\0')
291
entry->version=AcquireString(version);
292
entry->module=AcquireString("JBIG");
293
(void) RegisterMagickInfo(entry);
294
entry=SetMagickInfo("JBIG");
296
entry->decoder=(DecoderHandler) ReadJBIGImage;
297
entry->encoder=(EncoderHandler) WriteJBIGImage;
299
entry->description=AcquireString(JBIGDescription);
300
if (*version != '\0')
301
entry->version=AcquireString(version);
302
entry->module=AcquireString("JBIG");
303
(void) RegisterMagickInfo(entry);
307
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311
% U n r e g i s t e r J B I G I m a g e %
315
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317
% Method UnregisterJBIGImage removes format registrations made by the
318
% JBIG module from the list of supported formats.
320
% The format of the UnregisterJBIGImage method is:
322
% UnregisterJBIGImage(void)
325
ModuleExport void UnregisterJBIGImage(void)
327
(void) UnregisterMagickInfo("BIE");
328
(void) UnregisterMagickInfo("JBG");
329
(void) UnregisterMagickInfo("JBIG");
334
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338
% W r i t e J B I G I m a g e %
342
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
% Method WriteJBIGImage writes an image in the JBIG encoded image format.
346
% The format of the WriteJBIGImage method is:
348
% unsigned int WriteJBIGImage(const ImageInfo *image_info,Image *image)
350
% A description of each parameter follows.
352
% o status: Method WriteJBIGImage return True if the image is written.
353
% False is returned is there is a memory shortage or if the image file
356
% o image_info: Specifies a pointer to a ImageInfo structure.
358
% o image: A pointer to an Image structure.
363
static void JBIGEncode(unsigned char *pixels,size_t length,void *data)
368
image=(Image *) data;
369
(void) WriteBlob(image,length,pixels);
372
static unsigned int WriteJBIGImage(const ImageInfo *image_info,Image *image)
380
register const PixelPacket
389
register unsigned char
411
assert(image_info != (const ImageInfo *) NULL);
412
assert(image_info->signature == MagickSignature);
413
assert(image != (Image *) NULL);
414
assert(image->signature == MagickSignature);
415
status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
417
ThrowWriterException(FileOpenError,UnableToOpenFile,image);
418
jbig_lib_version=strtod(JBG_VERSION, (char **)NULL);
425
TransformColorspace(image,RGBColorspace);
426
number_packets=((image->columns+7) >> 3)*image->rows;
427
pixels=MagickAllocateMemory(unsigned char *,number_packets);
428
if (pixels == (unsigned char *) NULL)
429
ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image);
431
Convert pixels to a bitmap.
433
SetImageType(image,BilevelType);
434
polarity=PixelIntensityToQuantum(&image->colormap[0]) < (MaxRGB/2);
435
if (image->colors == 2)
436
polarity=PixelIntensityToQuantum(&image->colormap[0]) >
437
PixelIntensityToQuantum(&image->colormap[1]);
439
for (y=0; y < (long) image->rows; y++)
441
p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
442
if (p == (const PixelPacket *) NULL)
444
indexes=GetIndexes(image);
447
for (x=0; x < (long) image->columns; x++)
450
if (indexes[x] == polarity)
461
*q++=byte << (8-bit);
462
if (QuantumTick(y,image->rows))
463
if (!MagickMonitor(SaveImageText,y,image->rows,&image->exception))
467
Initialize JBIG info structure.
469
jbg_enc_init(&jbig_info,image->columns,image->rows,1,&pixels,
470
(void (*)(unsigned char *,size_t,void *)) JBIGEncode,image);
471
if (image_info->subimage != 0)
472
jbg_enc_layers(&jbig_info,(int) image_info->subimage);
485
if (image_info->density != (char *) NULL)
486
(void) GetGeometry(image_info->density,&sans_offset,&sans_offset,
487
&x_resolution,&y_resolution);
488
(void) jbg_enc_lrlmax(&jbig_info,x_resolution,y_resolution);
490
(void) jbg_enc_lrange(&jbig_info,-1,-1);
491
jbg_enc_options(&jbig_info, /* Encoder state */
492
JBG_ILEAVE| JBG_SMID, /* Order */
493
JBG_TPDON | JBG_TPBON | JBG_DPON, /* Options */
494
/* Lines per stripe in resolution layer 0. (was -1)*/
495
(jbig_lib_version < 1.6 ? -1 : 0),
501
jbg_enc_out(&jbig_info);
502
jbg_enc_free(&jbig_info);
503
MagickFreeMemory(pixels);
504
if (image->next == (Image *) NULL)
506
image=SyncNextImageInList(image);
507
if (!MagickMonitor(SaveImagesText,scene++,GetImageListLength(image),&image->exception))
509
} while (image_info->adjoin);
510
if (image_info->adjoin)
511
while (image->previous != (Image *) NULL)
512
image=image->previous;