mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2025-01-18 14:41:36 +08:00
Deleted lots of dead code, including PBLOB
git-svn-id: https://tesseract-ocr.googlecode.com/svn/trunk@561 d0cd1f9f-072b-0410-8dd7-cf729c803f20
This commit is contained in:
parent
7cd3c74419
commit
06dda0009e
@ -1,157 +0,0 @@
|
||||
/**********************************************************************
|
||||
* File: bitstrm.c (Formerly bits.c)
|
||||
* Description: Bitstream read/write class member functions.
|
||||
* Author: Ray Smith
|
||||
* Created: Tue Feb 19 10:59:44 GMT 1991
|
||||
*
|
||||
* (C) Copyright 1991, Hewlett-Packard Ltd.
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#include "mfcpch.h" //precompiled headers
|
||||
#ifdef __MSW32__
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "fileerr.h"
|
||||
#include "bitstrm.h"
|
||||
|
||||
const uinT16
|
||||
R_BITSTREAM::bitmasks[17] = {
|
||||
0, 1, 3, 7, 15, 31, 63, 127, 255,
|
||||
511, 1023, 2047, 4095, 8191, 16383, 32767, 65535
|
||||
};
|
||||
|
||||
/**********************************************************************
|
||||
* R_BITSTREAM::open
|
||||
*
|
||||
* Establish a bitstream for reading.
|
||||
**********************************************************************/
|
||||
|
||||
uinT16 R_BITSTREAM::open( //open for read
|
||||
int fd //file to read
|
||||
) {
|
||||
bitfd = fd;
|
||||
bufsize = read (fd, (char *) bitbuf, BITBUFSIZE * sizeof (uinT8));
|
||||
//fill buffer
|
||||
if (bufsize < 0) {
|
||||
READFAILED.error ("R_BITSTREAM::open", TESSLOG, NULL);
|
||||
return 0;
|
||||
}
|
||||
bitword = bitbuf[0] | (bitbuf[1] << 8);
|
||||
bitindex = 2;
|
||||
bitbit = 16;
|
||||
return (uinT16) bitword;
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* R_BITSTREAM::read_code
|
||||
*
|
||||
* Remove a code from the bitstream.
|
||||
**********************************************************************/
|
||||
|
||||
uinT16 R_BITSTREAM::read_code( //take code out
|
||||
uinT8 length //length of code
|
||||
) {
|
||||
bitbit -= length; //no of bits left
|
||||
bitword >>= length; //remove bits
|
||||
while (bitbit < 16) {
|
||||
//get next byte
|
||||
bitword |= bitbuf[bitindex++] << bitbit;
|
||||
bitbit += 8;
|
||||
if (bitindex >= bufsize) {
|
||||
bufsize =
|
||||
read (bitfd, (char *) bitbuf, BITBUFSIZE * sizeof (uinT8));
|
||||
if (bufsize < 0) {
|
||||
READFAILED.error ("R_BITSTREAM::read_code", TESSLOG, NULL);
|
||||
return 0;
|
||||
}
|
||||
bitindex = 0; //newly filled buffer
|
||||
}
|
||||
}
|
||||
return (uinT16) bitword;
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* R_BITSTREAM::masks
|
||||
*
|
||||
* Read a code from the static member.
|
||||
**********************************************************************/
|
||||
|
||||
uinT16 R_BITSTREAM::masks( //take code out
|
||||
inT32 index //length of code
|
||||
) {
|
||||
return bitmasks[index];
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* W_BITSTREAM::open
|
||||
*
|
||||
* Establish a bitstream for writing.
|
||||
**********************************************************************/
|
||||
|
||||
void W_BITSTREAM::open( //open for write
|
||||
int fd //file to write
|
||||
) {
|
||||
bitfd = fd;
|
||||
bitindex = 0;
|
||||
bitword = 0;
|
||||
bitbit = 0;
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* W_BITSTREAM::write_code
|
||||
*
|
||||
* Add a code to the bitstream.
|
||||
**********************************************************************/
|
||||
|
||||
inT8 W_BITSTREAM::write_code( //take code out
|
||||
uinT16 code, //code to add
|
||||
uinT8 length //length of code
|
||||
) {
|
||||
if (length == 0) {
|
||||
//flushing
|
||||
if (bitbit > 0)
|
||||
//get last byte
|
||||
bitbuf[bitindex++] = (uinT8) bitword;
|
||||
if ((bitindex > 0) &&
|
||||
(write (bitfd, (char *) bitbuf, bitindex * sizeof (uinT8)) !=
|
||||
(inT32) (bitindex * sizeof (uinT8)))) {
|
||||
WRITEFAILED.error ("W_BITSTREAM::write_code", TESSLOG, "Flushing");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
bitword |= code << bitbit; //add new code
|
||||
bitbit += length;
|
||||
while (bitbit >= 8) {
|
||||
//get next byte
|
||||
bitbuf[bitindex++] = (uinT8) bitword;
|
||||
bitbit -= 8;
|
||||
bitword >>= 8;
|
||||
if (bitindex >= BITBUFSIZE) {
|
||||
if (write (bitfd, (char *) bitbuf, bitindex * sizeof (uinT8))
|
||||
!= (inT32) (bitindex * sizeof (uinT8))) {
|
||||
WRITEFAILED.error ("W_BITSTREAM::write_code", TESSLOG, NULL);
|
||||
return -1;
|
||||
}
|
||||
bitindex = 0; //newly filled buffer
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0; //success
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
/**********************************************************************
|
||||
* File: bitstrm.h (Formerly bits.h)
|
||||
* Description: R_BITSTREAM and W_BITSTREAM class definitions.
|
||||
* Author: Ray Smith
|
||||
* Created: Tue Feb 19 10:44:22 GMT 1991
|
||||
*
|
||||
* (C) Copyright 1991, Hewlett-Packard Ltd.
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#ifndef BITSTRM_H
|
||||
#define BITSTRM_H
|
||||
|
||||
#include "host.h"
|
||||
|
||||
#define BITBUFSIZE 8192 //bitstream buffer
|
||||
|
||||
class DLLSYM R_BITSTREAM
|
||||
{
|
||||
private:
|
||||
int bitfd; //file descriptor
|
||||
inT32 bitindex; //current byte
|
||||
uinT32 bitword; //current word
|
||||
inT32 bitbit; //current bit
|
||||
inT32 bufsize; //size of buffer
|
||||
uinT8 bitbuf[BITBUFSIZE]; //bitstream buffer
|
||||
//for reading codes
|
||||
static const uinT16 bitmasks[17];
|
||||
|
||||
public:
|
||||
|
||||
R_BITSTREAM() {
|
||||
}; //Null constructor
|
||||
|
||||
uinT16 open( //open to read
|
||||
int fd); //file to read
|
||||
|
||||
uinT16 read_code( //read a code
|
||||
uinT8 length); //bits to lose
|
||||
uinT16 masks( //read a code
|
||||
inT32 index); //bits to lose
|
||||
};
|
||||
|
||||
class DLLSYM W_BITSTREAM
|
||||
{
|
||||
private:
|
||||
int bitfd; //file descriptor
|
||||
inT32 bitindex; //current byte
|
||||
uinT32 bitword; //current word
|
||||
inT32 bitbit; //current bit
|
||||
uinT8 bitbuf[BITBUFSIZE]; //bitstream buffer
|
||||
|
||||
public:
|
||||
W_BITSTREAM() {
|
||||
}; //Null constructor
|
||||
|
||||
void open( //open to write
|
||||
int fd); //file to write
|
||||
|
||||
inT8 write_code( //write a code
|
||||
uinT16 code, //code to write
|
||||
uinT8 length); //bits to lose
|
||||
};
|
||||
#endif
|
18
image/img.h
18
image/img.h
@ -60,24 +60,6 @@ class DLLSYM IMAGE
|
||||
IMAGE & operator= ( //assignment
|
||||
IMAGE & source);
|
||||
|
||||
/**
|
||||
* get file header
|
||||
* @param name name of image
|
||||
*/
|
||||
inT8 read_header(const char *name);
|
||||
|
||||
/**
|
||||
* get rest of image
|
||||
* @param buflines size of buffer
|
||||
*/
|
||||
inT8 read(inT32 buflines);
|
||||
|
||||
/**
|
||||
* write image
|
||||
* @param name name to write
|
||||
*/
|
||||
inT8 write(const char *name);
|
||||
|
||||
/**
|
||||
* create blank image
|
||||
* @param x x size required
|
||||
|
227
image/imgbmp.cpp
227
image/imgbmp.cpp
@ -1,227 +0,0 @@
|
||||
/**********************************************************************
|
||||
* File: imgbmp.c (Formerly lz.c)
|
||||
* Description: bmp image reader/writer.
|
||||
* Author: Ray Smith
|
||||
* Created: Tue Jan 06 15:31:25 GMT 1998
|
||||
*
|
||||
* (C) Copyright 1998, Ray Smith.
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#include "mfcpch.h" //precompiled headers
|
||||
#ifdef __MSW32__
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include "img.h"
|
||||
#include "imgbmp.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4244) // Conversion warnings
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{ // bmfh
|
||||
char bfType1; //'B'
|
||||
char bfType2; //'M'
|
||||
} BMPHEADER0;
|
||||
typedef struct
|
||||
{ // bmfh
|
||||
uinT32 bfSize; //filesize
|
||||
uinT16 bfReserved1; //zero
|
||||
uinT16 bfReserved2; //zero
|
||||
uinT32 bfOffBits; //offset to bitmap
|
||||
} BMPHEADER;
|
||||
|
||||
typedef struct
|
||||
{ // bmih
|
||||
uinT32 biSize; //size of struct
|
||||
inT32 biWidth; //image width
|
||||
inT32 biHeight; //image height
|
||||
uinT16 biPlanes; //1
|
||||
uinT16 biBitCount; //bpp
|
||||
uinT32 biCompression; //0 for uncompressed
|
||||
uinT32 biSizeImage; //image size
|
||||
inT32 biXPelsPerMeter; //res in pp metre
|
||||
inT32 biYPelsPerMeter;
|
||||
uinT32 biClrUsed; //0 or actual size of colour table
|
||||
uinT32 biClrImportant; //usually 0
|
||||
} BMPHEADER2;
|
||||
|
||||
typedef struct
|
||||
{ // rgbq
|
||||
uinT8 rgbBlue;
|
||||
uinT8 rgbGreen;
|
||||
uinT8 rgbRed;
|
||||
uinT8 rgbReserved; //0
|
||||
} WIN32_RGBQUAD;
|
||||
|
||||
/**
|
||||
* @name open_bmp_image
|
||||
*
|
||||
* Read the header of a bmp format image and prepare to read the rest.
|
||||
*/
|
||||
|
||||
inT8 open_bmp_image( //read header
|
||||
int fd, //file to read
|
||||
inT32 *xsize, //size of image
|
||||
inT32 *ysize,
|
||||
inT8 *bpp, //bits per pixel
|
||||
inT8 *photo,
|
||||
inT32 *res //resolution
|
||||
) {
|
||||
uinT32 nread; //current bits
|
||||
BMPHEADER0 head0; //first part of header
|
||||
BMPHEADER head1; //first part of header
|
||||
BMPHEADER2 head2; //first part of header
|
||||
|
||||
*photo = 1;
|
||||
nread = read (fd, &head0, sizeof (head0));
|
||||
if (nread != sizeof (head0))
|
||||
return -1;
|
||||
nread = read (fd, &head1, sizeof (head1));
|
||||
if (nread != sizeof (head1))
|
||||
return -1;
|
||||
nread = read (fd, &head2, sizeof (head2));
|
||||
if (nread != sizeof (head2))
|
||||
return -1;
|
||||
|
||||
if (head0.bfType1 != 'B')
|
||||
return -1;
|
||||
if (head0.bfType2 != 'M')
|
||||
return -1;
|
||||
lseek (fd, head1.bfOffBits, SEEK_SET);
|
||||
*bpp = head2.biBitCount;
|
||||
*xsize = head2.biWidth;
|
||||
*ysize = head2.biHeight;
|
||||
*res = 300; //make up resolution
|
||||
return -2; //success
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @name read_bmp_image
|
||||
*
|
||||
* Read a whole lz format image and close the file.
|
||||
*/
|
||||
|
||||
inT8 read_bmp_image( //read header
|
||||
int fd, //file to read
|
||||
uinT8 *pixels, //pixels of image
|
||||
inT32 xsize, //size of image
|
||||
inT32 ysize,
|
||||
inT8 bpp, //bits per pixel
|
||||
inT32 //bytes per line
|
||||
) {
|
||||
uinT32 bpl; //bytes per line
|
||||
uinT32 wpl; //words per line
|
||||
uinT32 nread; //current bits
|
||||
inT32 index; //to cols
|
||||
|
||||
bpl = (xsize * bpp + 7) / 8; //bytes per line
|
||||
wpl = (bpl + 3) / 4;
|
||||
wpl *= 4;
|
||||
for (index = 0; index < ysize; index++) {
|
||||
nread = read (fd, pixels + bpl * (ysize - 1 - index), bpl);
|
||||
if (nread != bpl)
|
||||
return -1;
|
||||
if (wpl != bpl)
|
||||
lseek (fd, wpl - bpl, SEEK_CUR);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @name write_bmp_image
|
||||
*
|
||||
* Write a whole lz format image and close the file.
|
||||
*/
|
||||
|
||||
inT8 write_bmp_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
|
||||
inT8,
|
||||
inT32 res //resolution
|
||||
) {
|
||||
uinT32 bpl; //bytes per line
|
||||
uinT32 wpl; //words per line
|
||||
uinT32 nread; //current bits
|
||||
inT32 cols; //entries in table
|
||||
inT32 index; //to cols
|
||||
BMPHEADER0 head0; //first part of header
|
||||
BMPHEADER head1; //first part of header
|
||||
BMPHEADER2 head2; //first part of header
|
||||
WIN32_RGBQUAD coltab[256]; //colour table
|
||||
|
||||
if (bpp == 24)
|
||||
cols = 0;
|
||||
else
|
||||
cols = 1 << bpp; //size of colour table
|
||||
bpl = (xsize * bpp + 7) / 8; //bytes per line
|
||||
wpl = (bpl + 3) / 4;
|
||||
|
||||
head2.biSize = sizeof (head2); //size of struct
|
||||
head2.biWidth = xsize; //image width
|
||||
head2.biHeight = ysize; //image height
|
||||
head2.biPlanes = 1; //1
|
||||
head2.biBitCount = bpp; //bpp
|
||||
head2.biCompression = 0; //0 for uncompressed
|
||||
//image size
|
||||
head2.biSizeImage = wpl * 4 * ysize;
|
||||
//res in pp metre
|
||||
head2.biXPelsPerMeter = (uinT32) (res * 39.37);
|
||||
head2.biYPelsPerMeter = (uinT32) (res * 39.37);
|
||||
head2.biClrUsed = cols; //0 or actual size of colour table
|
||||
head2.biClrImportant = 0; //usually 0
|
||||
|
||||
head0.bfType1 = 'B';
|
||||
head0.bfType2 = 'M';
|
||||
head1.bfReserved1 = 0; //zero
|
||||
head1.bfReserved2 = 0; //zero
|
||||
//offset to bitmap
|
||||
head1.bfOffBits = sizeof (head0) + sizeof (head1) + sizeof (head2) + sizeof (WIN32_RGBQUAD) * cols;
|
||||
//filesize
|
||||
head1.bfSize = head1.bfOffBits + head2.biSizeImage;
|
||||
|
||||
for (index = 0; index < cols; index++) {
|
||||
coltab[index].rgbBlue = index * 255 / (cols - 1);
|
||||
coltab[index].rgbGreen = coltab[index].rgbBlue;
|
||||
coltab[index].rgbRed = coltab[index].rgbBlue;
|
||||
coltab[index].rgbReserved = 0;
|
||||
}
|
||||
|
||||
nread = write (fd, &head0, sizeof (head0));
|
||||
if (nread != sizeof (head0))
|
||||
return -1;
|
||||
nread = write (fd, &head1, sizeof (head1));
|
||||
if (nread != sizeof (head1))
|
||||
return -1;
|
||||
nread = write (fd, &head2, sizeof (head2));
|
||||
if (nread != sizeof (head2))
|
||||
return -1;
|
||||
nread = write (fd, coltab, cols * sizeof (WIN32_RGBQUAD));
|
||||
if (nread != cols * sizeof (WIN32_RGBQUAD))
|
||||
return -1;
|
||||
for (index = 0; index < ysize; index++) {
|
||||
nread = write (fd, pixels + bpl * (ysize - 1 - index), wpl * 4);
|
||||
if (nread != wpl * 4)
|
||||
return -1;
|
||||
}
|
||||
close(fd); //done it
|
||||
return 0;
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
/**********************************************************************
|
||||
* File: imgbmp.c (Formerly lz.c)
|
||||
* Description: bmp image reader/writer.
|
||||
* Author: Ray Smith
|
||||
* Created: Tue Jan 06 20:15:52 GMT 1998
|
||||
*
|
||||
* (C) Copyright 1998, Ray Smith.
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#ifndef IMGBMP_H
|
||||
#define IMGBMP_H
|
||||
|
||||
#include "host.h"
|
||||
|
||||
inT8 open_bmp_image( //read header
|
||||
int fd, //file to read
|
||||
inT32 *xsize, //size of image
|
||||
inT32 *ysize,
|
||||
inT8 *bpp, //bits per pixel
|
||||
inT8 *photo,
|
||||
inT32 *res //resolution
|
||||
);
|
||||
inT8 read_bmp_image( //read header
|
||||
int fd, //file to read
|
||||
uinT8 *pixels, //pixels of image
|
||||
inT32 xsize, //size of image
|
||||
inT32 ysize,
|
||||
inT8 bpp, //bits per pixel
|
||||
inT32 //bytes per line
|
||||
);
|
||||
inT8 write_bmp_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
|
||||
inT8,
|
||||
inT32 //resolution
|
||||
);
|
||||
#endif
|
339
image/imgio.cpp
339
image/imgio.cpp
@ -1,339 +0,0 @@
|
||||
/**********************************************************************
|
||||
* File: imgio.c (Formerly imageio.c)
|
||||
* Description: Controls image input/output and selection of format.
|
||||
* Author: Ray Smith
|
||||
* Created: Mon Jun 11 11:47:26 BST 1990
|
||||
*
|
||||
* (C) Copyright 1990, Hewlett-Packard Ltd.
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#include "mfcpch.h" //precompiled headers
|
||||
#ifdef __MSW32__
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include "scanutils.h"
|
||||
#include "stderr.h"
|
||||
#include "fileerr.h"
|
||||
#include "imgerrs.h"
|
||||
#include "memry.h"
|
||||
#include "imgs.h"
|
||||
#include "imgbmp.h"
|
||||
#include "imgtiff.h"
|
||||
#include "imgio.h"
|
||||
|
||||
#define DEFAULTIMAGETYPE "tif" //default to im files
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *string; //extension
|
||||
IMAGE_OPENER opener; //opening function
|
||||
IMAGE_READER reader; //reading function
|
||||
IMAGE_WRITER writer; //writing function
|
||||
} IMAGETYPE; //image type record
|
||||
|
||||
static IMAGETYPE imagetypes[] = { {
|
||||
"TIF",
|
||||
open_tif_image,
|
||||
read_tif_image,
|
||||
write_moto_tif
|
||||
},
|
||||
{
|
||||
"itf",
|
||||
open_tif_image,
|
||||
read_tif_image,
|
||||
write_inverse_tif
|
||||
},
|
||||
{
|
||||
"tif",
|
||||
open_tif_image,
|
||||
read_tif_image,
|
||||
write_intel_tif
|
||||
},
|
||||
{
|
||||
"TIFF",
|
||||
open_tif_image,
|
||||
read_tif_image,
|
||||
write_moto_tif
|
||||
},
|
||||
{
|
||||
"tiff",
|
||||
open_tif_image,
|
||||
read_tif_image,
|
||||
write_intel_tif
|
||||
},
|
||||
{
|
||||
"bmp",
|
||||
open_bmp_image,
|
||||
read_bmp_image,
|
||||
write_bmp_image
|
||||
},
|
||||
{
|
||||
"BMP",
|
||||
open_bmp_image,
|
||||
read_bmp_image,
|
||||
write_bmp_image
|
||||
},
|
||||
}; //image readers/writers
|
||||
|
||||
#define MAXIMAGETYPES (sizeof(imagetypes)/sizeof(IMAGETYPE))
|
||||
|
||||
/**********************************************************************
|
||||
* name_to_image_type
|
||||
*
|
||||
* Convert a file name to an image type, picking defaults if it is
|
||||
* has no extension, and complaining if the extension is not supported.
|
||||
**********************************************************************/
|
||||
|
||||
static inT8 name_to_image_type( //get image type
|
||||
const char *name //name of image
|
||||
) {
|
||||
const char *nametype; //type part of name
|
||||
inT8 type; //imagetypes index
|
||||
|
||||
nametype = strrchr (name, '.');//find extension
|
||||
if (nametype != NULL)
|
||||
nametype++; //ptr to extension
|
||||
else
|
||||
nametype = DEFAULTIMAGETYPE; //had none
|
||||
|
||||
//find type of image
|
||||
for (type = 0; type < MAXIMAGETYPES && strcmp (imagetypes[type].string, nametype); type++);
|
||||
if (type >= MAXIMAGETYPES) {
|
||||
//unrecognized type
|
||||
BADIMAGETYPE.error ("name_to_image_type", TESSLOG, name);
|
||||
return -1;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* read_header
|
||||
*
|
||||
* Read the header of an image, typed according to the extension of
|
||||
* the name. Return is 0 for success, -1 for failure.
|
||||
**********************************************************************/
|
||||
|
||||
inT8 IMAGE::read_header( //get file header
|
||||
const char *name //name of image
|
||||
) {
|
||||
inT8 type; //image type
|
||||
|
||||
destroy(); //destroy old image
|
||||
//get type
|
||||
type = name_to_image_type (name);
|
||||
if (type < 0 || imagetypes[type].opener == NULL) {
|
||||
CANTREADIMAGETYPE.error ("IMAGE::read_header", TESSLOG, name);
|
||||
return -1; //read not supported
|
||||
}
|
||||
#ifdef __UNIX__
|
||||
if ((fd = open (name, O_RDONLY)) < 0)
|
||||
#endif
|
||||
#if defined (__MSW32__) || defined (__MAC__)
|
||||
if ((fd = open (name, O_RDONLY | O_BINARY)) < 0)
|
||||
#endif
|
||||
{
|
||||
CANTOPENFILE.error ("IMAGE::read_header", TESSLOG, name);
|
||||
return -1; //failed
|
||||
}
|
||||
lineskip =
|
||||
(*imagetypes[type].opener) (fd, &xsize, &ysize, &bpp, &photo_interp,
|
||||
&res);
|
||||
if (lineskip == -1) {
|
||||
//get header
|
||||
bpp = 0; //still empty
|
||||
close(fd);
|
||||
fd = -1;
|
||||
return -1; //failed
|
||||
}
|
||||
if (res <= 0)
|
||||
res = image_default_resolution;
|
||||
// fprintf(stderr,"Image size=(%d,%d), bpp=%d\n",
|
||||
// xsize,ysize,bpp);
|
||||
//bytes per line
|
||||
xdim = COMPUTE_IMAGE_XDIM (xsize, bpp);
|
||||
bps = bpp == 24 ? 8 : bpp;
|
||||
bytespp = (bpp + 7) / 8;
|
||||
//funtion to read with
|
||||
reader = imagetypes[type].reader;
|
||||
return 0; //success
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* read
|
||||
*
|
||||
* Read a previously opened image file into memory.
|
||||
* If buflines is 0, the whole image is read in one go.
|
||||
* If buflines>0, memory space is reserved for reading just that many
|
||||
* lines at once.
|
||||
* As soon as a request is made to get a line past the end of the buffer,
|
||||
* the buffer is re-read with a 50% overlap.
|
||||
* Backward seeks are not allowed.
|
||||
* Read returns -1 in case of failure or 0 if successful.
|
||||
**********************************************************************/
|
||||
|
||||
inT8 IMAGE::read( //get rest of image
|
||||
inT32 buflines //size of buffer
|
||||
) {
|
||||
inT32 row; //image row
|
||||
BOOL8 failed; //read failed
|
||||
|
||||
if (fd < 0 || image != NULL)
|
||||
IMAGEUNDEFINED.error ("IMAGE::read", ABORT, NULL);
|
||||
|
||||
if (buflines <= 0 || buflines > ysize || reader == NULL)
|
||||
buflines = ysize; //default to all
|
||||
bufheight = buflines;
|
||||
image =
|
||||
(uinT8 *) alloc_big_mem ((size_t) (xdim * bufheight * sizeof (uinT8)));
|
||||
if (image == NULL) {
|
||||
MEMORY_OUT.error ("IMAGE::read", TESSLOG, NULL);
|
||||
destroy();
|
||||
return -1;
|
||||
}
|
||||
captured = FALSE;
|
||||
ymax = ysize;
|
||||
ymin = ysize - buflines; //amount of image read
|
||||
if (reader != NULL && lineskip < 0)
|
||||
failed = (*reader) (fd, image, xsize, ysize, bpp, xdim) < 0;
|
||||
else {
|
||||
if (lineskip == 0)
|
||||
failed =::read (fd, (char *) image,
|
||||
(size_t) (xdim * bufheight)) != xdim * bufheight;
|
||||
else {
|
||||
for (failed = FALSE, row = 0; row < bufheight && !failed; row++) {
|
||||
failed =::read (fd, (char *) image + row * xdim,
|
||||
(size_t) xdim) != xdim;
|
||||
failed |= lseek (fd, lineskip, SEEK_CUR) < 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (failed) {
|
||||
READFAILED.error ("IMAGE::read", TESSLOG, NULL);
|
||||
destroy();
|
||||
return -1; //read failed
|
||||
}
|
||||
if (ymin <= 0) {
|
||||
close(fd); //finished reading
|
||||
fd = -1; //not open now
|
||||
}
|
||||
return 0; //success
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* bufread
|
||||
*
|
||||
* Read a bit more of an image into the buffer.
|
||||
**********************************************************************/
|
||||
|
||||
inT8 IMAGE::bufread( //read more into buffer
|
||||
inT32 y //required coord
|
||||
) {
|
||||
inT32 readtop; //no of lines copied
|
||||
inT32 linestoread; //no of lines to read
|
||||
inT32 row; //row to read
|
||||
BOOL8 failed; //read failed
|
||||
|
||||
//copy needed?
|
||||
if (y + bufheight / 2 >= ymin) {
|
||||
//no of lines to move
|
||||
readtop = y + bufheight / 2 - ymin + 1;
|
||||
//copy inside it
|
||||
copy_sub_image (this, 0, ymin, xsize, readtop, this, 0, ymax - readtop, TRUE);
|
||||
}
|
||||
else
|
||||
readtop = 0;
|
||||
ymax = y + bufheight / 2; //new top of image
|
||||
ymin = ymax - bufheight; //possible bottom
|
||||
if (ymin < 0)
|
||||
ymin = 0; //clip to image size
|
||||
linestoread = ymax - ymin - readtop;
|
||||
if (lineskip == 0)
|
||||
failed =::read (fd, (char *) (image + xdim * readtop),
|
||||
(size_t) (xdim * linestoread)) != xdim * linestoread;
|
||||
else {
|
||||
for (failed = FALSE, row = 0; row < linestoread && !failed; row++) {
|
||||
failed =::read (fd, (char *) (image + (readtop + row) * xdim),
|
||||
(size_t) xdim) != xdim;
|
||||
failed |= lseek (fd, lineskip, SEEK_CUR) < 0;
|
||||
}
|
||||
}
|
||||
if (failed) {
|
||||
READFAILED.error ("IMAGE::bufread", TESSLOG, NULL);
|
||||
return -1; //read failed
|
||||
}
|
||||
if (ymin <= 0) {
|
||||
close(fd); //finished reading
|
||||
fd = -1; //not open now
|
||||
}
|
||||
return 0; //success
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* write
|
||||
*
|
||||
* Write an image to a file in a format determined by the name.
|
||||
**********************************************************************/
|
||||
|
||||
inT8 IMAGE::write( //write image
|
||||
const char *name //name to write
|
||||
) {
|
||||
inT8 type; //type of image
|
||||
|
||||
if (bpp == 0 || image == NULL || bufheight != ysize)
|
||||
IMAGEUNDEFINED.error ("IMAGE::write", ABORT, NULL);
|
||||
if (fd >= 0) {
|
||||
close(fd); //close old file
|
||||
fd = -1; //no longer open
|
||||
}
|
||||
//get image type
|
||||
type = name_to_image_type (name);
|
||||
if (type < 0 || imagetypes[type].writer == NULL) {
|
||||
CANTWRITEIMAGETYPE.error ("IMAGE::write", TESSLOG, name);
|
||||
return -1; //write not supported
|
||||
}
|
||||
#ifdef __UNIX__
|
||||
if ((fd = creat (name, 0666)) < 0)
|
||||
#endif
|
||||
#ifdef __MSW32__
|
||||
if ((fd = open (name, _O_CREAT | _O_WRONLY | _O_BINARY, _S_IWRITE)) < 0)
|
||||
#endif
|
||||
#ifdef __MAC__
|
||||
if ((fd = creat (name, O_WRONLY | O_BINARY)) < 0)
|
||||
#endif
|
||||
{
|
||||
CANTCREATEFILE.error ("IMAGE::write", TESSLOG, name);
|
||||
return -1; //failed
|
||||
}
|
||||
if (res <= 0)
|
||||
res = image_default_resolution;
|
||||
if ((*imagetypes[type].writer) (fd, image, xsize, ysize, bpp, photo_interp,
|
||||
res) < 0) {
|
||||
//get header
|
||||
//write failed
|
||||
WRITEFAILED.error ("IMAGE::write", TESSLOG, name);
|
||||
close(fd);
|
||||
fd = -1;
|
||||
return -1; //failed
|
||||
}
|
||||
return 0; //success
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
/**********************************************************************
|
||||
* File: imgio.h (Formerly imageio.h)
|
||||
* Description: Header file for image input/output functions.
|
||||
* Author: Ray Smith
|
||||
* Created: Mon Jun 11 12:55:34 BST 1990
|
||||
*
|
||||
* (C) Copyright 1990, Hewlett-Packard Ltd.
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#ifndef IMGIO_H
|
||||
#define IMGIO_H
|
||||
#endif
|
@ -43,7 +43,6 @@
|
||||
#include "imgerrs.h"
|
||||
#include "memry.h"
|
||||
#include "imgs.h"
|
||||
#include "imgio.h"
|
||||
#include "imgunpk.h"
|
||||
|
||||
#define FIXED_COLOURS 32 /*number of fixed colours */
|
||||
@ -1492,10 +1491,8 @@ void IMAGE::check_legal_access( //check coords are legal
|
||||
if (x < 0 || x >= xsize || y < 0 || y >= ysize || x + xext > xsize)
|
||||
BADIMAGECOORDS.error ("IMAGE::check_legal_access",
|
||||
ABORT, "(%d+%d,%d)", x, xext, y);
|
||||
if (y >= ymax)
|
||||
if (y < ymin || y >= ymax)
|
||||
BADIMAGESEEK.error ("IMAGE::check_legal_access", ABORT, "(%d,%d)", x, y);
|
||||
if (y < ymin)
|
||||
bufread(y); //read some more
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBLEPT
|
||||
|
@ -18,12 +18,8 @@
|
||||
**********************************************************************/
|
||||
|
||||
#include "mfcpch.h" //precompiled headers
|
||||
#ifdef __MSW32__
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
/*
|
||||
** Include automatically generated configuration file if running autoconf
|
||||
*/
|
||||
@ -33,21 +29,9 @@
|
||||
#define __MOTO__ // Big-endian.
|
||||
#endif
|
||||
#endif
|
||||
#ifdef USING_GETTEXT
|
||||
#include <libintl.h>
|
||||
#include <locale.h>
|
||||
#define _(x) gettext(x)
|
||||
#else
|
||||
#define _(x) (x)
|
||||
#endif
|
||||
|
||||
#include "fileerr.h"
|
||||
#include "imgerrs.h"
|
||||
#include "img.h"
|
||||
#include "bitstrm.h"
|
||||
#include "tprintf.h"
|
||||
#include "serialis.h"
|
||||
#include "imgtiff.h"
|
||||
#include "helpers.h"
|
||||
|
||||
#define INTEL 0x4949
|
||||
#define MOTO 0x4d4d
|
||||
@ -88,101 +72,11 @@ typedef struct
|
||||
inT32 value;
|
||||
} TIFFENTRY; //tiff tag entry
|
||||
|
||||
typedef struct myrational
|
||||
{
|
||||
inT32 top;
|
||||
inT32 bottom;
|
||||
} MYRATIONAL; //type 5
|
||||
|
||||
//statics for the run length codes
|
||||
#define EOL_CODE 0x800
|
||||
#define EOL_MASK 0xfff
|
||||
#define EOL_LENGTH 12 //12 bits
|
||||
#define SHORT_CODE_SIZE 64 //no of short codes
|
||||
#define LONG_CODE_SIZE 40 //no of long codes
|
||||
|
||||
const uinT16 short_white_codes[SHORT_CODE_SIZE] = {
|
||||
0xac, 0x38, 0xe, 0x1, 0xd, 0x3, 0x7, 0xf,
|
||||
0x19, 0x5, 0x1c, 0x2, 0x4, 0x30, 0xb, 0x2b,
|
||||
0x15, 0x35, 0x72, 0x18, 0x8, 0x74, 0x60, 0x10,
|
||||
0xa, 0x6a, 0x64, 0x12, 0xc, 0x40, 0xc0, 0x58,
|
||||
0xd8, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x14,
|
||||
0x94, 0x54, 0xd4, 0x34, 0xb4, 0x20, 0xa0, 0x50,
|
||||
0xd0, 0x4a, 0xca, 0x2a, 0xaa, 0x24, 0xa4, 0x1a,
|
||||
0x9a, 0x5a, 0xda, 0x52, 0xd2, 0x4c, 0xcc, 0x2c
|
||||
};
|
||||
const uinT8 short_white_lengths[SHORT_CODE_SIZE] = {
|
||||
8, 6, 4, 4, 4, 4, 4, 4,
|
||||
5, 5, 5, 5, 6, 6, 6, 6,
|
||||
6, 6, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8
|
||||
};
|
||||
const uinT16 short_black_codes[SHORT_CODE_SIZE] = {
|
||||
0x3b0, 0x2, 0x3, 0x1, 0x6, 0xc, 0x4, 0x18,
|
||||
0x28, 0x8, 0x10, 0x50, 0x70, 0x20, 0xe0, 0x30,
|
||||
0x3a0, 0x60, 0x40, 0x730, 0xb0, 0x1b0, 0x760, 0xa0,
|
||||
0x740, 0xc0, 0x530, 0xd30,
|
||||
0x330, 0xb30, 0x160, 0x960,
|
||||
0x560, 0xd60, 0x4b0, 0xcb0,
|
||||
0x2b0, 0xab0, 0x6b0, 0xeb0,
|
||||
0x360, 0xb60, 0x5b0, 0xdb0,
|
||||
0x2a0, 0xaa0, 0x6a0, 0xea0,
|
||||
0x260, 0xa60, 0x4a0, 0xca0,
|
||||
0x240, 0xec0, 0x1c0, 0xe40,
|
||||
0x140, 0x1a0, 0x9a0, 0xd40,
|
||||
0x340, 0x5a0, 0x660, 0xe60
|
||||
};
|
||||
const uinT8 short_black_lengths[SHORT_CODE_SIZE] = {
|
||||
10, 3, 2, 2, 3, 4, 4, 5,
|
||||
6, 6, 7, 7, 7, 8, 8, 9,
|
||||
10, 10, 10, 11, 11, 11, 11, 11,
|
||||
11, 11, 12, 12, 12, 12, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12
|
||||
};
|
||||
const uinT16 long_white_codes[LONG_CODE_SIZE] = {
|
||||
0x1b, 0x9, 0x3a, 0x76, 0x6c, 0xec, 0x26, 0xa6,
|
||||
0x16, 0xe6, 0x66, 0x166, 0x96, 0x196, 0x56, 0x156,
|
||||
0xd6, 0x1d6, 0x36, 0x136, 0xb6, 0x1b6, 0x32, 0x132,
|
||||
0xb2, 0x6, 0x1b2,
|
||||
0x80, 0x180, 0x580, 0x480, 0xc80,
|
||||
0x280, 0xa80, 0x680, 0xe80, 0x380, 0xb80, 0x780, 0xf80
|
||||
};
|
||||
const uinT8 long_white_lengths[LONG_CODE_SIZE] = {
|
||||
5, 5, 6, 7, 8, 8, 8, 8,
|
||||
8, 8, 9, 9, 9, 9, 9, 9,
|
||||
9, 9, 9, 9, 9, 9, 9, 9,
|
||||
9, 6, 9, 11, 11, 11, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12
|
||||
};
|
||||
const uinT16 long_black_codes[LONG_CODE_SIZE] = {
|
||||
0x3c0, 0x130, 0x930, 0xda0,
|
||||
0xcc0, 0x2c0, 0xac0, 0x6c0,
|
||||
0x16c0, 0xa40, 0x1a40, 0x640,
|
||||
0x1640, 0x9c0, 0x19c0, 0x5c0,
|
||||
0x15c0, 0xdc0, 0x1dc0, 0x940,
|
||||
0x1940, 0x540, 0x1540, 0xb40,
|
||||
0x1b40, 0x4c0, 0x14c0,
|
||||
0x80, 0x180, 0x580, 0x480, 0xc80,
|
||||
0x280, 0xa80, 0x680, 0xe80, 0x380, 0xb80, 0x780, 0xf80
|
||||
};
|
||||
const uinT8 long_black_lengths[LONG_CODE_SIZE] = {
|
||||
10, 12, 12, 12, 12, 12, 12, 13,
|
||||
13, 13, 13, 13, 13, 13, 13, 13,
|
||||
13, 13, 13, 13, 13, 13, 13, 13,
|
||||
13, 13, 13, 11, 11, 11, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12
|
||||
};
|
||||
|
||||
// CountTiffPages
|
||||
// Returns the number of pages in the file if it is a tiff file, otherwise 0.
|
||||
// WARNING: requires __MOTO__ to be #defined on a big-endian system.
|
||||
// On linux this is handled by configure - see above.
|
||||
int CountTiffPages(FILE* fp) {
|
||||
if (fp == NULL) return 0;
|
||||
// Read header
|
||||
@ -217,511 +111,3 @@ int CountTiffPages(FILE* fp) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// TODO(rays) The rest of this file is redundant and should be deleted.
|
||||
|
||||
/**********************************************************************
|
||||
* open_tif_image
|
||||
*
|
||||
* Read the header of a tif format image and prepare to read the rest.
|
||||
**********************************************************************/
|
||||
|
||||
inT8 open_tif_image( //read header
|
||||
int fd, //file to read
|
||||
inT32 *xsize, //size of image
|
||||
inT32 *ysize,
|
||||
inT8 *bpp, //bits per pixel
|
||||
inT8 *photo, //interpretation
|
||||
inT32 *res //resolution
|
||||
) {
|
||||
inT16 filetype;
|
||||
inT32 start; //start of tiff directory
|
||||
inT16 entries; //no of tiff entries
|
||||
inT32 imagestart; //location of image in file
|
||||
inT32 resoffset; //location of res
|
||||
TIFFENTRY tiffentry; //tag table entry
|
||||
BOOL8 compressed; //compression control
|
||||
MYRATIONAL resinfo; //resolution
|
||||
BOOL8 strips = false; //if in strips
|
||||
|
||||
*xsize = -1; //illegal values
|
||||
*ysize = -1;
|
||||
*bpp = -1;
|
||||
*res = -1;
|
||||
resoffset = -1;
|
||||
if (read (fd, (char *) &filetype, sizeof filetype) != sizeof filetype
|
||||
|| (filetype != INTEL && filetype != MOTO)) {
|
||||
BADIMAGEFORMAT.error ("read_tif_image", TESSLOG, "Filetype");
|
||||
return -1;
|
||||
}
|
||||
lseek (fd, 4L, 0);
|
||||
if (read (fd, (char *) &start, sizeof start) != sizeof start) {
|
||||
READFAILED.error ("read_tif_image", TESSLOG, "Start of tag table");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (filetype != __NATIVE__)
|
||||
start = reverse32 (start);
|
||||
if (start <= 0) {
|
||||
BADIMAGEFORMAT.error ("read_tif_image", TESSLOG, "Start of tag table");
|
||||
return -1;
|
||||
}
|
||||
lseek (fd, start, 0);
|
||||
if (read (fd, (char *) &entries, sizeof (inT16)) != sizeof (inT16)) {
|
||||
BADIMAGEFORMAT.error ("read_tif_image", TESSLOG, "Size of tag table");
|
||||
return -1;
|
||||
}
|
||||
if (filetype != __NATIVE__)
|
||||
entries = reverse16 (entries);
|
||||
// printf("No of tiff directory entries=%d\n",entries);
|
||||
imagestart = 0;
|
||||
compressed = FALSE;
|
||||
int samples_per_pixel = 1;
|
||||
int bits_per_sample = 1;
|
||||
for (; entries-- > 0;) {
|
||||
if (read (fd, (char *) &tiffentry, sizeof tiffentry) !=
|
||||
sizeof tiffentry) {
|
||||
BADIMAGEFORMAT.error ("read_tif_image", TESSLOG, "Tag table entry");
|
||||
return -1;
|
||||
}
|
||||
if (filetype != __NATIVE__) {
|
||||
tiffentry.type = reverse16 (tiffentry.type);
|
||||
tiffentry.tag = reverse16 (tiffentry.tag);
|
||||
tiffentry.length = reverse32 (tiffentry.length);
|
||||
}
|
||||
if (tiffentry.type != 3) { //Full 32bit value
|
||||
if (filetype != __NATIVE__)
|
||||
tiffentry.value = reverse32 (tiffentry.value);
|
||||
}
|
||||
else {
|
||||
/* A 16bit value in 4 bytes - handle with care. SEE NOTE at start of file */
|
||||
if (__NATIVE__ == MOTO) {
|
||||
if (filetype == MOTO) //MOTO file on MOTO Machine
|
||||
tiffentry.value = tiffentry.value >> 16;
|
||||
else //INTEL file on MOTO Machine
|
||||
tiffentry.value = reverse32 (tiffentry.value);
|
||||
}
|
||||
else { //INTEL Machine
|
||||
if (filetype == MOTO) //MOTO file on INTEL Machine
|
||||
tiffentry.value = reverse16 ((uinT16) tiffentry.value);
|
||||
//INTEL file on INTEL Machine NO ACTION NEEDED
|
||||
}
|
||||
//Clear top 2 MSBytes
|
||||
tiffentry.value &= 0x0000ffff;
|
||||
}
|
||||
|
||||
// printf("Tag=%x, Type=%x, Length=%x, value=%x\n",
|
||||
// tiffentry.tag,tiffentry.type,tiffentry.length,tiffentry.value);
|
||||
switch (tiffentry.tag) {
|
||||
case 0x101:
|
||||
*ysize = tiffentry.value;
|
||||
break;
|
||||
case 0x100:
|
||||
*xsize = tiffentry.value;
|
||||
break;
|
||||
case 0x102:
|
||||
if (tiffentry.length == 1)
|
||||
bits_per_sample = (inT8) tiffentry.value;
|
||||
else
|
||||
bits_per_sample = 8;
|
||||
break;
|
||||
case 0x115:
|
||||
samples_per_pixel = (inT8) tiffentry.value;
|
||||
break;
|
||||
case 0x111:
|
||||
imagestart = tiffentry.value;
|
||||
strips = tiffentry.length > 1;
|
||||
break;
|
||||
case 0x103:
|
||||
if (tiffentry.value == 3) {
|
||||
compressed = TRUE;
|
||||
}
|
||||
else if (tiffentry.value != 1) {
|
||||
BADIMAGEFORMAT.error ("read_tif_image", TESSLOG, "Compression");
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case 0x11a:
|
||||
case 0x11b:
|
||||
//resolution
|
||||
resoffset = tiffentry.value;
|
||||
break;
|
||||
case 0x106:
|
||||
*photo = (inT8) tiffentry.value;
|
||||
break;
|
||||
} //endswitch
|
||||
}
|
||||
if (*xsize <= 0 || *ysize <= 0 || imagestart <= 0) {
|
||||
BADIMAGEFORMAT.error ("read_tif_image", TESSLOG, "Vital tag");
|
||||
return -1;
|
||||
}
|
||||
tprintf(_("Image has %d * %d bit%c per pixel, and size (%d,%d)\n"),
|
||||
bits_per_sample, samples_per_pixel, bits_per_sample == 1 ? ' ' : 's',
|
||||
*xsize, *ysize);
|
||||
*bpp = bits_per_sample * samples_per_pixel;
|
||||
if (resoffset >= 0) {
|
||||
lseek (fd, resoffset, 0);
|
||||
if (read (fd, (char *) &resinfo, sizeof (resinfo)) != sizeof (resinfo)) {
|
||||
READFAILED.error ("read_tif_image", TESSLOG, "Resolution");
|
||||
return -1;
|
||||
}
|
||||
if (filetype != __NATIVE__) {
|
||||
resinfo.top = reverse32 (resinfo.top);
|
||||
resinfo.bottom = reverse32 (resinfo.bottom);
|
||||
}
|
||||
*res = resinfo.top / resinfo.bottom;
|
||||
tprintf (_("Resolution=%d\n"), *res);
|
||||
}
|
||||
lseek (fd, (long) imagestart, 0);
|
||||
if (strips) {
|
||||
if (read (fd, (char *) &imagestart, sizeof (imagestart)) !=
|
||||
sizeof (imagestart)) {
|
||||
READFAILED.error ("read_tif_image", TESSLOG, "Strip offset");
|
||||
return -1;
|
||||
}
|
||||
if (filetype != __NATIVE__)
|
||||
imagestart = reverse32 (imagestart);
|
||||
//indirection
|
||||
lseek (fd, (long) imagestart, 0);
|
||||
}
|
||||
return compressed ? -2 : 0;
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* read_tif_image
|
||||
*
|
||||
* Read a whole tif image into memory.
|
||||
**********************************************************************/
|
||||
|
||||
inT8 read_tif_image(int fd, // file to read
|
||||
uinT8 *pixels, // pixels of image
|
||||
inT32 xsize, // size of image
|
||||
inT32 ysize,
|
||||
inT8 bpp, // bits per pixel
|
||||
inT32) { // bytes per line
|
||||
inT32 xindex; // indices in image
|
||||
inT32 yindex;
|
||||
inT32 length; // short length
|
||||
inT32 biglength; // extender
|
||||
const uinT8 *lengths; // current lengths
|
||||
const uinT16 *codes; // current codes
|
||||
uinT16 codeword; // current code word
|
||||
IMAGELINE imageline; // current line
|
||||
IMAGE image; // dummy image
|
||||
R_BITSTREAM bits; // read bitstream
|
||||
uinT8 colour; // current colour
|
||||
|
||||
image.capture(pixels, xsize, ysize, bpp);
|
||||
codeword = bits.open(fd); // open bitstream
|
||||
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
|
||||
|
||||
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", TESSLOG, "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", TESSLOG, "Version");
|
||||
return -1;
|
||||
}
|
||||
if (type != __NATIVE__)
|
||||
start = reverse32 (start);
|
||||
if (write (fd, (char *) &start, sizeof start) != sizeof start) {
|
||||
WRITEFAILED.error ("write_tif_image", TESSLOG, "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", TESSLOG, "Entries");
|
||||
return -1;
|
||||
}
|
||||
//line length
|
||||
size = COMPUTE_IMAGE_XDIM (xsize, bpp);
|
||||
size *= ysize; //total image size
|
||||
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", TESSLOG, "Tag Table");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (write (fd, (char *) &zero, sizeof zero) != sizeof zero) {
|
||||
WRITEFAILED.error ("write_tif_image", TESSLOG, "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", TESSLOG, "Resolution");
|
||||
return -1;
|
||||
}
|
||||
if (write (fd, (char *) pixels, (size_t) size) != size) {
|
||||
WRITEFAILED.error ("write_tif_image", TESSLOG, "Image");
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
@ -21,67 +21,9 @@
|
||||
#define IMGTIFF_H
|
||||
|
||||
#include "host.h"
|
||||
#include "bitstrm.h"
|
||||
|
||||
// CountTiffPages
|
||||
// Returns the number of pages in the file if it is a tiff file, otherwise 0.
|
||||
int CountTiffPages(FILE* fp);
|
||||
|
||||
inT8 open_tif_image( //read header
|
||||
int fd, //file to read
|
||||
inT32 *xsize, //size of image
|
||||
inT32 *ysize,
|
||||
inT8 *bpp, //bits per pixel
|
||||
inT8 *photo, //interpretation
|
||||
inT32 *res //resolution
|
||||
);
|
||||
inT8 read_tif_image( //read whole image
|
||||
int fd, //file to read
|
||||
uinT8 *pixels, //pixels of image
|
||||
inT32 xsize, //size of image
|
||||
inT32 ysize,
|
||||
inT8 bpp, //bits per pixel
|
||||
inT32 //bytes per line
|
||||
);
|
||||
inT32 read_eol( //read end of line
|
||||
R_BITSTREAM *bits, //bitstream to read
|
||||
uinT16 &code //current code
|
||||
);
|
||||
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
|
||||
);
|
||||
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
|
||||
);
|
||||
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
|
||||
);
|
||||
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
|
||||
);
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user