Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 159 lines (138 sloc) 4.735 kb
b44f1ef Add version 0.21
Adam Langley authored
1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: agl@google.com (Adam Langley)
3
4 #ifndef THIRD_PARTY_JBIG2ENC_JBIG2SEGMENTS_H__
5 #define THIRD_PARTY_JBIG2ENC_JBIG2SEGMENTS_H__
6
7 #include <vector>
b92225b Add version 0.26
Adam Langley authored
8 #ifdef _MSC_VER
9 #include <winsock2.h>
10 #else
b44f1ef Add version 0.21
Adam Langley authored
11 #include <netinet/in.h>
b92225b Add version 0.26
Adam Langley authored
12 #endif
b44f1ef Add version 0.21
Adam Langley authored
13
14 // -----------------------------------------------------------------------------
b92225b Add version 0.26
Adam Langley authored
15 // See comments in jbig2structs.h about the bit packing in this structure.
b44f1ef Add version 0.21
Adam Langley authored
16 // -----------------------------------------------------------------------------
b92225b Add version 0.26
Adam Langley authored
17 #if defined(WIN32)
18 #pragma pack(1)
19 #endif
b44f1ef Add version 0.21
Adam Langley authored
20 struct jbig2_segment {
21 u32 number;
22 #ifndef _BIG_ENDIAN
b92225b Add version 0.26
Adam Langley authored
23 unsigned char type : 6;
24 unsigned char page_assoc_size : 1;
25 unsigned char deferred_non_retain : 1;
b44f1ef Add version 0.21
Adam Langley authored
26 #else
b92225b Add version 0.26
Adam Langley authored
27 unsigned char deferred_non_retain : 1;
28 unsigned char page_assoc_size : 1;
29 unsigned char type : 6;
b44f1ef Add version 0.21
Adam Langley authored
30 #endif
31
32 #ifndef _BIG_ENDIAN
b92225b Add version 0.26
Adam Langley authored
33 unsigned char retain_bits : 5;
34 unsigned char segment_count : 3;
35 #else
36 unsigned char segment_count : 3;
37 unsigned char retain_bits : 5;
38 #endif
39 }
40 #if defined(WIN32)
d529be5 Fix a couple of issues with Windows building
Adam Langley authored
41 ;
b92225b Add version 0.26
Adam Langley authored
42 #pragma pack()
b44f1ef Add version 0.21
Adam Langley authored
43 #else
b92225b Add version 0.26
Adam Langley authored
44 __attribute__((packed));
b44f1ef Add version 0.21
Adam Langley authored
45 #endif
b92225b Add version 0.26
Adam Langley authored
46 ;
b44f1ef Add version 0.21
Adam Langley authored
47
48 // -----------------------------------------------------------------------------
49 // This structure represents a JBIG2 segment header because they have too many
50 // variable length fields (number of referred to segments, page length etc).
51 // You should access and set the members directly. Endian swapping is carried
52 // out internally.
53 // -----------------------------------------------------------------------------
54 struct Segment {
55 unsigned number; // segment number
56 int type; // segment type (see enum in jbig2structs.h)
57 int deferred_non_retain; // see JBIG2 spec
58 int retain_bits;
59 std::vector<unsigned> referred_to; // list of segment numbers referred to
60 unsigned page; // page number
61 unsigned len; // length of trailing data
62
63 Segment()
64 : number(0),
65 type(0),
66 deferred_non_retain(0),
67 retain_bits(0),
68 page(0),
69 len(0) {}
70
71 // ---------------------------------------------------------------------------
72 // Return the size of the segment reference for this segment. Segments can
73 // only refer to previous segments, so the bits needed is determined by the
74 // number of this segment. (7.2.5)
75 // ---------------------------------------------------------------------------
76 unsigned reference_size() const {
77 int refsize;
78 if (number <= 256) {
79 refsize = 1;
80 } else if (number <= 65536) {
81 refsize = 2;
82 } else {
83 refsize = 4;
84 }
85
86 return refsize;
87 }
88
89 // ---------------------------------------------------------------------------
0077964 @smuehlst The size of the segment page association field must be four bytes for
smuehlst authored
90 // Return the size of the segment page association field for this segment.
91 // (7.2.6)
92 // ---------------------------------------------------------------------------
93 unsigned page_size() const {
94 return page <= 255 ? 1 : 4;
95 }
96
97 // ---------------------------------------------------------------------------
b44f1ef Add version 0.21
Adam Langley authored
98 // Return the number of bytes that this segment header will take up
99 // ---------------------------------------------------------------------------
100 unsigned size() const {
101 const int refsize = reference_size();
0077964 @smuehlst The size of the segment page association field must be four bytes for
smuehlst authored
102 const int pagesize = page_size();
b44f1ef Add version 0.21
Adam Langley authored
103
104 return sizeof(struct jbig2_segment) + refsize * referred_to.size() +
105 pagesize + sizeof(u32);
106 }
107
108 // ---------------------------------------------------------------------------
109 // Serialise this segment header into the memory pointed to by buf, which
110 // must be at least long enough to contain it (e.g. size() bytes)
111 // ---------------------------------------------------------------------------
112 void write(u8 *buf) {
113 struct jbig2_segment s;
114 memset(&s, 0, sizeof(s));
115 #define F(x) s.x = x;
116 s.number = htonl(number);
117 s.type = type;
118 s.deferred_non_retain = deferred_non_retain;
119 s.retain_bits = retain_bits;
120 #undef F
121 s.segment_count = referred_to.size();
122
0077964 @smuehlst The size of the segment page association field must be four bytes for
smuehlst authored
123 const int pagesize = page_size();
b44f1ef Add version 0.21
Adam Langley authored
124 const int refsize = reference_size();
0077964 @smuehlst The size of the segment page association field must be four bytes for
smuehlst authored
125 if (pagesize == 4) s.page_assoc_size = 1;
b44f1ef Add version 0.21
Adam Langley authored
126
127 unsigned j = 0;
128
129 memcpy(buf, &s, sizeof(s));
130 j += sizeof(s);
131 #define APPEND(type, val) type __i; __i = val; \
132 memcpy(&buf[j], &__i, sizeof(type)); \
133 j += sizeof(type)
134
135 for (std::vector<unsigned>::const_iterator i = referred_to.begin();
136 i != referred_to.end(); ++i) {
137 if (refsize == 4) {
138 APPEND(u32, htonl(*i));
139 } else if (refsize == 2) {
140 APPEND(u16, htons(*i));
141 } else {
142 APPEND(u8, *i);
143 }
144 }
145
0077964 @smuehlst The size of the segment page association field must be four bytes for
smuehlst authored
146 if (pagesize == 4) {
147 APPEND(u32, htonl(page));
b44f1ef Add version 0.21
Adam Langley authored
148 } else {
149 APPEND(u8, page);
150 }
151
152 APPEND(u32, htonl(len));
153
154 if (j != size()) abort();
155 }
156 };
157
158 #endif // THIRD_PARTY_JBIG2ENC_JBIG2SEGMENTS_H__
Something went wrong with that request. Please try again.