Browse Source

Did not fix bitmap struct alignment issue in linux, reverting

refactor
suhrke 5 years ago
parent
commit
2313bbee5a
  1. 18
      source/Bitmap.h
  2. 34
      source/Decoder.cpp
  3. 8
      source/Printer.cpp
  4. 4
      source/Printer.h

18
source/Bitmap.h

@ -25,16 +25,16 @@ @@ -25,16 +25,16 @@
typedef struct BitmapFileHeader {
typedef struct BITMAPFILEHEADER {
uint16_t bfType = 19778; //filetype, must be 'BM' (19778)
uint32_t bfSize; //size in bytes of bitmap file
uint16_t bfReserved1 = 0; //unused, except to keep alignment
uint16_t bfReserved2 = 0; //unused, except to keep alignment
uint32_t bfOffBits; //offset in bytes from beginning of
// BITMAPFILEHEADER to the bitmap bits
} BitmapFileHeader;
} BITMAPFILEHEADER;
typedef struct BitmapInfoHeader {
typedef struct BITMAPINFOHEADER {
uint32_t biSize; //# of bytes in struct
int32_t biWidth; //width of bitmap in pixels
int32_t biHeight; //height of bitmap in pixels
@ -49,18 +49,18 @@ typedef struct BitmapInfoHeader { @@ -49,18 +49,18 @@ typedef struct BitmapInfoHeader {
// if 0, bitmap uses max # of colors that
// correspond to biBitCount
uint32_t biClrImportant; //# of color indices required for displaying bitmap
} BitmapInfoHeader;
} BITMAPINFOHEADER;
typedef struct RgbQuad {
typedef struct RGBQUAD {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved = 0;
} RgbQuad;
} RGBQUAD;
typedef struct BitmapInfo {
BitmapInfoHeader bmiHeader;
RgbQuad bmiColors[1];
typedef struct BITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;

34
source/Decoder.cpp

@ -34,10 +34,12 @@ @@ -34,10 +34,12 @@
#include <stdio.h>
#include <sstream>
#include "Crc16.h"
#include "Bitmap.h"
#include "Decoder.h"
#include "Ecc.h"
#include "Fileproc.h"
#ifdef __linux__
#include "Bitmap.h"
#endif
#define NHYST 1024 // Number of points in histogramm
#define NPEAK 32 // Maximal number of peaks
@ -1039,10 +1041,10 @@ void Stopbitmapdecoding(t_procdata *pdata) { @@ -1039,10 +1041,10 @@ void Stopbitmapdecoding(t_procdata *pdata) {
// Opens and decodes bitmap. Returns 0 on success and -1 on error.
int Decodebitmap(const std::string &fileName) {
int i,size;
uchar *data,buf[sizeof(BitmapFileHeader)+sizeof(BitmapInfoHeader)];
uchar *data,buf[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
FILE *f;
BitmapFileHeader *pbfh;
BitmapInfoHeader *pbih;
BITMAPFILEHEADER *pbfh;
BITMAPINFOHEADER *pbih;
//HCURSOR prevcursor; //GUI
// Ask for file name.
//!!! REQUIRE input bmp exists before this can be run
@ -1078,8 +1080,8 @@ int Decodebitmap(const std::string &fileName) { @@ -1078,8 +1080,8 @@ int Decodebitmap(const std::string &fileName) {
fclose(f);
return -1;
};
pbfh=(BitmapFileHeader *)buf;
pbih=(BitmapInfoHeader *)(buf+sizeof(BitmapFileHeader));
pbfh=(BITMAPFILEHEADER *)buf;
pbih=(BITMAPINFOHEADER *)(buf+sizeof(BITMAPFILEHEADER));
std::cout << "Size of bmp file header: " << sizeof(*pbfh) << std::endl;
std::cout << "Size of bmp info header: " << sizeof(*pbih) << std::endl;
if ( pbfh->bfType!=19778 ) {//First two bytes must be 'BM' (19778)
@ -1116,7 +1118,7 @@ int Decodebitmap(const std::string &fileName) { @@ -1116,7 +1118,7 @@ int Decodebitmap(const std::string &fileName) {
Reporterror(oss.str());
return -1;
}
if ( pbih->biSize!=sizeof(BitmapInfoHeader) || pbih->biPlanes!=1 ) {
if ( pbih->biSize!=sizeof(BITMAPINFOHEADER) || pbih->biPlanes!=1 ) {
std::ostringstream oss;
oss << "Unsupported Bitmap: size mismatch or invalid planes value (internal error): " << fileName << std::endl;
Reporterror(oss.str());
@ -1125,14 +1127,14 @@ int Decodebitmap(const std::string &fileName) { @@ -1125,14 +1127,14 @@ int Decodebitmap(const std::string &fileName) {
};
// Allocate buffer and read file.
fseek(f,0,SEEK_END);
size=ftell(f)-sizeof(BitmapFileHeader);
size=ftell(f)-sizeof(BITMAPFILEHEADER);
data=(uchar *)malloc(size);
if (data==NULL) { // Unable to allocate memory
Reporterror("Low memory");
fclose(f);
return -1;
};
fseek(f,sizeof(BitmapFileHeader),SEEK_SET);
fseek(f,sizeof(BITMAPFILEHEADER),SEEK_SET);
i=fread(data,1,size,f);
fclose(f);
if (i!=size) { // Unable to read bitmap
@ -1143,7 +1145,7 @@ int Decodebitmap(const std::string &fileName) { @@ -1143,7 +1145,7 @@ int Decodebitmap(const std::string &fileName) {
return -1;
};
// Process bitmap.
ProcessDIB(data,pbfh->bfOffBits-sizeof(BitmapFileHeader));
ProcessDIB(data,pbfh->bfOffBits-sizeof(BITMAPFILEHEADER));
free(data);
return 0;
};
@ -1154,13 +1156,13 @@ int Decodebitmap(const std::string &fileName) { @@ -1154,13 +1156,13 @@ int Decodebitmap(const std::string &fileName) {
int ProcessDIB(void *hdata,int offset) {
int i,j,sizex,sizey,ncolor;
uchar scale[256],*data,*pdata,*pbits;
BitmapInfo *pdib;
//pdib=(BitmapInfo *)GlobalLock(hdata);
pdib =(BitmapInfo *)hdata;
BITMAPINFO *pdib;
//pdib=(BITMAPINFO *)GlobalLock(hdata);
pdib =(BITMAPINFO *)hdata;
if (pdib==NULL)
return -1; // Something is wrong with this DIB
// Check that bitmap is more or less valid.
if (pdib->bmiHeader.biSize!=sizeof(BitmapInfoHeader) ||
if (pdib->bmiHeader.biSize!=sizeof(BITMAPINFOHEADER) ||
pdib->bmiHeader.biPlanes!=1 ||
(pdib->bmiHeader.biBitCount!=8 && pdib->bmiHeader.biBitCount!=24) ||
(pdib->bmiHeader.biBitCount==24 && pdib->bmiHeader.biClrUsed!=0) ||
@ -1188,7 +1190,7 @@ int ProcessDIB(void *hdata,int offset) { @@ -1188,7 +1190,7 @@ int ProcessDIB(void *hdata,int offset) {
else {
for (i=0; i<256; i++) scale[i]=(uchar)i; };
if (offset==0)
offset=sizeof(BitmapInfoHeader)+ncolor*sizeof(RgbQuad);
offset=sizeof(BITMAPINFOHEADER)+ncolor*sizeof(RGBQUAD);
pdata=data;
for (j=0; j<sizey; j++) {
offset=(offset+3) & 0xFFFFFFFC;
@ -1200,7 +1202,7 @@ int ProcessDIB(void *hdata,int offset) { @@ -1200,7 +1202,7 @@ int ProcessDIB(void *hdata,int offset) {
else {
// 24-bit bitmap without palette.
if (offset==0)
offset=sizeof(BitmapInfoHeader)+ncolor*sizeof(RgbQuad);
offset=sizeof(BITMAPINFOHEADER)+ncolor*sizeof(RGBQUAD);
pdata=data;
for (j=0; j<sizey; j++) {
offset=(offset+3) & 0xFFFFFFFC;

8
source/Printer.cpp

@ -458,8 +458,8 @@ void Initializeprinting(t_printdata *print) { @@ -458,8 +458,8 @@ void Initializeprinting(t_printdata *print) {
// Fill in bitmap header. To simplify processing, I use 256-color bitmap
// (1 byte per pixel).
pbmi=(BITMAPINFO *)print->bmi;
memset(pbmi,0,sizeof(BitmapInfoHeader));
pbmi->bmiHeader.biSize=sizeof(BitmapInfoHeader);
memset(pbmi,0,sizeof(BITMAPINFOHEADER));
pbmi->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth=width;
pbmi->bmiHeader.biHeight=height;
pbmi->bmiHeader.biPlanes=1;
@ -608,7 +608,7 @@ void Printnextpage(t_printdata *print) { @@ -608,7 +608,7 @@ void Printnextpage(t_printdata *print) {
uchar *bits;
ulong u,size,pagesize,offset;
t_data block,cksum;
BitmapFileHeader bmfh;
BITMAPFILEHEADER bmfh;
BITMAPINFO *pbmi;
// Calculate offset of this page in data.
offset=print->frompage*print->pagesize;
@ -809,7 +809,7 @@ void Printnextpage(t_printdata *print) { @@ -809,7 +809,7 @@ void Printnextpage(t_printdata *print) {
// Create and save bitmap file header.
success=1;
n=sizeof(BitmapInfoHeader)+256*sizeof(RgbQuad);
n=sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
bmfh.bfType=19778; //First two bytes are 'BM' (19778)
bmfh.bfSize=sizeof(bmfh)+n+width*height;
bmfh.bfReserved1=bmfh.bfReserved2=0;

4
source/Printer.h

@ -21,8 +21,8 @@ @@ -21,8 +21,8 @@
#include <cstring>
#include <string>
#include "Global.h"
#include "Bitmap.h"
#ifdef __linux__
#include "Bitmap.h"
#include <sys/stat.h>
#endif
@ -83,7 +83,7 @@ typedef struct t_printdata { // Print control structure @@ -83,7 +83,7 @@ typedef struct t_printdata { // Print control structure
int border; // Border around the data grid, pixels
uchar *dibbits; // Pointer to DIB bits
uchar *drawbits; // Pointer to file bitmap bits
uchar bmi[sizeof(BitmapInfo)+256*sizeof(RgbQuad)]; // Bitmap info
uchar bmi[sizeof(BITMAPINFO)+256*sizeof(RGBQUAD)]; // Bitmap info
int startdoc; // Print job started
} t_printdata;

Loading…
Cancel
Save