bmpf.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:26k
- /*
- Copyright (C) 2007 Ibadov Tariel <itk@bk.ru>
- */
- #include <fstream.h>
- #include "bmpf.h"
- #include "tchar.h"
- static unsigned char bmpHeader[62];
- static unsigned char bmpHeader1[62] =
- { 'B', 'M', 0xBE, 0x48, 0x28, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00,
- 0x00, 0x00, 0xA4, 0x01, 0x00, 0x00, 0x9D, 0x00,
- 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xBC, 0x04, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- static unsigned char bmpHeader2[62] =
- { 'B', 'M', 0x3E, 0xF9, 0x15, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00,
- 0x00, 0x00, 0xA4, 0x01, 0x00, 0x00, 0x9D, 0x00,
- 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xBC, 0x04, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- //==========================================================================================
- //PDF
- //==========================================================================================
- static unsigned char pdfHeader[9] =
- { '%', 'P', 'D', 'F', '-', '1', '.', '2',0x0a};
- int pdf_save( char *filename, BMPINFO *pbmpinfo )
- {
- std::string namepdf;
- namepdf=filename;
- namepdf +=".pdf";
- ofstream ofPdf;
- ofPdf.open((const char *)namepdf.c_str(), ios::out | ios::binary );
- //-----------
- ofPdf.write((const char *) pdfHeader, 9 );
-
- ofPdf.write("1 0 objn", 8 );
- ofPdf.write("<<n", 3 );
- ofPdf.write("/Type /Catalogn", 15 );
- ofPdf.write("/Pages 2 0 Rn", 13 );
- ofPdf.write("/OpenAction [3 0 R /XYZ -32768 -32768 1 ]n", 42 );
- ofPdf.write(">>n", 3 );
- ofPdf.write("endobjn", 7 );
- ofPdf.write("2 0 objn", 8 );
- ofPdf.write("<<n", 3 );
- ofPdf.write("/Type /Pagesn", 13 );
- ofPdf.write("/Kids [ 3 0 R ]n", 16 );
- ofPdf.write("/Count 1n", 9 );
- ofPdf.write(">>n", 3 );
- ofPdf.write("endobjn", 7 );
- ofPdf.write("3 0 objn", 8 );
- ofPdf.write("<<n", 3 );
- ofPdf.write("/Type /Pagen", 12 );
- ofPdf.write("/Parent 2 0 Rn", 14 );
- ofPdf.write("/Resourcesn", 11 );
- ofPdf.write("<<n", 3 );
- ofPdf.write("/XObject << /Im0 4 0 R >>n", 26 );
- ofPdf.write("/ProcSet [ /PDF /ImageC ]n", 26 );
- ofPdf.write(">>n", 3 );
- if (pbmpinfo->height==600)
- ofPdf.write("/MediaBox [ 0 0 800 600 ]n", 26 );
- else
- ofPdf.write("/MediaBox [ 0 0 800 1100 ]n", 27 );
- ofPdf.write("/Contents 5 0 Rn", 16 );
- ofPdf.write(">>n", 3 );
- ofPdf.write("endobjn", 7 );
- ofPdf.write("4 0 objn", 8 );
- ofPdf.write("<<n", 3 );
- ofPdf.write("/Type /XObjectn", 15 );
- ofPdf.write("/Subtype /Imagen", 16 );
- ofPdf.write("/Name /Im0n", 11 );
- ofPdf.write("/Width 800n", 11 );
- if (pbmpinfo->height==600)
- ofPdf.write("/Height 600n", 12 );
- else
- ofPdf.write("/Height 1100n", 13 );
- ofPdf.write("/BitsPerComponent 8n", 20 );
- ofPdf.write("/Filter []n", 11 );
- ofPdf.write("/ColorSpace /DeviceRGBn", 23 );
- if (pbmpinfo->height==600)
- ofPdf.write("/Length 1440000 >>n", 19 );
- else
- ofPdf.write("/Length 2640000 >>n", 19 );
- ofPdf.write("streamn", 7 );
- ///==============================================================
- ///==============================================================
-
- unsigned long w = pbmpinfo->width;
- unsigned long h = pbmpinfo->height;
- int bpl2 = w * 3;
- //unsigned char *p = pbmpinfo->bits + (bpl2 * h);
- unsigned char *p = pbmpinfo->bits;
-
- for( unsigned y = 0; y < h; y++ ) {
- p += bpl2;
- ofPdf.write( (const char *)p, bpl2 );
- }
-
- ///==============================================================
- ///==============================================================
-
- ofPdf.write("endstreamn", 10 );
- ofPdf.write("endobjn", 7 );
- ofPdf.write("5 0 objn", 8 );
- ofPdf.write("<< /Length 32 >>n", 17 );
- ofPdf.write("streamn", 7 );
- ofPdf.write("qn", 2 );
-
- if (pbmpinfo->height==600)
- ofPdf.write("800 0 0 600 0 0 cmn", 19 );
- else
- ofPdf.write("800 0 0 1100 0 0 cmn", 20 );
-
- ofPdf.write("/Im0 Don", 8 );
- ofPdf.write("Qn", 2 );
-
- ofPdf.write("endstreamn", 10 );
- ofPdf.write("endobjn", 7 );
- ofPdf.write("xrefn", 5 );
- ofPdf.write("0 6n", 4 );
- ofPdf.write("0000000000 65535 fn", 19 );
- ofPdf.write("0000000009 00000 nn", 19 );
- ofPdf.write("0000000100 00000 nn", 19 );
- ofPdf.write("0000000159 00000 nn", 19 );
- ofPdf.write("0000000318 00000 nn", 19 );
- ofPdf.write("0002640492 00000 nn", 19 );
- ofPdf.write("trailern", 8 );
- ofPdf.write("<<n", 3 );
- ofPdf.write("/Size 6n", 8 );
- ofPdf.write("/Root 1 0 Rn", 12 );
- ofPdf.write(">>n", 3 );
- ofPdf.write("startxrefn", 10 );
- ofPdf.write("2640573n", 8 );
- ofPdf.write("%%EOF", 5 );
-
- //-----------
- ofPdf.close();
- return 0;
- }
- int bmp_save( char *filename, BMPINFO *pbmpinfo )
- {
- if (pbmpinfo->height==600){
- for(int i=0;i<62;i++)
- bmpHeader[i]=bmpHeader2[i];
- }else {
- for(int i=0;i<62;i++)
- bmpHeader[i]=bmpHeader1[i];
- }
-
- int endy = pbmpinfo->height - 1;
- int bpl = pbmpinfo->width*3;
- ofstream ofBmp;
- ofBmp.open( filename, ios::out | ios::binary );
-
- unsigned long w = pbmpinfo->width;
- unsigned long h = endy + 1;
-
- ofBmp.write((const char *) bmpHeader, 18 );
-
- ofBmp.put( char( w & 0x000000FF) );
- ofBmp.put( char((w>> 8) & 0x000000FF) );
- ofBmp.put( char((w>>16) & 0x000000FF) );
- ofBmp.put( char((w>>24) & 0x000000FF) );
- ofBmp.put( char( h & 0x000000FF) );
- ofBmp.put( char((h>> 8) & 0x000000FF) );
- ofBmp.put( char((h>>16) & 0x000000FF) );
- ofBmp.put( char((h>>24) & 0x000000FF) );
- ofBmp.write((const char *) bmpHeader + 26, sizeof(bmpHeader) - 26 );
-
- unsigned char *p = pbmpinfo->bits + bpl * h;
- int bpl2 = w * 3;
- for( unsigned y = 0; y < h; y++ ) {
- p -= bpl;
- ofBmp.write( (const char *)p, bpl2 );
- }
- ofBmp.close();
- return 0;
- }
- //================================================================
- // TIFF
- //================================================================
- enum BMPType
- {
- BMPT_WIN4, /* BMP used in Windows 3.0/NT 3.51/95 */
- BMPT_WIN5, /* BMP used in Windows NT 4.0/98/Me/2000/XP */
- BMPT_OS21, /* BMP used in OS/2 PM 1.x */
- BMPT_OS22 /* BMP used in OS/2 PM 2.x */
- };
- enum BMPComprMethod
- {
- BMPC_RGB = 0L, /* Uncompressed */
- BMPC_RLE8 = 1L, /* RLE for 8 bpp images */
- BMPC_RLE4 = 2L, /* RLE for 4 bpp images */
- BMPC_BITFIELDS = 3L, /* Bitmap is not compressed and the colour table
- * consists of three DWORD color masks that specify
- * the red, green, and blue components of each
- * pixel. This is valid when used with
- * 16- and 32-bpp bitmaps. */
- BMPC_JPEG = 4L, /* Indicates that the image is a JPEG image. */
- BMPC_PNG = 5L /* Indicates that the image is a PNG image. */
- };
- enum BMPLCSType /* Type of logical color space. */
- {
- BMPLT_CALIBRATED_RGB = 0, /* This value indicates that endpoints and
- * gamma values are given in the appropriate
- * fields. */
- BMPLT_DEVICE_RGB = 1,
- BMPLT_DEVICE_CMYK = 2
- };
- typedef struct
- {
- int32 iCIEX;
- int32 iCIEY;
- int32 iCIEZ;
- } BMPCIEXYZ;
- typedef struct /* This structure contains the x, y, and z */
- { /* coordinates of the three colors that */
- /* correspond */
- BMPCIEXYZ iCIERed; /* to the red, green, and blue endpoints for */
- BMPCIEXYZ iCIEGreen; /* a specified logical color space. */
- BMPCIEXYZ iCIEBlue;
- } BMPCIEXYZTriple;
- typedef struct
- {
- char bType[2]; /* Signature "BM" */
- uint32 iSize; /* Size in bytes of the bitmap file. Should
- * always be ignored while reading because
- * of error in Windows 3.0 SDK's description
- * of this field */
- uint16 iReserved1; /* Reserved, set as 0 */
- uint16 iReserved2; /* Reserved, set as 0 */
- uint32 iOffBits; /* Offset of the image from file start in bytes */
- } BMPFileHeader;
- /* File header size in bytes: */
- const int BFH_SIZE = 14;
- typedef struct
- {
- uint32 iSize; /* Size of BMPInfoHeader structure in bytes.
- * Should be used to determine start of the
- * colour table */
- int32 iWidth; /* Image width */
- int32 iHeight; /* Image height. If positive, image has bottom
- * left origin, if negative --- top left. */
- int16 iPlanes; /* Number of image planes (must be set to 1) */
- int16 iBitCount; /* Number of bits per pixel (1, 4, 8, 16, 24
- * or 32). If 0 then the number of bits per
- * pixel is specified or is implied by the
- * JPEG or PNG format. */
- uint32 iCompression; /* Compression method */
- uint32 iSizeImage; /* Size of uncomressed image in bytes. May
- * be 0 for BMPC_RGB bitmaps. If iCompression
- * is BI_JPEG or BI_PNG, iSizeImage indicates
- * the size of the JPEG or PNG image buffer. */
- int32 iXPelsPerMeter; /* X resolution, pixels per meter (0 if not used) */
- int32 iYPelsPerMeter; /* Y resolution, pixels per meter (0 if not used) */
- uint32 iClrUsed; /* Size of colour table. If 0, iBitCount should
- * be used to calculate this value
- * (1<<iBitCount). This value should be
- * unsigned for proper shifting. */
- int32 iClrImportant; /* Number of important colours. If 0, all
- * colours are required */
- /*
- * Fields above should be used for bitmaps, compatible with Windows NT 3.51
- * and earlier. Windows 98/Me, Windows 2000/XP introduces additional fields:
- */
- int32 iRedMask; /* Colour mask that specifies the red component
- * of each pixel, valid only if iCompression
- * is set to BI_BITFIELDS. */
- int32 iGreenMask; /* The same for green component */
- int32 iBlueMask; /* The same for blue component */
- int32 iAlphaMask; /* Colour mask that specifies the alpha
- * component of each pixel. */
- uint32 iCSType; /* Colour space of the DIB. */
- BMPCIEXYZTriple sEndpoints; /* This member is ignored unless the iCSType
- * member specifies BMPLT_CALIBRATED_RGB. */
- int32 iGammaRed; /* Toned response curve for red. This member
- * is ignored unless color values are
- * calibrated RGB values and iCSType is set to
- * BMPLT_CALIBRATED_RGB. Specified
- * in 16^16 format. */
- int32 iGammaGreen; /* Toned response curve for green. */
- int32 iGammaBlue; /* Toned response curve for blue. */
- } BMPInfoHeader;
- /*
- * Info header size in bytes:
- */
- const unsigned int BIH_WIN4SIZE = 40; /* for BMPT_WIN4 */
- const unsigned int BIH_WIN5SIZE = 57; /* for BMPT_WIN5 */
- const unsigned int BIH_OS21SIZE = 12; /* for BMPT_OS21 */
- const unsigned int BIH_OS22SIZE = 64; /* for BMPT_OS22 */
- /*
- * We will use plain byte array instead of this structure, but declaration
- * provided for reference
- */
- typedef struct
- {
- char bBlue;
- char bGreen;
- char bRed;
- char bReserved; /* Must be 0 */
- } BMPColorEntry;
- static uint16 compression = (uint16) -1;
- static int jpegcolormode = JPEGCOLORMODE_RGB;
- static int quality = 75; /* JPEG quality */
- static uint16 predictor = 0;
- static void usage(void);
- static int processCompressOptions(char*);
- static void rearrangePixels(char *, uint32, uint32);
- int start_toTiff(char *NameBmp, char *NameTiff, uint16 _compression)
- {
- std::string nametiff;
- nametiff=NameBmp;
- nametiff +=".tif";
- uint32 width, length;
- uint16 nbands = 1; /* number of bands in input image */
- uint16 depth = 8; /* bits per pixel in input image */
- uint32 rowsperstrip = (uint32) -1;
- uint16 photometric = PHOTOMETRIC_MINISBLACK;
- int fd;
- struct stat instat;
- char *outfilename = NULL, *infilename = NULL;
- TIFF *out = NULL;
- BMPFileHeader file_hdr;
- BMPInfoHeader info_hdr;
- int bmp_type;
- uint32 clr_tbl_size, n_clr_elems = 3;
- unsigned char *clr_tbl;
- unsigned short *red_tbl = NULL, *green_tbl = NULL, *blue_tbl = NULL;
- uint32 row, clr;
- int c;
- extern int optind;
- extern char* optarg;
- predictor =2;
- compression = _compression;
- infilename =NameBmp;
- fd = open(infilename, O_RDONLY|O_BINARY, 0);
- if (fd < 0) {
- return -1;
- }
- read(fd, file_hdr.bType, 2);
- if(file_hdr.bType[0] != 'B' || file_hdr.bType[1] != 'M') {
- goto bad;
- }
- /* -------------------------------------------------------------------- */
- /* Read the BMPFileHeader. We need iOffBits value only */
- /* -------------------------------------------------------------------- */
- lseek(fd, 10, SEEK_SET);
- read(fd, &file_hdr.iOffBits, 4);
- fstat(fd, &instat);
- file_hdr.iSize = instat.st_size;
- /* -------------------------------------------------------------------- */
- /* Read the BMPInfoHeader. */
- /* -------------------------------------------------------------------- */
- lseek(fd, BFH_SIZE, SEEK_SET);
- read(fd, &info_hdr.iSize, 4);
- if (info_hdr.iSize == BIH_WIN4SIZE)
- bmp_type = BMPT_WIN4;
- else if (info_hdr.iSize == BIH_OS21SIZE)
- bmp_type = BMPT_OS21;
- else if (info_hdr.iSize == BIH_OS22SIZE || info_hdr.iSize == 16)
- bmp_type = BMPT_OS22;
- else
- bmp_type = BMPT_WIN5;
- if (bmp_type == BMPT_WIN4 || bmp_type == BMPT_WIN5 || bmp_type == BMPT_OS22) {
- read(fd, &info_hdr.iWidth, 4);
- read(fd, &info_hdr.iHeight, 4);
- read(fd, &info_hdr.iPlanes, 2);
- read(fd, &info_hdr.iBitCount, 2);
- read(fd, &info_hdr.iCompression, 4);
- read(fd, &info_hdr.iSizeImage, 4);
- read(fd, &info_hdr.iXPelsPerMeter, 4);
- read(fd, &info_hdr.iYPelsPerMeter, 4);
- read(fd, &info_hdr.iClrUsed, 4);
- read(fd, &info_hdr.iClrImportant, 4);
- n_clr_elems = 4;
- }
- if (info_hdr.iBitCount != 1 && info_hdr.iBitCount != 4 &&
- info_hdr.iBitCount != 8 && info_hdr.iBitCount != 16 &&
- info_hdr.iBitCount != 24 && info_hdr.iBitCount != 32) {
- close(fd);
- return 0;
- }
- width = info_hdr.iWidth;
- length = (info_hdr.iHeight > 0) ? info_hdr.iHeight : -info_hdr.iHeight;
- switch (info_hdr.iBitCount)
- {
- case 1:
- case 4:
- case 8:
- nbands = 1;
- depth = info_hdr.iBitCount;
- photometric = PHOTOMETRIC_PALETTE;
- /* Allocate memory for colour table and read it. */
- if (info_hdr.iClrUsed)
- clr_tbl_size = ((uint32)(1 << depth) < info_hdr.iClrUsed) ?
- (uint32) (1 << depth) : info_hdr.iClrUsed;
- else
- clr_tbl_size = 1 << depth;
- clr_tbl = (unsigned char *)
- _TIFFmalloc(n_clr_elems * clr_tbl_size);
- if (!clr_tbl) {
- goto bad;
- }
- lseek(fd, BFH_SIZE + info_hdr.iSize, SEEK_SET);
- read(fd, clr_tbl, n_clr_elems * clr_tbl_size);
- red_tbl = (unsigned short*)
- _TIFFmalloc(1<<depth * sizeof(unsigned short));
- if (!red_tbl) {
- goto bad1;
- }
- green_tbl = (unsigned short*)
- _TIFFmalloc(1<<depth * sizeof(unsigned short));
- if (!green_tbl) {
- goto bad2;
- }
- blue_tbl = (unsigned short*)
- _TIFFmalloc(1<<depth * sizeof(unsigned short));
- if (!blue_tbl) {
- goto bad3;
- }
- for(clr = 0; clr < clr_tbl_size; clr++) {
- red_tbl[clr] = 257 * clr_tbl[clr*n_clr_elems+2];
- green_tbl[clr] = 257 * clr_tbl[clr*n_clr_elems+1];
- blue_tbl[clr] = 257 * clr_tbl[clr*n_clr_elems];
- }
- _TIFFfree(clr_tbl);
- break;
- case 16:
- case 24:
- nbands = 3;
- depth = info_hdr.iBitCount / nbands;
- photometric = PHOTOMETRIC_RGB;
- break;
- case 32:
- nbands = 3;
- depth = 8;
- photometric = PHOTOMETRIC_RGB;
- break;
- default:
- break;
- }
- /* -------------------------------------------------------------------- */
- /* Create output file. */
- /* -------------------------------------------------------------------- */
- if (outfilename == NULL)
- outfilename = (char *)nametiff.c_str();
- out = TIFFOpen(outfilename, "w");
- if (out == NULL) {
- goto bad3;
- }
-
- TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
- TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);
- TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nbands);
- TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, depth);
- TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
- switch( compression )
- {
- case COMPRESSION_CCITTFAX3:
- {
- TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
- TIFFSetField(out, TIFFTAG_SUBFILETYPE, 0);
- TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
- TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip));
- TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
- TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, GROUP3OPT_2DENCODING|GROUP3OPT_FILLBITS);
- TIFFSetField(out, TIFFTAG_FAXMODE, FAXMODE_CLASSIC/* generate "classic" g3 format */);
- TIFFSetField( out, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
- TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
- TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0);
- TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.0);
- }break;
- case COMPRESSION_CCITTFAX4:
- {
- TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
- TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
- TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip));
- TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
- TIFFSetField(out, TIFFTAG_SUBFILETYPE, 0);
- TIFFSetField(out, TIFFTAG_GROUP4OPTIONS, 0);
- TIFFSetField(out, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
- TIFFSetField(out, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
- TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
- TIFFSetField(out, TIFFTAG_XRESOLUTION, 1.0);
- TIFFSetField(out, TIFFTAG_YRESOLUTION, 1.0);
- break;
- }
- default:
- {
- TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
- TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
- TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, rowsperstrip));
-
- if (red_tbl && green_tbl && blue_tbl)
- TIFFSetField(out, TIFFTAG_COLORMAP, red_tbl, green_tbl, blue_tbl);
-
- if (compression == (uint16) -1)
- compression = COMPRESSION_PACKBITS;
- TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
- switch (compression) {
- case COMPRESSION_JPEG:
- if (photometric == PHOTOMETRIC_RGB
- && jpegcolormode == JPEGCOLORMODE_RGB)
- photometric = PHOTOMETRIC_YCBCR;
- TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
- TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
- break;
- case COMPRESSION_LZW:
- case COMPRESSION_DEFLATE:
- if (predictor != 0)
- TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
- break;
- }
- }
- }
- /* -------------------------------------------------------------------- */
- /* Read uncompressed image data. */
- /* -------------------------------------------------------------------- */
- if (info_hdr.iCompression == BMPC_RGB) {
- uint32 offset, size;
- char *scanbuf;
- /* XXX: Avoid integer overflow. We can calculate size in one
- * step using
- *
- * size = ((width * info_hdr.iBitCount + 31) & ~31) / 8
- *
- * formulae, but we should check for overflow conditions
- * during calculation.
- */
- size = width * info_hdr.iBitCount + 31;
- if (!width || !info_hdr.iBitCount
- || (size - 31) / info_hdr.iBitCount != width ) {
- goto bad3;
- }
- size = (size & ~31) / 8;
- scanbuf = (char *) _TIFFmalloc(size);
- if (!scanbuf) {
- goto bad3;
- }
- for (row = 0; row < length; row++) {
- if (info_hdr.iHeight > 0)
- offset = file_hdr.iOffBits + (length - row - 1) * size;
- else
- offset = file_hdr.iOffBits + row * size;
- if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
- break;
- }
- if (read(fd, scanbuf, size) < 0) {
- break;
- }
- rearrangePixels(scanbuf, width, info_hdr.iBitCount);
- if (TIFFWriteScanline(out, scanbuf, row, 0) < 0) {
- break;
- }
- }
- _TIFFfree(scanbuf);
- /* -------------------------------------------------------------------- */
- /* Read compressed image data. */
- /* -------------------------------------------------------------------- */
- } else if ( info_hdr.iCompression == BMPC_RLE8
- || info_hdr.iCompression == BMPC_RLE4 ) {
- uint32 i, j, k, runlength;
- uint32 compr_size, uncompr_size;
- unsigned char *comprbuf;
- unsigned char *uncomprbuf;
- compr_size = file_hdr.iSize - file_hdr.iOffBits;
- uncompr_size = width * length;
- comprbuf = (unsigned char *) _TIFFmalloc( compr_size );
- if (!comprbuf) {
- goto bad3;
- }
- uncomprbuf = (unsigned char *) _TIFFmalloc( uncompr_size );
- if (!uncomprbuf) {
- goto bad3;
- }
- lseek(fd, file_hdr.iOffBits, SEEK_SET);
- read(fd, comprbuf, compr_size);
- i = 0;
- j = 0;
- if (info_hdr.iBitCount == 8) { /* RLE8 */
- while( j < uncompr_size && i < compr_size ) {
- if ( comprbuf[i] ) {
- runlength = comprbuf[i++];
- while( runlength > 0
- && j < uncompr_size
- && i < compr_size ) {
- uncomprbuf[j++] = comprbuf[i];
- runlength--;
- }
- i++;
- } else {
- i++;
- if ( comprbuf[i] == 0 ) /* Next scanline */
- i++;
- else if ( comprbuf[i] == 1 ) /* End of image */
- break;
- else if ( comprbuf[i] == 2 ) { /* Move to... */
- i++;
- if ( i < compr_size - 1 ) {
- j += comprbuf[i] + comprbuf[i+1] * width;
- i += 2;
- }
- else
- break;
- } else { /* Absolute mode */
- runlength = comprbuf[i++];
- for ( k = 0; k < runlength && j < uncompr_size && i < compr_size; k++ )
- uncomprbuf[j++] = comprbuf[i++];
- if ( k & 0x01 )
- i++;
- }
- }
- }
- }
- else { /* RLE4 */
- while( j < uncompr_size && i < compr_size ) {
- if ( comprbuf[i] ) {
- runlength = comprbuf[i++];
- while( runlength > 0 && j < uncompr_size && i < compr_size ) {
- if ( runlength & 0x01 )
- uncomprbuf[j++] = (comprbuf[i] & 0xF0) >> 4;
- else
- uncomprbuf[j++] = comprbuf[i] & 0x0F;
- runlength--;
- }
- i++;
- } else {
- i++;
- if ( comprbuf[i] == 0 ) /* Next scanline */
- i++;
- else if ( comprbuf[i] == 1 ) /* End of image */
- break;
- else if ( comprbuf[i] == 2 ) { /* Move to... */
- i++;
- if ( i < compr_size - 1 ) {
- j += comprbuf[i] + comprbuf[i+1] * width;
- i += 2;
- }
- else
- break;
- } else { /* Absolute mode */
- runlength = comprbuf[i++];
- for ( k = 0; k < runlength && j < uncompr_size && i < compr_size; k++) {
- if ( k & 0x01 )
- uncomprbuf[j++] = comprbuf[i++] & 0x0F;
- else
- uncomprbuf[j++] = (comprbuf[i] & 0xF0) >> 4;
- }
- if ( k & 0x01 )
- i++;
- }
- }
- }
- }
- _TIFFfree(comprbuf);
- for (row = 0; row < length; row++) {
- if (TIFFWriteScanline(out, uncomprbuf + (length - row - 1) * width, row, 0) < 0)
- {}
- }
- _TIFFfree(uncomprbuf);
- }
- bad3:
- if (blue_tbl)
- _TIFFfree(blue_tbl);
- bad2:
- if (green_tbl)
- _TIFFfree(green_tbl);
- bad1:
- if (red_tbl)
- _TIFFfree(red_tbl);
- bad:
- close(fd);
- if (out)
- TIFFClose(out);
- return 0;
- }
- /*
- * Image data in BMP file stored in BGR (or ABGR) format. We should rearrange
- * pixels to RGB (RGBA) format.
- */
- static void
- rearrangePixels(char *buf, uint32 width, uint32 bit_count)
- {
- char tmp;
- uint32 i;
- switch(bit_count) {
- case 16: /* FIXME: need a sample file */
- break;
- case 24:
- for (i = 0; i < width; i++, buf += 3) {
- tmp = *buf;
- *buf = *(buf + 2);
- *(buf + 2) = tmp;
- }
- break;
- case 32:
- {
- char *buf1 = buf;
- for (i = 0; i < width; i++, buf += 4) {
- tmp = *buf;
- *buf1++ = *(buf + 2);
- *buf1++ = *(buf + 1);
- *buf1++ = tmp;
- }
- }
- break;
- default:
- break;
- }
- }