summaryrefslogtreecommitdiffstats
path: root/media/img_utils/include/img_utils/TiffHelpers.h
blob: 0969e4d98a84f2d5a7f61b6ba563192574ca9c84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright 2014 The Android Open Source Project
 *
 * 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 IMG_UTILS_TIFF_HELPERS_H
#define IMG_UTILS_TIFF_HELPERS_H

#include <stdint.h>

namespace android {
namespace img_utils {

const uint8_t ZERO_WORD[] = {0, 0, 0, 0};

#define BAIL_ON_FAIL(x, flag) \
    if ((flag = (x)) != OK) return flag;

#define BYTES_TILL_WORD(index) \
    ((TIFF_WORD_SIZE - ((index) % TIFF_WORD_SIZE)) % TIFF_WORD_SIZE)

#define WORD_ALIGN(count) \
    count += BYTES_TILL_WORD(count);

#define ZERO_TILL_WORD(output, index, ret) \
    { \
        size_t remaining = BYTES_TILL_WORD(index); \
        if (remaining > 0) { \
            BAIL_ON_FAIL((output)->write(ZERO_WORD, 0, remaining), ret); \
        } \
    }

/**
 * Basic TIFF header constants.
 */
enum {
    BAD_OFFSET = 0,
    TIFF_WORD_SIZE = 4, // Size in bytes
    IFD_HEADER_SIZE = 2, // Size in bytes
    IFD_FOOTER_SIZE = 4, // Size in bytes
    TIFF_ENTRY_SIZE = 12, // Size in bytes
    MAX_IFD_ENTRIES = UINT16_MAX,
    FILE_HEADER_SIZE = 8, // Size in bytes
    ENDIAN_MARKER_SIZE = 2, // Size in bytes
    TIFF_MARKER_SIZE = 2, // Size in bytes
    OFFSET_MARKER_SIZE = 4, // Size in bytes
    TIFF_FILE_MARKER = 42,
    BIG_ENDIAN_MARKER = 0x4D4Du,
    LITTLE_ENDIAN_MARKER = 0x4949u
};

/**
 * Constants for the TIFF tag types.
 */
enum TagType {
    UNKNOWN_TAGTYPE = 0,
    BYTE=1,
    ASCII,
    SHORT,
    LONG,
    RATIONAL,
    SBYTE,
    UNDEFINED,
    SSHORT,
    SLONG,
    SRATIONAL,
    FLOAT,
    DOUBLE
};

/**
 * Sizes of the TIFF entry fields (in bytes).
 */
enum {
    TAG_SIZE = 2,
    TYPE_SIZE = 2,
    COUNT_SIZE = 4,
    OFFSET_SIZE = 4
};

/**
 * Convenience IFD id constants.
 */
enum {
    IFD_0 = 0,
    RAW_IFD,
    PROFILE_IFD,
    PREVIEW_IFD
};

inline size_t getTypeSize(TagType type) {
    switch(type) {
        case UNDEFINED:
        case ASCII:
        case BYTE:
        case SBYTE:
            return 1;
        case SHORT:
        case SSHORT:
            return 2;
        case LONG:
        case SLONG:
        case FLOAT:
            return 4;
        case RATIONAL:
        case SRATIONAL:
        case DOUBLE:
            return 8;
        default:
            return 0;
    }
}

inline uint32_t calculateIfdSize(size_t numberOfEntries) {
    return IFD_HEADER_SIZE + IFD_FOOTER_SIZE + TIFF_ENTRY_SIZE * numberOfEntries;
}

} /*namespace img_utils*/
} /*namespace android*/

#endif /*IMG_UTILS_TIFF_HELPERS_H*/