modifications for VS2015, some

additional static/const's and a few bugfixes for HanXin and DotStream.

Patch by Michael <virtual_worlds@gmx.de>
This commit is contained in:
Robin Stuart 2016-09-05 22:06:50 +01:00
parent 8d3abf8440
commit 2c2200a7cb
31 changed files with 1639 additions and 1278 deletions

View file

@ -34,11 +34,11 @@
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "pcx.h" /* PCX header structure */
#include <math.h>
#ifdef _MSC_VER
#include <io.h>
#include <fcntl.h>
#include <malloc.h>
#endif
#define SSET "0123456789ABCDEF"
@ -53,232 +53,211 @@
#include <stdlib.h>
typedef struct s_statestruct {
unsigned char * pOut;
unsigned char *pIn;
unsigned int InLen;
unsigned int OutLength;
unsigned int OutPosCur;
unsigned int OutByteCountPos;
unsigned short ClearCode;
unsigned short FreeCode;
char fByteCountByteSet;
unsigned char OutBitsFree;
unsigned short NodeAxon[4096];
unsigned short NodeNext[4096];
unsigned char NodePix[4096];
unsigned char * pOut;
unsigned char *pIn;
unsigned int InLen;
unsigned int OutLength;
unsigned int OutPosCur;
unsigned int OutByteCountPos;
unsigned short ClearCode;
unsigned short FreeCode;
char fByteCountByteSet;
unsigned char OutBitsFree;
unsigned short NodeAxon[4096];
unsigned short NodeNext[4096];
unsigned char NodePix[4096];
} statestruct;
static char BufferNextByte(statestruct *pState)
{
(pState->OutPosCur)++;
/* Check if this position is a byte count position
* fg_f_bytecountbyte_set indicates, if byte count position bytes should be
* inserted in general.
* If this is true, and the distance to the last byte count position is 256
* (e.g. 255 bytes in between), a byte count byte is inserted, and the value
* of the last one is set to 255.
* */
if ( pState->fByteCountByteSet && ( pState->OutByteCountPos + 256 == pState->OutPosCur ) )
{
(pState->pOut)[pState->OutByteCountPos] = 255;
pState->OutByteCountPos = pState->OutPosCur;
(pState->OutPosCur)++;
}
if ( pState->OutPosCur >= pState->OutLength )
return 1;
(pState->pOut)[pState->OutPosCur]=0x00;
return 0;
static char BufferNextByte(statestruct *pState) {
(pState->OutPosCur)++;
/* Check if this position is a byte count position
* fg_f_bytecountbyte_set indicates, if byte count position bytes should be
* inserted in general.
* If this is true, and the distance to the last byte count position is 256
* (e.g. 255 bytes in between), a byte count byte is inserted, and the value
* of the last one is set to 255.
* */
if (pState->fByteCountByteSet && (pState->OutByteCountPos + 256 == pState->OutPosCur)) {
(pState->pOut)[pState->OutByteCountPos] = 255;
pState->OutByteCountPos = pState->OutPosCur;
(pState->OutPosCur)++;
}
if (pState->OutPosCur >= pState->OutLength)
return 1;
(pState->pOut)[pState->OutPosCur] = 0x00;
return 0;
}
static char AddCodeToBuffer(statestruct *pState, unsigned short CodeIn,unsigned char CodeBits)
{
/* Check, if we may fill up the current byte completely */
if (CodeBits >= pState->OutBitsFree)
{
(pState->pOut)[pState->OutPosCur] |= (unsigned char)
( CodeIn << ( 8 - pState->OutBitsFree ) );
if ( BufferNextByte(pState) )
return -1;
CodeIn = (unsigned short) ( CodeIn >> pState->OutBitsFree );
CodeBits -= pState->OutBitsFree;
pState->OutBitsFree = 8;
/* Write a full byte if there are at least 8 code bits left */
if (CodeBits >= pState->OutBitsFree)
{
(pState->pOut)[pState->OutPosCur] = (unsigned char) CodeIn;
if ( BufferNextByte(pState) )
return -1;
CodeIn = (unsigned short) ( CodeIn >> 8 );
CodeBits -= 8;
}
}
/* The remaining bits of CodeIn fit in the current byte. */
if( CodeBits > 0 )
{
(pState->pOut)[pState->OutPosCur] |= (unsigned char)
( CodeIn << ( 8 - pState->OutBitsFree ) );
pState->OutBitsFree -= CodeBits;
}
return 0;
static char AddCodeToBuffer(statestruct *pState, unsigned short CodeIn, unsigned char CodeBits) {
/* Check, if we may fill up the current byte completely */
if (CodeBits >= pState->OutBitsFree) {
(pState->pOut)[pState->OutPosCur] |= (unsigned char)
(CodeIn << (8 - pState->OutBitsFree));
if (BufferNextByte(pState))
return -1;
CodeIn = (unsigned short) (CodeIn >> pState->OutBitsFree);
CodeBits -= pState->OutBitsFree;
pState->OutBitsFree = 8;
/* Write a full byte if there are at least 8 code bits left */
if (CodeBits >= pState->OutBitsFree) {
(pState->pOut)[pState->OutPosCur] = (unsigned char) CodeIn;
if (BufferNextByte(pState))
return -1;
CodeIn = (unsigned short) (CodeIn >> 8);
CodeBits -= 8;
}
}
/* The remaining bits of CodeIn fit in the current byte. */
if (CodeBits > 0) {
(pState->pOut)[pState->OutPosCur] |= (unsigned char)
(CodeIn << (8 - pState->OutBitsFree));
pState->OutBitsFree -= CodeBits;
}
return 0;
}
static void FlushStringTable(statestruct *pState)
{
unsigned short Pos;
for( Pos = 0; Pos < pState->ClearCode; Pos++ )
{
(pState->NodeAxon)[Pos] = 0;
}
static void FlushStringTable(statestruct *pState) {
unsigned short Pos;
for (Pos = 0; Pos < pState->ClearCode; Pos++) {
(pState->NodeAxon)[Pos] = 0;
}
}
unsigned short FindPixelOutlet(statestruct *pState, unsigned short HeadNode, unsigned char Byte) {
unsigned short Outlet;
unsigned short FindPixelOutlet( statestruct *pState, unsigned short HeadNode, unsigned char Byte )
{
unsigned short Outlet;
Outlet = (pState->NodeAxon)[HeadNode];
while( Outlet )
{
if ( (pState->NodePix)[Outlet] == Byte )
return Outlet;
Outlet = (pState->NodeNext)[Outlet];
}
return 0;
Outlet = (pState->NodeAxon)[HeadNode];
while (Outlet) {
if ((pState->NodePix)[Outlet] == Byte)
return Outlet;
Outlet = (pState->NodeNext)[Outlet];
}
return 0;
}
static char NextCode ( statestruct *pState, unsigned char * pPixelValueCur, unsigned char CodeBits )
{
unsigned short UpNode;
unsigned short DownNode;
/* start with the root node for last pixel chain */
UpNode = *pPixelValueCur;
if ( (pState->InLen) == 0 )
return AddCodeToBuffer(pState, UpNode, CodeBits );
*pPixelValueCur = (*(pState->pIn))-'0';
(pState->pIn)++;
(pState->InLen)--;
/* Follow the string table and the data stream to the end of the longest string that has a code */
while( 0 != ( DownNode = FindPixelOutlet(pState, UpNode, *pPixelValueCur ) ) )
{
UpNode = DownNode;
if ( (pState->InLen) == 0 )
return AddCodeToBuffer(pState, UpNode, CodeBits);
*pPixelValueCur = (*(pState->pIn)) - '0';
(pState->pIn)++;
(pState->InLen)--;
}
/* Submit 'UpNode' which is the code of the longest string */
if ( AddCodeToBuffer(pState, UpNode, CodeBits ) )
return -1;
/* ... and extend the string by appending 'PixelValueCur' */
/* Create a successor node for 'PixelValueCur' whose code is 'freecode' */
(pState->NodePix)[pState->FreeCode] = *pPixelValueCur;
(pState->NodeAxon)[pState->FreeCode] = (pState->NodeNext)[pState->FreeCode]=0;
/* ...and link it to the end of the chain emanating from fg_axon[UpNode]. */
DownNode = (pState->NodeAxon)[UpNode];
if( ! DownNode )
{
(pState->NodeAxon)[UpNode] = pState->FreeCode;
} else {
while( (pState->NodeNext)[DownNode] )
{
DownNode = (pState->NodeNext)[DownNode];
}
(pState->NodeNext)[DownNode] = pState->FreeCode;
}
return 1;
static char NextCode(statestruct *pState, unsigned char * pPixelValueCur, unsigned char CodeBits) {
unsigned short UpNode;
unsigned short DownNode;
/* start with the root node for last pixel chain */
UpNode = *pPixelValueCur;
if ((pState->InLen) == 0)
return AddCodeToBuffer(pState, UpNode, CodeBits);
*pPixelValueCur = (*(pState->pIn)) - '0';
(pState->pIn)++;
(pState->InLen)--;
/* Follow the string table and the data stream to the end of the longest string that has a code */
while (0 != (DownNode = FindPixelOutlet(pState, UpNode, *pPixelValueCur))) {
UpNode = DownNode;
if ((pState->InLen) == 0)
return AddCodeToBuffer(pState, UpNode, CodeBits);
*pPixelValueCur = (*(pState->pIn)) - '0';
(pState->pIn)++;
(pState->InLen)--;
}
/* Submit 'UpNode' which is the code of the longest string */
if (AddCodeToBuffer(pState, UpNode, CodeBits))
return -1;
/* ... and extend the string by appending 'PixelValueCur' */
/* Create a successor node for 'PixelValueCur' whose code is 'freecode' */
(pState->NodePix)[pState->FreeCode] = *pPixelValueCur;
(pState->NodeAxon)[pState->FreeCode] = (pState->NodeNext)[pState->FreeCode] = 0;
/* ...and link it to the end of the chain emanating from fg_axon[UpNode]. */
DownNode = (pState->NodeAxon)[UpNode];
if (!DownNode) {
(pState->NodeAxon)[UpNode] = pState->FreeCode;
} else {
while ((pState->NodeNext)[DownNode]) {
DownNode = (pState->NodeNext)[DownNode];
}
(pState->NodeNext)[DownNode] = pState->FreeCode;
}
return 1;
}
int gif_lzw(unsigned char *pOut, int OutLength, unsigned char *pIn, int InLen)
{
unsigned char PixelValueCur;
unsigned char CodeBits;
unsigned short Pos;
statestruct State;
State.pIn = pIn;
State.InLen = InLen;
State.pOut = pOut;
State.OutLength = OutLength;
// > Get first data byte
if (State.InLen == 0)
return 0;
PixelValueCur = (unsigned char) ((*(State.pIn)) - '0');
(State.pIn)++;
(State.InLen)--;
CodeBits = 3;
State.ClearCode = 4;
State.FreeCode = 6;
State.OutBitsFree = 8;
State.OutPosCur = -1;
State.fByteCountByteSet = 0;
if ( BufferNextByte(&State) )
return 0;
for ( Pos = 0; Pos < State.ClearCode; Pos++)
State.NodePix[Pos] = (unsigned char) Pos;
FlushStringTable(&State);
/* Write what the GIF specification calls the "code size". */
(State.pOut)[State.OutPosCur] = 2;
/* Reserve first bytecount byte */
if ( BufferNextByte(&State) )
return 0;
State.OutByteCountPos = State.OutPosCur;
if ( BufferNextByte(&State) )
return 0;
State.fByteCountByteSet = 1;
/* Submit one 'ClearCode' as the first code */
if ( AddCodeToBuffer(&State, State.ClearCode, CodeBits) )
return 0;
for(;;)
{
char Res;
/* generate and save the next code, which may consist of multiple input pixels. */
Res = NextCode(&State, &PixelValueCur, CodeBits);
if ( Res < 0)
return 0;
//* Check for end of data stream */
if( ! Res )
{
/* submit 'eoi' as the last item of the code stream */
if ( AddCodeToBuffer(&State, (unsigned short)(State.ClearCode + 1), CodeBits ) )
return 0;
State.fByteCountByteSet = 0;
if( State.OutBitsFree < 8 )
{
if ( BufferNextByte(&State) )
return 0;
}
// > Update last bytecount byte;
if ( State.OutByteCountPos < State.OutPosCur )
{
(State.pOut)[State.OutByteCountPos] = (unsigned char) (State.OutPosCur - State.OutByteCountPos - 1);
}
State.OutPosCur++;
return State.OutPosCur;
}
/* Check for currently last code */
if( State.FreeCode == ( 1U << CodeBits ) )
CodeBits++;
State.FreeCode++;
/* Check for full stringtable */
if( State.FreeCode == 0xfff )
{
FlushStringTable(&State);
if ( AddCodeToBuffer(&State, State.ClearCode, CodeBits ) )
return 0;
CodeBits=(unsigned char)( 1 + 2 );
State.FreeCode=(unsigned short)( State.ClearCode + 2 );
}
}
int gif_lzw(unsigned char *pOut, int OutLength, unsigned char *pIn, int InLen) {
unsigned char PixelValueCur;
unsigned char CodeBits;
unsigned short Pos;
statestruct State;
State.pIn = pIn;
State.InLen = InLen;
State.pOut = pOut;
State.OutLength = OutLength;
// > Get first data byte
if (State.InLen == 0)
return 0;
PixelValueCur = (unsigned char) ((*(State.pIn)) - '0');
(State.pIn)++;
(State.InLen)--;
CodeBits = 3;
State.ClearCode = 4;
State.FreeCode = 6;
State.OutBitsFree = 8;
State.OutPosCur = -1;
State.fByteCountByteSet = 0;
if (BufferNextByte(&State))
return 0;
for (Pos = 0; Pos < State.ClearCode; Pos++)
State.NodePix[Pos] = (unsigned char) Pos;
FlushStringTable(&State);
/* Write what the GIF specification calls the "code size". */
(State.pOut)[State.OutPosCur] = 2;
/* Reserve first bytecount byte */
if (BufferNextByte(&State))
return 0;
State.OutByteCountPos = State.OutPosCur;
if (BufferNextByte(&State))
return 0;
State.fByteCountByteSet = 1;
/* Submit one 'ClearCode' as the first code */
if (AddCodeToBuffer(&State, State.ClearCode, CodeBits))
return 0;
for (;;) {
char Res;
/* generate and save the next code, which may consist of multiple input pixels. */
Res = NextCode(&State, &PixelValueCur, CodeBits);
if (Res < 0)
return 0;
//* Check for end of data stream */
if (!Res) {
/* submit 'eoi' as the last item of the code stream */
if (AddCodeToBuffer(&State, (unsigned short) (State.ClearCode + 1), CodeBits))
return 0;
State.fByteCountByteSet = 0;
if (State.OutBitsFree < 8) {
if (BufferNextByte(&State))
return 0;
}
// > Update last bytecount byte;
if (State.OutByteCountPos < State.OutPosCur) {
(State.pOut)[State.OutByteCountPos] = (unsigned char) (State.OutPosCur - State.OutByteCountPos - 1);
}
State.OutPosCur++;
return State.OutPosCur;
}
/* Check for currently last code */
if (State.FreeCode == (1U << CodeBits))
CodeBits++;
State.FreeCode++;
/* Check for full stringtable */
if (State.FreeCode == 0xfff) {
FlushStringTable(&State);
if (AddCodeToBuffer(&State, State.ClearCode, CodeBits))
return 0;
CodeBits = (unsigned char) (1 + 2);
State.FreeCode = (unsigned short) (State.ClearCode + 2);
}
}
}
int gif_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) {
@ -286,21 +265,21 @@ int gif_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
int errno;
int row, column;
FILE *gif_file;
unsigned short ImageWidth = image_width;
unsigned short ImageHeight = image_height;
unsigned short usTemp;
unsigned short ImageWidth = image_width;
unsigned short ImageHeight = image_height;
unsigned short usTemp;
int byte_out;
#ifdef _MSC_VER
char* rotated_bitmap;
char* rotated_bitmap;
char * lzwoutbuf;
#endif
#ifndef _MSC_VER
char rotated_bitmap[image_height * image_width];
char lzwoutbuf[image_height * image_width];
#else
rotated_bitmap = (char *) _alloca((image_height * image_width) * sizeof(char));
lzwoutbuf = (char *) _alloca((image_height * image_width) * sizeof(char));
rotated_bitmap = (char *) _alloca((image_height * image_width) * sizeof (char));
lzwoutbuf = (char *) _alloca((image_height * image_width) * sizeof (char));
#endif /* _MSC_VER */
switch (rotate_angle) {
@ -376,9 +355,9 @@ int gif_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
}
break;
}
/* Open output file in binary mode */
if ((symbol->output_options & BARCODE_STDOUT) != 0) {
#ifdef _MSC_VER
@ -400,26 +379,26 @@ int gif_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
rotated_bitmap[1] = 1;
rotated_bitmap[2] = 0;
rotated_bitmap[3] = 0;
*/
*/
/* GIF signature (6) */
memcpy(outbuf,"GIF87a",6);
if ( TRANSPARENT_INDEX != -1 )
outbuf[4]='9';
memcpy(outbuf, "GIF87a", 6);
if (TRANSPARENT_INDEX != -1)
outbuf[4] = '9';
fwrite(outbuf, 6, 1, gif_file);
/* Screen Descriptor (7) */
/* Screen Width */
usTemp=(unsigned short)ImageWidth;
outbuf[0] = (unsigned char)(0xff & usTemp);
outbuf[1] = (unsigned char)( (0xff00 & usTemp) / 0x100 );
usTemp = (unsigned short) ImageWidth;
outbuf[0] = (unsigned char) (0xff & usTemp);
outbuf[1] = (unsigned char) ((0xff00 & usTemp) / 0x100);
/* Screen Height */
usTemp=(unsigned short)ImageHeight;
outbuf[2] = (unsigned char)( 0xff & usTemp );
outbuf[3] = (unsigned char)( (0xff00 & usTemp) / 0x100 );
usTemp = (unsigned short) ImageHeight;
outbuf[2] = (unsigned char) (0xff & usTemp);
outbuf[3] = (unsigned char) ((0xff00 & usTemp) / 0x100);
/* write ImageBits-1 to the three least significant bits of byte 5 of
* the Screen Descriptor
*/
outbuf[4] = (unsigned char)( 0xf0 | (0x7&(DESTINATION_IMAGE_BITS-1)) );
outbuf[4] = (unsigned char) (0xf0 | (0x7 & (DESTINATION_IMAGE_BITS - 1)));
/* Background color = colortable index 0 */
outbuf[5] = 0x00;
/* Byte 7 must be 0x00 */
@ -427,21 +406,20 @@ int gif_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
fwrite(outbuf, 7, 1, gif_file);
/* Global Color Table (6) */
/* RGB 0 color */
outbuf[0] = (unsigned char)(16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]);
outbuf[1] = (unsigned char)(16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]);
outbuf[2] = (unsigned char)(16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]);
outbuf[0] = (unsigned char) (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]);
outbuf[1] = (unsigned char) (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]);
outbuf[2] = (unsigned char) (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]);
/* RGB 1 color */
outbuf[3] = (unsigned char)(16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]);
outbuf[4] = (unsigned char)(16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]);
outbuf[5] = (unsigned char)(16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]);
outbuf[3] = (unsigned char) (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]);
outbuf[4] = (unsigned char) (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]);
outbuf[5] = (unsigned char) (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]);
fwrite(outbuf, 6, 1, gif_file);
/* Graphic control extension (8) */
/* A graphic control extension block is used for overlay gifs.
* This is necessary to define a transparent color.
*/
if (TRANSPARENT_INDEX != -1)
{
if (TRANSPARENT_INDEX != -1) {
/* Extension Introducer = '!' */
outbuf[0] = '\x21';
/* Graphic Control Label */
@ -459,7 +437,7 @@ int gif_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
outbuf[4] = 0;
outbuf[5] = 0;
/* Transparent Color Index */
outbuf[6] = (unsigned char)TRANSPARENT_INDEX;
outbuf[6] = (unsigned char) TRANSPARENT_INDEX;
/* Block Terminator */
outbuf[7] = 0;
fwrite(outbuf, 8, 1, gif_file);
@ -474,35 +452,34 @@ int gif_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
outbuf[3] = 0x00;
outbuf[4] = 0x00;
/* Image Width (low byte first) */
outbuf[5] = (unsigned char)(0xff & ImageWidth);
outbuf[6] = (unsigned char)((0xff00 & ImageWidth) / 0x100);
outbuf[5] = (unsigned char) (0xff & ImageWidth);
outbuf[6] = (unsigned char) ((0xff00 & ImageWidth) / 0x100);
/* Image Height */
outbuf[7] = (unsigned char)(0xff & ImageHeight);
outbuf[8] = (unsigned char)((0xff00 & ImageHeight) / 0x100);
outbuf[7] = (unsigned char) (0xff & ImageHeight);
outbuf[8] = (unsigned char) ((0xff00 & ImageHeight) / 0x100);
/* Byte 10 contains the interlaced flag and
* information on the local color table.
* There is no local color table if its most significant bit is reset.
*/
outbuf[9] = (unsigned char)(0|(0x7 & (DESTINATION_IMAGE_BITS-1)));
outbuf[9] = (unsigned char) (0 | (0x7 & (DESTINATION_IMAGE_BITS - 1)));
fwrite(outbuf, 10, 1, gif_file);
/* call lzw encoding */
byte_out = gif_lzw(
(unsigned char *) lzwoutbuf,
image_height * image_width,
(unsigned char *) rotated_bitmap,
image_height * image_width);
if (byte_out <= 0)
{
(unsigned char *) lzwoutbuf,
image_height * image_width,
(unsigned char *) rotated_bitmap,
image_height * image_width);
if (byte_out <= 0) {
fclose(gif_file);
return ZINT_ERROR_MEMORY;
}
fwrite(lzwoutbuf, byte_out, 1, gif_file);
/* GIF terminator */
/* GIF terminator */
fputc('\x3b', gif_file);
fclose(gif_file);
return 0;
}