File: tst_jpeg.c

package info (click to toggle)
g2clib 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,524 kB
  • sloc: ansic: 28,287; python: 76; sh: 46; makefile: 26
file content (276 lines) | stat: -rw-r--r-- 9,557 bytes parent folder | download
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
275
276
/* This is a test for the NCEPLIBS-g2c project. This test is for
 * the JPEG compression functions.
 *
 * Ed Hartnett 11/1/21
 */

#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>

#define DATA_LEN 4
#define PACKED_LEN 200
#define EPSILON .001
#define EPSILON_BIG .1

int
main()
{
    int i;

    printf("Testing JPEG functions.\n");
    /* g2c_set_log_level(10); */
    printf("Testing enc_jpeg2000()/dec_jpeg2000() call...");
    {
        unsigned char data[DATA_LEN] = {1, 2, 3, 4};
        g2int width = 2, height = 2, nbits = 4;
        g2int ltype = 0, ratio = 0, retry = 0, jpclen = PACKED_LEN;
        char outjpc[PACKED_LEN];
        g2int outfld[DATA_LEN];
        int ret;

        /* Encode some data. */
        if ((ret = enc_jpeg2000(data, width, height, nbits, ltype,
                                ratio, retry, outjpc, jpclen)) < 0)
            return G2C_ERROR;

        /* Now decode it. */
        if ((ret = dec_jpeg2000(outjpc, jpclen, outfld)))
            return G2C_ERROR;

        for (i = 0; i < DATA_LEN; i++)
        {
            if (outfld[i] != data[i])
                return G2C_ERROR;
        }
    }
    printf("ok!\n");
    printf("Testing g2c_enc_jpeg2000()/g2c_dec_jpeg2000() call...");
    {
        unsigned char data[DATA_LEN] = {1, 2, 3, 4};
        int width = 2, height = 2, nbits = 4;
        int ltype = 0, ratio = 0, retry = 0;
        size_t jpclen = PACKED_LEN;
        char outjpc[PACKED_LEN];
        int outfld[DATA_LEN];
        int ret;

        /* Encode some data. */
        if ((ret = g2c_enc_jpeg2000(data, width, height, nbits, ltype,
                                    ratio, retry, outjpc, jpclen)) < 0)
            return G2C_ERROR;

        /* Now decode it. */
        if ((ret = g2c_dec_jpeg2000(outjpc, jpclen, outfld)))
            return G2C_ERROR;

        for (i = 0; i < DATA_LEN; i++)
        {
            if (outfld[i] != data[i])
                return G2C_ERROR;
        }
    }
    printf("ok!\n");
    {
        g2int height = 2, width = 2;
        g2int len = PACKED_LEN, ndpts = DATA_LEN;
        unsigned char cpack[PACKED_LEN];

        printf("Testing jpcpack()/jpcunpack() call...");
        {
            float fld[DATA_LEN] = {100.0, 200.0, 300.0, 0.0};
            float fld_in[DATA_LEN];
            g2int lcpack = PACKED_LEN;
            g2int idrstmpl[7] = {0, 1, 1, 16, 0, 0, 0};
            int i;

            /* Pack the data. */
            jpcpack(fld, width, height, idrstmpl, cpack, &lcpack);

            /* Unpack the data. */
            if (jpcunpack(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
            {
                /* printf("%g %g\n", fld[i], fld_in[i]); */
                if (fld[i] != fld_in[i])
                    return G2C_ERROR;
            }
        }
        printf("ok!\n");
        printf("Testing g2c_jpcpackd()/g2c_jpcunpackd() call...");
        {
            double fld[DATA_LEN] = {10000.0, 20000.0, 30000.0, 0.0};
            double fld_in[DATA_LEN];
            size_t lcpack_st = PACKED_LEN;
            int idrstmpl[7] = {0, 1, 1, 16, 0, 0, 0};

            /* Pack the data. */
            g2c_jpcpackd(fld, width, height, idrstmpl, cpack, &lcpack_st);

            /* Unpack the data. */
            if (g2c_jpcunpackd(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
            {
                /* printf("%g %g\n", fld[i], fld_in[i]); */
                if (fld[i] != fld_in[i])
                    return G2C_ERROR;
            }
        }
        printf("ok!\n");
        printf("Testing g2c_jpcpackf()/g2c_jpcunpackf() call...");
        {
            float fld[DATA_LEN] = {1000.0, 2000.0, 3000.0, 0.0};
            float fld_in[DATA_LEN];
            size_t lcpack_st = PACKED_LEN;
            int idrstmpl[7] = {0, 1, 1, 16, 0, 0, 0};

            /* Pack the data. */
            g2c_jpcpackf(fld, width, height, idrstmpl, cpack, &lcpack_st);

            /* Unpack the data. */
            if (g2c_jpcunpackf(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
            {
                /* printf("%g %g\n", fld[i], fld_in[i]); */
                if (fld[i] != fld_in[i])
                    return G2C_ERROR;
            }
        }
        printf("ok!\n");
        printf("Testing jpcpack()/jpcunpack() call with different drstmpl values...");
        {
            float fld[DATA_LEN] = {1.0, 2.0, 3.0, 0.0};
            float fld_in[DATA_LEN];
            g2int lcpack = PACKED_LEN;
            /* See
             * https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-40.shtml */
            g2int idrstmpl[7] = {
                0,  /* Reference value (R) (IEEE 32-bit floating-point value) */
                0,  /* Binary scale factor (E) */
                1,  /* Decimal scale factor (D) */
                32, /* Number of bits required to hold the resulting scaled and referenced data values. (i.e. The depth of the grayscale image.) (see Note 2) */
                0,  /* Type of original field values (see Code Table 5.1) */
                0,  /* Type of Compression used. (see Code Table 5.40) */
                1   /* Target compression ratio, M:1 (with respect to the bit-depth specified in octet 20), when octet 22 indicates Lossy Compression. Otherwise, set to missing (see Note 3) */
            };

            /* Pack the data. */
            jpcpack(fld, width, height, idrstmpl, cpack, &lcpack);

            /* Unpack the data. */
            if (jpcunpack(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
                if (fld[i] != fld_in[i])
                    return G2C_ERROR;
        }
        printf("ok!\n");
        printf("Testing g2c_jpcpackd()/g2c_jpcunpackd() call with different drstmpl values...");
        {
            double fld[DATA_LEN] = {1.0, 2.0, 3.0, 0.0};
            double fld_in[DATA_LEN];
            size_t lcpack = PACKED_LEN;
            int idrstmpl[7] = {0, 0, 1, 32, 0, 0, 1};

            /* Pack the data. */
            g2c_jpcpackd(fld, width, height, idrstmpl, cpack, &lcpack);

            /* Unpack the data. */
            if (g2c_jpcunpackd(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
                if (fld[i] != fld_in[i])
                    return G2C_ERROR;
        }
        printf("ok!\n");
        printf("Testing g2c_jpcpackd()/g2c_jpcunpackd() call with drstmpl values as in NCEPLIBS-g2 test_jpcpack...");
        {
            double fld[DATA_LEN] = {1.1, 2.2, 3.3, 4.4};
            double fld_in[DATA_LEN];
            size_t lcpack = PACKED_LEN;
            int idrstmpl[7] = {0, 1, 1, 32, 0, 0, 1};

            /* Pack the data. */
            g2c_jpcpackd(fld, width, height, idrstmpl, cpack, &lcpack);

            /* Unpack the data. */
            if (g2c_jpcunpackd(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
                if (abs(fld[i] - fld_in[i]) > EPSILON)
                    return G2C_ERROR;
        }
        printf("ok!\n");
        printf("Testing jpcpack()/jpcunpack() call with drstmpl values as in NCEPLIBS-g2 test_jpcpack...");
        {
            float fld[DATA_LEN] = {1.1, 2.2, 3.3, 4.4};
            float fld_in[DATA_LEN];
            g2int lcpack = PACKED_LEN;
            g2int idrstmpl[7] = {0, 1, 1, 32, 0, 0, 1};

            /* Pack the data. */
            jpcpack(fld, width, height, idrstmpl, cpack, &lcpack);

            /* Unpack the data. */
            if (jpcunpack(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
            {
                /* printf("%d %g %g\n", i, fld[i], fld_in[i]); */
                if (abs(fld[i] - fld_in[i]) > EPSILON_BIG)
                    return G2C_ERROR;
            }
        }
        printf("ok!\n");
        printf("Testing jpcpack()/jpcunpack() call with constant data field...");
        {
            float fld[DATA_LEN] = {1.0, 1.0, 1.0, 1.0};
            float fld_in[DATA_LEN];
            g2int lcpack = PACKED_LEN;
            g2int idrstmpl[7] = {0, 0, 1, 32, 0, 0, 1};

            /* Pack the data. */
            jpcpack(fld, width, height, idrstmpl, cpack, &lcpack);

            /* Unpack the data. */
            if (jpcunpack(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
                if (fld[i] != fld_in[i])
                    return G2C_ERROR;
        }
        printf("ok!\n");
        printf("Testing g2c_jpcpackd()/g2c_jpcunpackd() call with constant data field...");
        {
            double fld[DATA_LEN] = {1.0, 1.0, 1.0, 1.0};
            double fld_in[DATA_LEN];
            size_t lcpack = PACKED_LEN;
            int idrstmpl[7] = {0, 0, 1, 32, 0, 0, 1};

            /* Pack the data. */
            g2c_jpcpackd(fld, width, height, idrstmpl, cpack, &lcpack);

            /* Unpack the data. */
            if (g2c_jpcunpackd(cpack, len, idrstmpl, ndpts, fld_in))
                return G2C_ERROR;

            for (i = 0; i < DATA_LEN; i++)
                if (fld[i] != fld_in[i])
                    return G2C_ERROR;
        }
        printf("ok!\n");
    }
    printf("SUCCESS!\n");
    return 0;
}