📄 imgtiff.cpp
字号:
read_eol(&bits, codeword); //find end of line for (yindex = ysize - 1; yindex >= 0; yindex--) { imageline.init (); colour = TRUE; for (xindex = 0; xindex < xsize;) { if (colour) { lengths = long_white_lengths; codes = long_white_codes; } else { lengths = long_black_lengths; codes = long_black_codes; } for (biglength = 0; biglength < LONG_CODE_SIZE && (codeword & bits.masks (*lengths)) != *codes; codes++, lengths++, biglength++); if (biglength < LONG_CODE_SIZE) { codeword = bits.read_code (*lengths); biglength++; biglength *= SHORT_CODE_SIZE; } else biglength = 0; if (colour) { lengths = short_white_lengths; codes = short_white_codes; } else { lengths = short_black_lengths; codes = short_black_codes; } for (length = 0; length < SHORT_CODE_SIZE && (codeword & bits.masks (*lengths)) != *codes; codes++, lengths++, length++); if (length < SHORT_CODE_SIZE) { codeword = bits.read_code (*lengths); for (length += biglength; length > 0; length--, xindex++) imageline.pixels[xindex] = colour; colour = !colour; } else break; } if (xindex < xsize) { tprintf ("%d pixels short on line %d", xsize - xindex, yindex); tprintf (", unknown code=%x\n", codeword); } xindex = read_eol (&bits, codeword); if (xindex > 0) tprintf ("Discarding %d bits on line %d\n", xindex, yindex); image.put_line (0, yindex, xsize, &imageline, 0); } return 0;}/********************************************************************** * read_eol * * Take bits out of the stream until and end-of-line code is hit. **********************************************************************/INT32 read_eol( //read end of line R_BITSTREAM *bits, //bitstream to read UINT16 &code //current code ) { BOOL8 anyones; //any 1 bits skipped INT32 bitcount; //total bits skipped anyones = FALSE; bitcount = 0; while ((code & EOL_MASK) != EOL_CODE) { if (code & 1) anyones = TRUE; //discarded one bit bitcount++; //total discarded bits code = bits->read_code (1); //take single bits } //extract EOL code code = bits->read_code (EOL_LENGTH); if (!anyones) bitcount = 0; //ignore filler bits return bitcount;}/********************************************************************** * write_moto_tif * * Write a whole tif format image and close the file. **********************************************************************/INT8 write_moto_tif( //write whole image int fd, //file to write on UINT8 *pixels, //image pixels INT32 xsize, //size of image INT32 ysize, INT8 bpp, //bits per pixel INT8 photo, INT32 res //resolution ) { return write_tif_image (fd, pixels, xsize, ysize, bpp, res, MOTO, photo); //use moto format}/********************************************************************** * write_intel_tif * * Write a whole tif format image and close the file. **********************************************************************/INT8 write_intel_tif( //write whole image int fd, //file to write on UINT8 *pixels, //image pixels INT32 xsize, //size of image INT32 ysize, INT8 bpp, //bits per pixel INT8 photo, INT32 res //resolution ) { return write_tif_image (fd, pixels, xsize, ysize, bpp, res, INTEL, photo); //use intel format}/********************************************************************** * write_inverse_tif * * Write a whole tif format image and close the file. **********************************************************************/INT8 write_inverse_tif( //write whole image int fd, //file to write on UINT8 *pixels, //image pixels INT32 xsize, //size of image INT32 ysize, INT8 bpp, //bits per pixel INT8 photo, INT32 res //resolution ) { return write_tif_image (fd, pixels, xsize, ysize, bpp, res, INTEL, 1 - photo); //use intel format}/********************************************************************** * write_tif_image * * Write a whole tif format image and close the file. **********************************************************************/INT8 write_tif_image( //write whole image int fd, //file to write on UINT8 *pixels, //image pixels INT32 xsize, //size of image INT32 ysize, INT8 bpp, //bits per pixel INT32 res, //resolution INT16 type, //format type INT16 photo //metric interp ) { INT32 size; //line/image size INT16 entries; //no of tiff entries INT32 start; //start of tag table INT32 zero = 0; MYRATIONAL resolution; //resolution TIFFENTRY entry; //current entry static TIFFENTRY tags[ENTRIES] = { {0xfe, 4, 1, 0}, {0x100, 3, 1, 0}, {0x101, 3, 1, 0}, {0x102, 3, 1, 0}, {0x103, 3, 1, 1}, {0x106, 3, 1, 1}, { /*line art */ 0x107, 3, 1, 1 }, {0x10a, 3, 1, 1}, { 0x111, 4, 1, START + ENTRIES * sizeof (TIFFENTRY) + sizeof (INT32) + sizeof (short) + sizeof (MYRATIONAL) * 2 } , {0x112, 3, 1, 1} , {0x115, 3, 1, 1} , {0x116, 4, 1, 0} , {0x117, 4, 1, 0} , {0x118, 3, 1, 0} , {0x119, 3, 1, 1} , { 0x11a, 5, 1, START + ENTRIES * sizeof (TIFFENTRY) + sizeof (INT32) + sizeof (short) }, { 0x11b, 5, 1, START + ENTRIES * sizeof (TIFFENTRY) + sizeof (INT32) + sizeof (short) + sizeof (MYRATIONAL) } , {0x11c, 3, 1, 1} , {0x128, 3, 1, 2} }; resolution.top = res; resolution.bottom = 1; if (write (fd, (char *) &type, sizeof type) != sizeof type || type != INTEL && type != MOTO) { WRITEFAILED.error ("write_tif_image", LOG, "Filetype"); return -1; } start = START; entries = 0x002a; if (type != __NATIVE__) entries = reverse16 (entries); if (write (fd, (char *) &entries, sizeof entries) != sizeof entries) { WRITEFAILED.error ("write_tif_image", LOG, "Version"); return -1; } if (type != __NATIVE__) start = reverse32 (start); if (write (fd, (char *) &start, sizeof start) != sizeof start) { WRITEFAILED.error ("write_tif_image", LOG, "Start"); return -1; } lseek (fd, (long) START, 0); entries = ENTRIES; if (type != __NATIVE__) entries = reverse16 (entries); if (write (fd, (char *) &entries, sizeof entries) != sizeof entries) { WRITEFAILED.error ("write_tif_image", LOG, "Entries"); return -1; } //line length size = COMPUTE_IMAGE_XDIM (xsize, bpp); size *= ysize; //total image size // if (photo==0) // { // tags[0].tag=0xfe; // tags[0].type=4; // tags[0].value=0; // } // else // { // tags[0].tag=0xff; // tags[0].type=3; // tags[0].value=1; // } tags[1].value = xsize; tags[2].value = ysize; if (bpp == 24) { tags[3].value = 8; tags[10].value = 3; tags[5].value = 2; } else { tags[3].value = bpp; tags[5].value = photo; } tags[11].value = ysize; tags[14].value = (1 << bpp) - 1; tags[12].value = size; for (entries = 0; entries < ENTRIES; entries++) { entry = tags[entries]; //get an entry /* NB Convert entry.value BEFORE converting entry.type!!! */ if (entry.type != 3) { //Full 32bit value if (type != __NATIVE__) entry.value = reverse32 (entry.value); } else { /* A 16bit value in 4 bytes - handle with care. SEE NOTE at start of file */ entry.value &= 0x0000ffff; //Ensure top 2 MSBytes clear if (__NATIVE__ == MOTO) { if (type == MOTO) //MOTO file on MOTO Machine entry.value = entry.value << 16; else //INTEL file on MOTO Machine entry.value = reverse32 (entry.value); } else { //INTEL Machine if (type == MOTO) //MOTO file on INTEL Machine entry.value = reverse16 ((UINT16) entry.value); //INTEL file on INTEL Machine NO ACTION NEEDED } } if (type != __NATIVE__) { entry.tag = reverse16 (entry.tag); entry.type = reverse16 (entry.type); entry.length = reverse32 (entry.length); } if (write (fd, (char *) &entry, sizeof (TIFFENTRY)) != sizeof (TIFFENTRY)) { WRITEFAILED.error ("write_tif_image", LOG, "Tag Table"); return -1; } } if (write (fd, (char *) &zero, sizeof zero) != sizeof zero) { WRITEFAILED.error ("write_tif_image", LOG, "Tag table Terminator"); return -1; } if (type != __NATIVE__) { resolution.top = reverse32 (resolution.top); resolution.bottom = reverse32 (resolution.bottom); } if (write (fd, (char *) &resolution, sizeof resolution) != sizeof resolution || write (fd, (char *) &resolution, sizeof resolution) != sizeof resolution) { WRITEFAILED.error ("write_tif_image", LOG, "Resolution"); return -1; } if (write (fd, (char *) pixels, (size_t) size) != size) { WRITEFAILED.error ("write_tif_image", LOG, "Image"); return -1; } close(fd); return 0;}/********************************************************************** * reverse32 * * Byte swap the 32 bit number between Motorola & Intel format. **********************************************************************///INT32 reverse32( //reverse 32 bit int//UINT32 value //value to reverse//)//{// return (value>>24) | (value>>8) & 0xff00// | (value<<8) & 0xff0000 | (value<<24);//}/********************************************************************** * reverse16 * * Byte swap the 16 bit number between Motorola & Intel format. **********************************************************************///INT16 reverse16( //reverse 16 bit int//UINT16 value //value to reverse//)//{// return (value>>8) | (value<<8);//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -