Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
imgtiff.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: imgtiff.c (Formerly tiff.c)
3  * Description: Max format image reader/writer.
4  * Author: Ray Smith
5  * Created: Mon Jun 11 14:00:21 BST 1990
6  *
7  * (C) Copyright 1990, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include "mfcpch.h" //precompiled headers
21 
22 #include <stdio.h>
23 /*
24 ** Include automatically generated configuration file if running autoconf
25 */
26 #ifdef HAVE_CONFIG_H
27 #include "config_auto.h"
28 #if defined(MOTOROLA_BYTE_ORDER) || defined(WORDS_BIGENDIAN)
29 #define __MOTO__ // Big-endian.
30 #endif
31 #endif
32 
33 #include "imgtiff.h"
34 #include "helpers.h"
35 
36 #define INTEL 0x4949
37 #define MOTO 0x4d4d
38 
39 /*************************************************************************
40  * NOTE ON BIG-ENDIAN vs LITTLE-ENDIAN
41  *
42  * Intel machines store numbers with LSByte in the left position.
43  * Motorola (and PA_RISC) machines use the opposite byte ordering.
44  *
45  * This code is written so that:
46  * a) it will compile and run on EITHER machine type AND
47  * b) the program (on either machine) will process tiff file written in either
48  * Motorola or Intel format.
49  *
50  * The code is compiled with a __NATIVE__ define which is either MOTO or INTEL.
51  * MOTO and INTEL are defined (above) to be the value of the first two bytes of
52  * a tiff file in either format. (This identifies the filetype).
53  *
54  * Subsequent reads and writes normally just reverse the byte order if the
55  * machine type (__NATIVE__) is not equal to the filetype determined from the
56  * first two bytes of the tiff file.
57  *
58  * A special case is the "value" field of the tag structure. This can contain
59  * EITHER a 16bit or a 32bit value. According to the "type" field. The 4 cases
60  * of machine type / file type combinations need to be treated differently in
61  * the case of 16 bit values
62  *************************************************************************/
63 
64 #define ENTRIES 19 /*no of entries */
65 #define START 8 /*start of tag table */
66 
67 typedef struct
68 {
69  uinT16 tag; //entry tag
73 } TIFFENTRY; //tiff tag entry
74 
75 
76 // CountTiffPages
77 // Returns the number of pages in the file if it is a tiff file, otherwise 0.
78 // WARNING: requires __MOTO__ to be #defined on a big-endian system.
79 // On linux this is handled by configure - see above.
80 int CountTiffPages(FILE* fp) {
81  if (fp == NULL) return 0;
82  // Read header
83  inT16 filetype = 0;
84  if (fread(&filetype, sizeof(filetype), 1, fp) != 1 ||
85  (filetype != INTEL && filetype != MOTO)) {
86  return 0;
87  }
88  fseek(fp, 4L, SEEK_SET);
89  int npages = 0;
90  do {
91  inT32 start; // Start of tiff directory.
92  if (fread(&start, sizeof(start), 1, fp) != 1) {
93  return npages;
94  }
95  if (filetype != __NATIVE__)
96  ReverseN(&start, sizeof(start));
97  if (start <= 0) {
98  return npages;
99  }
100  fseek(fp, start, SEEK_SET);
101  inT16 entries; // No of tiff entries.
102  if (fread(&entries, sizeof(entries), 1, fp) != 1) {
103  return npages;
104  }
105  if (filetype != __NATIVE__)
106  ReverseN(&entries, sizeof(entries));
107  // Skip the tags and get to the next start.
108  fseek(fp, entries * sizeof(TIFFENTRY), SEEK_CUR);
109  ++npages;
110  } while (1);
111  return 0;
112 }
113