This source file includes following definitions.
- WBMPReadInteger
- ReadWBMPImage
- RegisterWBMPImage
- UnregisterWBMPImage
- WBMPWriteInteger
- WriteWBMPImage
#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/exception.h"
#include "magick/exception-private.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/pixel-accessor.h"
#include "magick/quantum-private.h"
#include "magick/static.h"
#include "magick/string_.h"
#include "magick/module.h"
static MagickBooleanType
WriteWBMPImage(const ImageInfo *,Image *);
static MagickBooleanType WBMPReadInteger(Image *image,size_t *value)
{
int
byte;
*value=0;
do
{
byte=ReadBlobByte(image);
if (byte == EOF)
return(MagickFalse);
*value<<=7;
*value|=(unsigned int) (byte & 0x7f);
} while (byte & 0x80);
return(MagickTrue);
}
static Image *ReadWBMPImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
Image
*image;
int
byte;
MagickBooleanType
status;
register IndexPacket
*indexes;
register ssize_t
x;
register PixelPacket
*q;
ssize_t
y;
unsigned char
bit;
unsigned short
header;
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);
}
if (ReadBlob(image,2,(unsigned char *) &header) == 0)
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
if (header != 0)
ThrowReaderException(CoderError,"OnlyLevelZerofilesSupported");
if (WBMPReadInteger(image,&image->columns) == MagickFalse)
ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
if (WBMPReadInteger(image,&image->rows) == MagickFalse)
ThrowReaderException(CorruptImageError,"CorruptWBMPimage");
if ((image->columns == 0) || (image->rows == 0))
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
if (DiscardBlobBytes(image,image->offset) == MagickFalse)
ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
image->filename);
if (AcquireImageColormap(image,2) == MagickFalse)
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
if (image_info->ping != MagickFalse)
{
(void) CloseBlob(image);
return(GetFirstImageInList(image));
}
status=SetImageExtent(image,image->columns,image->rows);
if (status == MagickFalse)
{
InheritException(exception,&image->exception);
return(DestroyImageList(image));
}
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=ReadBlobByte(image);
if (byte == EOF)
ThrowReaderException(CorruptImageError,"CorruptImage");
}
SetPixelIndex(indexes+x,(byte & (0x01 << (7-bit))) ? 1 : 0);
bit++;
if (bit == 8)
bit=0;
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
break;
status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
image->rows);
if (status == MagickFalse)
break;
}
(void) SyncImage(image);
if (EOFBlob(image) != MagickFalse)
ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
image->filename);
(void) CloseBlob(image);
return(GetFirstImageInList(image));
}
ModuleExport size_t RegisterWBMPImage(void)
{
MagickInfo
*entry;
entry=SetMagickInfo("WBMP");
entry->decoder=(DecodeImageHandler *) ReadWBMPImage;
entry->encoder=(EncodeImageHandler *) WriteWBMPImage;
entry->adjoin=MagickFalse;
entry->description=ConstantString("Wireless Bitmap (level 0) image");
entry->module=ConstantString("WBMP");
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
}
ModuleExport void UnregisterWBMPImage(void)
{
(void) UnregisterMagickInfo("WBMP");
}
static void WBMPWriteInteger(Image *image,const size_t value)
{
int
bits,
flag,
n;
register ssize_t
i;
unsigned char
buffer[5],
octet;
n=1;
bits=28;
flag=MagickFalse;
for (i=4; i >= 0; i--)
{
octet=(unsigned char) ((value >> bits) & 0x7f);
if ((flag == 0) && (octet != 0))
{
flag=MagickTrue;
n=i+1;
}
buffer[4-i]=octet | (i && (flag || octet))*(0x01 << 7);
bits-=7;
}
(void) WriteBlob(image,(size_t) n,buffer+5-n);
}
static MagickBooleanType WriteWBMPImage(const ImageInfo *image_info,
Image *image)
{
MagickBooleanType
status;
register const PixelPacket
*p;
register ssize_t
x;
ssize_t
y;
unsigned char
bit,
byte;
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);
(void) TransformImageColorspace(image,sRGBColorspace);
(void) SetImageType(image,BilevelType);
(void) WriteBlobMSBShort(image,0);
WBMPWriteInteger(image,image->columns);
WBMPWriteInteger(image,image->rows);
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++)
{
if (GetPixelLuma(image,p) >= (QuantumRange/2.0))
byte|=0x1 << (7-bit);
bit++;
if (bit == 8)
{
(void) WriteBlobByte(image,byte);
bit=0;
byte=0;
}
p++;
}
if (bit != 0)
(void) WriteBlobByte(image,byte);
status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
image->rows);
if (status == MagickFalse)
break;
}
(void) CloseBlob(image);
return(MagickTrue);
}