This source file includes following definitions.
- ReadJBIGImage
- RegisterJBIGImage
- UnregisterJBIGImage
- JBIGEncode
- WriteJBIGImage
#include "magick/studio.h"
#include "magick/attribute.h"
#include "magick/blob.h"
#include "magick/blob-private.h"
#include "magick/cache.h"
#include "magick/color-private.h"
#include "magick/colormap.h"
#include "magick/colorspace.h"
#include "magick/colorspace-private.h"
#include "magick/constitute.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/geometry.h"
#include "magick/image.h"
#include "magick/image-private.h"
#include "magick/list.h"
#include "magick/magick.h"
#include "magick/memory_.h"
#include "magick/monitor.h"
#include "magick/monitor-private.h"
#include "magick/nt-base-private.h"
#include "magick/nt-feature.h"
#include "magick/pixel-accessor.h"
#include "magick/quantum-private.h"
#include "magick/static.h"
#include "magick/string_.h"
#include "magick/string-private.h"
#include "magick/module.h"
#if defined(MAGICKCORE_JBIG_DELEGATE)
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
#include "jbig.h"
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
#endif
#if defined(MAGICKCORE_JBIG_DELEGATE)
static MagickBooleanType
WriteJBIGImage(const ImageInfo *,Image *);
#endif
#if defined(MAGICKCORE_JBIG_DELEGATE)
static Image *ReadJBIGImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
Image
*image;
IndexPacket
index;
MagickStatusType
status;
register IndexPacket
*indexes;
register ssize_t
x;
register PixelPacket
*q;
register unsigned char
*p;
ssize_t
length,
y;
struct jbg_dec_state
jbig_info;
unsigned char
bit,
*buffer,
byte;
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
if (image_info->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
image_info->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
image=AcquireImage(image_info);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
image=DestroyImageList(image);
return((Image *) NULL);
}
jbg_dec_init(&jbig_info);
jbg_dec_maxsize(&jbig_info,(unsigned long) image->columns,(unsigned long)
image->rows);
image->columns=jbg_dec_getwidth(&jbig_info);
image->rows=jbg_dec_getheight(&jbig_info);
image->depth=8;
image->storage_class=PseudoClass;
image->colors=2;
buffer=(unsigned char *) AcquireQuantumMemory(MagickMaxBufferExtent,
sizeof(*buffer));
if (buffer == (unsigned char *) NULL)
{
jbg_dec_free(&jbig_info);
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
}
status=JBG_EAGAIN;
do
{
length=(ssize_t) ReadBlob(image,MagickMaxBufferExtent,buffer);
if (length == 0)
break;
p=buffer;
while ((length > 0) && ((status == JBG_EAGAIN) || (status == JBG_EOK)))
{
size_t
count;
status=jbg_dec_in(&jbig_info,p,length,&count);
p+=count;
length-=(ssize_t) count;
}
} while ((status == JBG_EAGAIN) || (status == JBG_EOK));
image->columns=jbg_dec_getwidth(&jbig_info);
image->rows=jbg_dec_getheight(&jbig_info);
image->compression=JBIG2Compression;
if (AcquireImageColormap(image,2) == MagickFalse)
{
jbg_dec_free(&jbig_info);
buffer=(unsigned char *) RelinquishMagickMemory(buffer);
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
}
image->colormap[0].red=0;
image->colormap[0].green=0;
image->colormap[0].blue=0;
image->colormap[1].red=QuantumRange;
image->colormap[1].green=QuantumRange;
image->colormap[1].blue=QuantumRange;
image->x_resolution=300;
image->y_resolution=300;
if (image_info->ping != MagickFalse)
{
jbg_dec_free(&jbig_info);
buffer=(unsigned char *) RelinquishMagickMemory(buffer);
(void) CloseBlob(image);
return(GetFirstImageInList(image));
}
status=SetImageExtent(image,image->columns,image->rows);
if (status == MagickFalse)
{
jbg_dec_free(&jbig_info);
buffer=(unsigned char *) RelinquishMagickMemory(buffer);
InheritException(exception,&image->exception);
return(DestroyImageList(image));
}
p=jbg_dec_getimage(&jbig_info,0);
for (y=0; y < (ssize_t) image->rows; y++)
{
q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
if (q == (PixelPacket *) NULL)
break;
indexes=GetAuthenticIndexQueue(image);
bit=0;
byte=0;
for (x=0; x < (ssize_t) image->columns; x++)
{
if (bit == 0)
byte=(*p++);
index=(byte & 0x80) ? 0 : 1;
bit++;
byte<<=1;
if (bit == 8)
bit=0;
SetPixelIndex(indexes+x,index);
SetPixelRGBO(q,image->colormap+(ssize_t) index);
q++;
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
break;
status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
image->rows);
if (status == MagickFalse)
break;
}
jbg_dec_free(&jbig_info);
buffer=(unsigned char *) RelinquishMagickMemory(buffer);
(void) CloseBlob(image);
return(GetFirstImageInList(image));
}
#endif
ModuleExport size_t RegisterJBIGImage(void)
{
#define JBIGDescription "Joint Bi-level Image experts Group interchange format"
char
version[MaxTextExtent];
MagickInfo
*entry;
*version='\0';
#if defined(JBG_VERSION)
(void) CopyMagickString(version,JBG_VERSION,MaxTextExtent);
#endif
entry=SetMagickInfo("BIE");
#if defined(MAGICKCORE_JBIG_DELEGATE)
entry->decoder=(DecodeImageHandler *) ReadJBIGImage;
entry->encoder=(EncodeImageHandler *) WriteJBIGImage;
#endif
entry->adjoin=MagickFalse;
entry->description=ConstantString(JBIGDescription);
if (*version != '\0')
entry->version=ConstantString(version);
entry->module=ConstantString("JBIG");
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("JBG");
#if defined(MAGICKCORE_JBIG_DELEGATE)
entry->decoder=(DecodeImageHandler *) ReadJBIGImage;
entry->encoder=(EncodeImageHandler *) WriteJBIGImage;
#endif
entry->description=ConstantString(JBIGDescription);
if (*version != '\0')
entry->version=ConstantString(version);
entry->module=ConstantString("JBIG");
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("JBIG");
#if defined(MAGICKCORE_JBIG_DELEGATE)
entry->decoder=(DecodeImageHandler *) ReadJBIGImage;
entry->encoder=(EncodeImageHandler *) WriteJBIGImage;
#endif
entry->description=ConstantString(JBIGDescription);
if (*version != '\0')
entry->version=ConstantString(version);
entry->module=ConstantString("JBIG");
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
}
ModuleExport void UnregisterJBIGImage(void)
{
(void) UnregisterMagickInfo("BIE");
(void) UnregisterMagickInfo("JBG");
(void) UnregisterMagickInfo("JBIG");
}
#if defined(MAGICKCORE_JBIG_DELEGATE)
static void JBIGEncode(unsigned char *pixels,size_t length,void *data)
{
Image
*image;
image=(Image *) data;
(void) WriteBlob(image,length,pixels);
}
static MagickBooleanType WriteJBIGImage(const ImageInfo *image_info,
Image *image)
{
double
version;
MagickBooleanType
status;
MagickOffsetType
scene;
MemoryInfo
*pixel_info;
register const PixelPacket
*p;
register ssize_t
x;
register unsigned char
*q;
size_t
number_packets;
ssize_t
y;
struct jbg_enc_state
jbig_info;
unsigned char
bit,
byte,
*pixels;
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
assert(image != (Image *) NULL);
assert(image->signature == MagickSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
if (status == MagickFalse)
return(status);
version=StringToDouble(JBG_VERSION,(char **) NULL);
scene=0;
do
{
(void) TransformImageColorspace(image,sRGBColorspace);
number_packets=(image->columns+7)/8;
pixel_info=AcquireVirtualMemory(number_packets,image->rows*sizeof(*pixels));
if (pixel_info == (MemoryInfo *) NULL)
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
(void) SetImageType(image,BilevelType);
q=pixels;
for (y=0; y < (ssize_t) image->rows; y++)
{
p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
if (p == (const PixelPacket *) NULL)
break;
bit=0;
byte=0;
for (x=0; x < (ssize_t) image->columns; x++)
{
byte<<=1;
if (GetPixelLuma(image,p) < (QuantumRange/2.0))
byte|=0x01;
bit++;
if (bit == 8)
{
*q++=byte;
bit=0;
byte=0;
}
p++;
}
if (bit != 0)
*q++=byte << (8-bit);
if (image->previous == (Image *) NULL)
{
status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
image->rows);
if (status == MagickFalse)
break;
}
}
jbg_enc_init(&jbig_info,(unsigned long) image->columns,(unsigned long)
image->rows,1,&pixels,(void (*)(unsigned char *,size_t,void *))
JBIGEncode,image);
if (image_info->scene != 0)
jbg_enc_layers(&jbig_info,(int) image_info->scene);
else
{
size_t
x_resolution,
y_resolution;
x_resolution=640;
y_resolution=480;
if (image_info->density != (char *) NULL)
{
GeometryInfo
geometry_info;
MagickStatusType
flags;
flags=ParseGeometry(image_info->density,&geometry_info);
x_resolution=geometry_info.rho;
y_resolution=geometry_info.sigma;
if ((flags & SigmaValue) == 0)
y_resolution=x_resolution;
}
if (image->units == PixelsPerCentimeterResolution)
{
x_resolution=(size_t) (100.0*2.54*x_resolution+0.5)/100.0;
y_resolution=(size_t) (100.0*2.54*y_resolution+0.5)/100.0;
}
(void) jbg_enc_lrlmax(&jbig_info,(unsigned long) x_resolution,
(unsigned long) y_resolution);
}
(void) jbg_enc_lrange(&jbig_info,-1,-1);
jbg_enc_options(&jbig_info,JBG_ILEAVE | JBG_SMID,JBG_TPDON | JBG_TPBON |
JBG_DPON,version < 1.6 ? -1 : 0,-1,-1);
jbg_enc_out(&jbig_info);
jbg_enc_free(&jbig_info);
pixel_info=RelinquishVirtualMemory(pixel_info);
if (GetNextImageInList(image) == (Image *) NULL)
break;
image=SyncNextImageInList(image);
status=SetImageProgress(image,SaveImagesTag,scene++,
GetImageListLength(image));
if (status == MagickFalse)
break;
} while (image_info->adjoin != MagickFalse);
(void) CloseBlob(image);
return(MagickTrue);
}
#endif