File: RFC1-SerializedFormat

package info (click to toggle)
postgis 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,052 kB
  • sloc: ansic: 162,204; sql: 93,950; xml: 53,121; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,434; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (274 lines) | stat: -rw-r--r-- 8,565 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
RFC1: serialized format (storage) for RASTER type
------------------------------------------------------
Author: Sandro Santilli <strk@kbt.io>
Date: 2009-01-28
Status: Adopted
Revisions:
 2011-01-24 by Jorge Arévalo
  - Adds isNodataValue bit to band flags
------------------------------------------------------

The goals of the serialized version for RASTER type are:

 - Small memory footprint on deserialization
   This means that the amount of allocated memory
   required for deserialization is minimal

 - Fast access 
   Access to band data must be aligned, saving from
   memory copies on full scan.

 - Ease of format switch
   On-disk format must be allowed to change 
   w/out need for dump-reload of the whole
   database.

The first two goals boil down to forcing alignment of band
data in the serialized format itself, which in turn will
require variable padding based on pixeltype of each band.

For simplicity we will ensure that each band of the 
raster starts at the 8-byte boundary and thus pad
previous structures in the stream accordingly.

The structure will then look like this:

 [HEADER]  [BAND0]    [BAND1]    [BAND2]
           ^aligned   ^aligned   ^aligned

The third goal can be accomplished by adding a version
number to the serialized format so that in case of changes
the deserializer can pick the correct parsing procedure
based on that.

The HEADER
----------

PostgreSQL forces a 4-byte size field a the start of
the detoasted datum, and ensure this start of structure
is aligned to 8-byte.  We'll add version number right after it,
and then make sure the total size is a multiple of 8 bytes.

 The following structure is composed by 8 slots of 8-bytes,
 totaling 64 bytes:

 struct rt_raster_serialized_t {

    /*---[ 8 byte boundary ]---{ */
    uint32_t size;    /* required by postgresql: 4 bytes */
    uint16_t version; /* format version (this is version 0): 2 bytes */
    uint16_t numBands; /* Number of bands: 2 bytes */

    /* }---[ 8 byte boundary ]---{ */
    double scaleX; /* pixel width: 8 bytes */

    /* }---[ 8 byte boundary ]---{ */
    double scaleY; /* pixel height: 8 bytes */

    /* }---[ 8 byte boundary ]---{ */
    double ipX; /* insertion point X: 8 bytes */

    /* }---[ 8 byte boundary ]---{ */
    double ipY; /* insertion point Y: 8 bytes */

    /* }---[ 8 byte boundary ]---{ */
    double skewX;  /* rotation about the X axis: 8 bytes */

    /* }---[ 8 byte boundary ]---{ */
    double skewY;  /* rotation about the Y axis: 8 bytes */

    /* }---[ 8 byte boundary ]--- */
    int32_t srid; /* Spatial reference id: 4 bytes */
    uint16_t width;  /* pixel columns: 2 bytes */
    uint16_t height; /* pixel rows: 2 bytes */
 };

The BANDS
---------

Given the serialized raster header structure above, it
is guaranteed that a serialized band always start at 8-bytes
boundary, so it's simpler to compute padding required at
the end of each band to ensure next band will be guaranteed
the same assumption.

We'll need to take 2 padding spots into account:
the first is to ensure actual band data is aligned accordingly
to the pixel type (and storage flag) needs, the second is to
ensure next band (if any) will also be aligned to 8-bytes:

 [PIXELTYPE+STORAGE_FLAG] [DATA_PADDING] [DATA] [TRAILING_PADDING]

The total size of a band's serialized form in bytes
must be a multiple of 8.

The maximum required data padding size will be of 7 bytes
(64bit pixel type). The maximum required trailing padding size
will be of 7 bytes.

 Pixel type and storage flag
 ---------------------------

 Pixel type specifies type of pixel values in a band.
 Storage flag specifies whether the band data is stored
 as part of the datum or is to be found on the server's
 filesystem.

 There are currently 11 supported pixel value types, so 4
 bits are enough to account for all. We'll reserve
 the upper 4 bits for generic flags and define upmost as
 storage flag:
 
 #define BANDTYPE_FLAGS_MASK 0xF0
 #define BANDTYPE_PIXTYPE_MASK 0x0F

 #define BANDTYPE_FLAG_OFFDB     (1<<7)
 #define BANDTYPE_FLAG_HASNODATA (1<<6)
 #define BANDTYPE_FLAG_ISNODATA  (1<<5)
 #define BANDTYPE_FLAG_RESERVED3 (1<<4)

 Data padding
 ------------

 Band alignment depends on pixeltypes, as follows:

    - PT_1BB, PT_2BUI, PT_4BUI, PT_8BSI, PT_8BUI:
      No alignment required, each value is 1 byte.

    - PT_16BSI, PT_16BUI:
      Data must be aligned to 2-bytes boundary.

    - PT_32BSI, PT_32BUI, PT_32BF:
      Data must be aligned to 4-bytes boundary.

    - PT_64BF:
      Data must be aligned to 8-bytes boundary.
 
 Accordingly we can then define the following structures:

      struct rt_band8_serialized_t {
            uint8_t pixeltype;
            uint8_t data[1]; /* no data padding */
      }

      struct rt_band16_serialized_t {
            uint8_t pixeltype;
            uint8_t padding; /* 1-byte padding */
            uint8_t data[1];
      }

      struct rt_band32_serialized_t {
            uint8_t pixeltype;
            uint8_t padding[3]; /* 3-bytes padding */
            uint8_t data[1];
      }

      struct rt_band64_serialized_t {
            uint8_t pixeltype;
            uint8_t padding[7]; /* 7-bytes padding */
            uint8_t data[1];
      }

 And an abstract base class:

      struct rt_band_serialized_t {
            uint8_t pixeltype
      }

 Data
 ----

 The band data - guaranteed to be always aligned as required by
 pixeltype - will start with the nodata value.
 After that we may have pixel values or off-db raster reference
 depending on OFFDB flag in the pixeltype field:

 * For in-db bands the nodata value is followed by a value
   for each column in first row, then in second row and so on.
   For example, a 2x2 raster band data will have this form:

      [nodata] [x:0,y:0] [x:1,y:0] [x:0,y:1] [x:1,y:1]

   Where the size of the [...] blocks is 1,2,4 or 8 bytes depending
   on pixeltype. Endianness of multi-bytes value is the host endianness.

 * For off-db bands the nodata value is followed by a band number
   followed by a null-terminated string expressing the path to
   the raster file:

      [nodata] [bandno] [path]

   Where the size of the [nodata] block is 1,2,4 or 8 bytes depending
   on pixeltype (endianness of multi-bytes value is the host endianness),
   size of [bandno] is 1 byte, and [path] is null-terminated.
   

 Trailing padding
 ----------------

 The trailing band padding is used to ensure next band (if any)
 will start on the 8-bytes boundary.
 It is both dependent on raster dimensions (number of values)
 and band data pixel type (size of each value).

 In order to obtain the required padding size for a band
 we'll need to compute the minimum size required to hold the band
 data, add the data padding and pixeltype sizes, and then grow
 the resulting size to reach a multiple of 8 bytes:

    size_t 
    rt_band_serialized_size(rt_context ctx, rt_band band)
    {
        rt_pixtype pixtype = rt_band_get_pixtype(ctx, band);
        size_t sz;

        /* pixeltype + data padding */
        sz = rt_pixtype_alignment(ctx, pixtype);

        /* add data size */
        sz += rt_band_get_data_size(ctx, band);

        /* grow size to reach a multiple of 8 bytes */
        sz = TYPEALIGN(sz, 8);

        assert( !(sz%8) );

        return sz;
    }


Example sizes
-------------

255x255 single band PT_16BUI:
    header size:                        64 +
    pixeltype+data_padding:              2 +
    data size:     (255*255+1)*2 == 130052 =
                                    130118 +
    trailing padding:                    2 =
    total size:                     130120 (~127k)

255x255 single band PT_8BUI:
    header size:                        64 +
    pixeltype+data_padding:              1 +
    data size:        (255*255+1) == 65026 =
                                     65091 + 
    trailing padding:                    5 =
    total size:                      65096 (~63k)

64x64 single band PT_16BSI:
    header size:                        64 +
    pixeltype+data_padding:              2 +
    data size:         (64*64+1)*2 == 8194 =
                                      8260 + 
    trailing padding:                    4 =
    total size:                       8264 (~8k -- >page size)

64x64 single band PT_8BUI:
    header size:                        64 +
    pixeltype+data_padding:              1 +
    data size:           (64*64+1) == 4097 =
                                      4162 + 
    trailing padding:                    6 =
    total size:                       4168 (~4k)