w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

jbig2segments.h
Go to the documentation of this file.
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>
8 /*
9 #include <netinet/in.h>
10 */
11 
12 // -----------------------------------------------------------------------------
13 // See comments in jbig2struct.h about the bit packing in this structure.
14 // -----------------------------------------------------------------------------
15 struct jbig2_segment {
17 #ifndef _BIG_ENDIAN
18  unsigned type : 6;
19  unsigned page_assoc_size : 1;
20  unsigned deferred_non_retain : 1;
21 #else
22  unsigned deferred_non_retain : 1;
23  unsigned page_assoc_size : 1;
24  unsigned type : 6;
25 #endif
26 
27 #ifndef _BIG_ENDIAN
28  unsigned retain_bits : 5;
29  unsigned segment_count : 3;
30 #else
31  unsigned segment_count : 3;
32  unsigned retain_bits : 5;
33 #endif
34 } __attribute__((packed));
35 
36 // -----------------------------------------------------------------------------
37 // This structure represents a JBIG2 segment header because they have too many
38 // variable length fields (number of referred to segments, page length etc).
39 // You should access and set the members directly. Endian swapping is carried
40 // out internally.
41 // -----------------------------------------------------------------------------
42 struct Segment {
43  unsigned number; // segment number
44  int type; // segment type (see enum in jbig2structs.h)
45  int deferred_non_retain; // see JBIG2 spec
47  std::vector<unsigned> referred_to; // list of segment numbers referred to
48  unsigned page; // page number
49  unsigned len; // length of trailing data
50 
52  : number(0),
53  type(0),
55  retain_bits(0),
56  page(0),
57  len(0) {}
58 
59  // ---------------------------------------------------------------------------
60  // Return the size of the segment reference for this segment. Segments can
61  // only refer to previous segments, so the bits needed is determined by the
62  // number of this segment. (7.2.5)
63  // ---------------------------------------------------------------------------
64  unsigned reference_size() const {
65  int refsize;
66  if (number <= 256) {
67  refsize = 1;
68  } else if (number <= 65536) {
69  refsize = 2;
70  } else {
71  refsize = 4;
72  }
73 
74  return refsize;
75  }
76 
77  // ---------------------------------------------------------------------------
78  // Return the number of bytes that this segment header will take up
79  // ---------------------------------------------------------------------------
80  unsigned size() const {
81  const int refsize = reference_size();
82  const int pagesize = page <= 255 ? 1 : 2;
83 
84  return sizeof(struct jbig2_segment) + refsize * referred_to.size() +
85  pagesize + sizeof(u32);
86  }
87 
88  // ---------------------------------------------------------------------------
89  // Serialise this segment header into the memory pointed to by buf, which
90  // must be at least long enough to contain it (e.g. size() bytes)
91  // ---------------------------------------------------------------------------
92  void write(u8 *buf) {
93  struct jbig2_segment s;
94  memset(&s, 0, sizeof(s));
95 #define F(x) s.x = x;
96  s.number = htonl(number);
97  s.type = type;
98  s.deferred_non_retain = deferred_non_retain;
99  s.retain_bits = retain_bits;
100 #undef F
101  s.segment_count = referred_to.size();
102 
103  const int pagesize = page <= 255 ? 1 : 2;
104  const int refsize = reference_size();
105  if (pagesize == 2) s.page_assoc_size = 1;
106 
107  unsigned j = 0;
108 
109  memcpy(buf, &s, sizeof(s));
110  j += sizeof(s);
111 #define APPEND(type, val) type __i; __i = val; \
112  memcpy(&buf[j], &__i, sizeof(type)); \
113  j += sizeof(type)
114 
115  for (std::vector<unsigned>::const_iterator i = referred_to.begin();
116  i != referred_to.end(); ++i) {
117  if (refsize == 4) {
118  APPEND(u32, htonl(*i));
119  } else if (refsize == 2) {
120  APPEND(u16, htons(*i));
121  } else {
122  APPEND(u8, *i);
123  }
124  }
125 
126  if (pagesize == 2) {
127  APPEND(u16, htonl(page));
128  } else {
129  APPEND(u8, page);
130  }
131 
132  APPEND(u32, htonl(len));
133 
134  if (j != size()) abort();
135  }
136 };
137 
138 #endif // THIRD_PARTY_JBIG2ENC_JBIG2SEGMENTS_H__
#define s
Definition: afcover.h:80
#define memcpy(d, s, n)
Definition: gsftopk.c:64
#define u16
Definition: jbig2arith.cc:25
#define u32
Definition: jbig2arith.cc:24
#define u8
Definition: jbig2arith.cc:26
#define APPEND(type, val)
small capitals from c petite p scientific i
Definition: afcover.h:80
#define buf
#define __attribute__(A)
Definition: synctex.c:338
void abort()
bstring c int memset(void *s, int c, int length)
unsigned number
Definition: jbig2segments.h:43
unsigned reference_size() const
Definition: jbig2segments.h:64
unsigned size() const
Definition: jbig2segments.h:80
int retain_bits
Definition: jbig2segments.h:46
unsigned len
Definition: jbig2segments.h:49
void write(u8 *buf)
Definition: jbig2segments.h:92
unsigned page
Definition: jbig2segments.h:48
int deferred_non_retain
Definition: jbig2segments.h:45
std::vector< unsigned > referred_to
Definition: jbig2segments.h:47
unsigned type
Definition: jbig2segments.h:18
unsigned page_assoc_size
Definition: jbig2segments.h:19
unsigned segment_count
Definition: jbig2segments.h:29
unsigned retain_bits
Definition: jbig2segments.h:28
unsigned deferred_non_retain
Definition: jbig2segments.h:20
Definition: mendex.h:14
int j
Definition: t4ht.c:1589
unsigned long htonl(unsigned long inv)