@@ -27,154 +27,160 @@ const std::string dataRoot = GRK_DATA_ROOT;
2727
2828int main ([[maybe_unused]] int argc, [[maybe_unused]] char ** argv)
2929{
30- const uint32_t dimX = 640 ;
31- const uint32_t dimY = 480 ;
32- const uint32_t numComps = 3 ;
33- const uint32_t precision = 8 ;
34- grk_image_comp* compParams = nullptr ;
35- grk_image* image = nullptr ;
36- grk_stream_params streamParams;
37- grk_set_default_stream_params (&streamParams);
38-
39- bool inputFromImage = true ;
40- bool outputToBuffer = true ;
41-
42- if (outputToBuffer)
43- {
44- streamParams.buf_len = (size_t )numComps * (precision / 8 ) * dimX * dimY;
45- streamParams.buf = new uint8_t [streamParams.buf_len ];
46- }
47-
48- std::vector<std::string> argString;
49- std::vector<char *> args;
50-
51- std::string inputFile, outputFile;
5230 int rc = 1 ;
53-
54- if (inputFromImage )
31+ // perform two identical compressions
32+ for ( uint32_t i = 0 ; i < 2 ; ++i )
5533 {
56- // create blank image
57- compParams = new grk_image_comp[numComps];
58- for (uint32_t i = 0 ; i < numComps; ++i)
34+ const uint32_t dimX = 640 ;
35+ const uint32_t dimY = 480 ;
36+ const uint32_t numComps = 3 ;
37+ const uint32_t precision = 8 ;
38+ grk_image_comp* compParams = nullptr ;
39+ grk_image* image = nullptr ;
40+ grk_stream_params streamParams;
41+ grk_set_default_stream_params (&streamParams);
42+ bool inputFromImage = true ;
43+ bool outputToBuffer = true ;
44+ if (outputToBuffer)
5945 {
60- auto c = compParams + i;
61- c->w = dimX;
62- c->h = dimY;
63- c->dx = 1 ;
64- c->dy = 1 ;
65- c->prec = precision;
66- c->sgnd = false ;
46+ streamParams.buf_len = (size_t )numComps * (precision / 8 ) * dimX * dimY;
47+ streamParams.buf = new uint8_t [streamParams.buf_len ];
6748 }
68- image = grk_image_new (numComps, compParams, GRK_CLRSPC_SRGB, true );
49+ std::vector<std::string> argString;
50+ std::vector<char *> args;
51+ std::string inputFile, outputFile;
6952
70- // fill in component data
71- // see grok.h header for full details of image structure
72- for (uint16_t compno = 0 ; compno < image->numcomps ; ++compno)
53+ if (inputFromImage)
7354 {
74- auto comp = image->comps + compno;
75- auto compWidth = comp->w ;
76- auto compHeight = comp->h ;
77- auto compData = comp->data ;
78- if (!compData)
55+ // create blank image
56+ compParams = new grk_image_comp[numComps];
57+ for (uint32_t i = 0 ; i < numComps; ++i)
7958 {
80- fprintf (stderr, " Image has null data for component %d\n " , compno);
81- goto beach;
59+ auto c = compParams + i;
60+ memset (c, 0 , sizeof (grk_image_comp));
61+ c->w = dimX;
62+ c->h = dimY;
63+ c->dx = 1 ;
64+ c->dy = 1 ;
65+ c->prec = precision;
66+ c->sgnd = false ;
8267 }
83- // fill in component data, taking component stride into account
84- auto srcData = new int32_t [compWidth * compHeight];
85- auto srcPtr = srcData;
86- for (uint32_t j = 0 ; j < compHeight; ++j)
68+ image = grk_image_new (numComps, compParams, GRK_CLRSPC_SRGB, true );
69+
70+ // fill in component data
71+ // see grok.h header for full details of image structure
72+ for (uint16_t compno = 0 ; compno < image->numcomps ; ++compno)
8773 {
88- memcpy (compData, srcPtr, compWidth * sizeof (int32_t ));
89- srcPtr += compWidth;
90- compData += comp->stride ;
74+ auto comp = image->comps + compno;
75+ auto compWidth = comp->w ;
76+ auto compHeight = comp->h ;
77+ auto compData = comp->data ;
78+ if (!compData)
79+ {
80+ fprintf (stderr, " Image has null data for component %d\n " , compno);
81+ goto beach;
82+ }
83+ // fill in component data, taking component stride into account
84+ // Note: in this example, we just fill the buffer with a constant value whose precision
85+ // matches the precision specified above.
86+ // !! do not pass in data whose precision exceeds the precision specified above
87+ auto srcData = new int32_t [compWidth * compHeight];
88+ for (uint32_t i = 0 ; i < compWidth * compHeight; ++i)
89+ srcData[i] = 0xFF ;
90+ auto srcPtr = srcData;
91+ for (uint32_t j = 0 ; j < compHeight; ++j)
92+ {
93+ memcpy (compData, srcPtr, compWidth * sizeof (int32_t ));
94+ srcPtr += compWidth;
95+ compData += comp->stride ;
96+ }
97+ delete[] srcData;
9198 }
92- delete[] srcData;
9399 }
94- }
95100
96- // 1. form vector of command line args
101+ // 1. form vector of command line args
97102
98- // first entry must always be the name of the program, as is
99- // required by argv/argc variables in main method
100- argString.push_back (" codec_compress" );
103+ // first entry must always be the name of the program, as is
104+ // required by argv/argc variables in main method
105+ argString.push_back (" codec_compress" );
101106
102- // verbose output
103- argString.push_back (" -v" );
107+ // verbose output
108+ argString.push_back (" -v" );
104109
105- // a file can be passed in as a command line argument
106- // example:
107- // $ codec_compress foo.tif
108- // otherwise a file from the Grok test suite, specified below, will be used.
110+ // a file can be passed in as a command line argument
111+ // example:
112+ // $ codec_compress foo.tif
113+ // otherwise a file from the Grok test suite, specified below, will be used.
109114
110- inputFile = dataRoot + std::filesystem::path::preferred_separator + " input" +
111- std::filesystem::path::preferred_separator + " nonregression" +
112- std::filesystem::path::preferred_separator + " basn6a08.tif" ;
113- outputFile = " basn6a08.jp2" ;
114- if (argc > 1 )
115- {
116- inputFile = argv[1 ];
117- outputFile = inputFile + " .tif" ;
118- }
119- if (!inputFromImage)
120- {
121- argString.push_back (" -i " + inputFile);
122- }
115+ inputFile = dataRoot + std::filesystem::path::preferred_separator + " input" +
116+ std::filesystem::path::preferred_separator + " nonregression" +
117+ std::filesystem::path::preferred_separator + " basn6a08.tif" ;
118+ outputFile = " basn6a08.jp2" ;
119+ if (argc > 1 )
120+ {
121+ inputFile = argv[1 ];
122+ outputFile = inputFile + " .tif" ;
123+ }
124+ if (!inputFromImage)
125+ {
126+ argString.push_back (" -i " + inputFile);
127+ }
123128
124- if (outputToBuffer)
125- {
126- argString.push_back (" -out_fmt jp2" );
127- }
128- else
129- {
130- argString.push_back (" -o " + outputFile);
131- }
129+ if (outputToBuffer)
130+ {
131+ argString.push_back (" -out_fmt jp2" );
132+ }
133+ else
134+ {
135+ argString.push_back (" -o " + outputFile);
136+ }
132137
133- // 2. convert to array of C strings
134- for (auto & s : argString)
135- {
136- char * arg = new char [s.size () + 1 ];
137- copy (s.begin (), s.end (), arg);
138- arg[s.size ()] = ' \0 ' ;
139- args.push_back (arg);
140- }
138+ // 2. convert to array of C strings
139+ for (auto & s : argString)
140+ {
141+ char * arg = new char [s.size () + 1 ];
142+ copy (s.begin (), s.end (), arg);
143+ arg[s.size ()] = ' \0 ' ;
144+ args.push_back (arg);
145+ }
141146
142- // 3. decompress
143- rc = grk_codec_compress ((int )args.size (), &args[0 ], image,
144- outputToBuffer ? &streamParams : nullptr );
145- if (rc)
146- fprintf (stderr, " Failed to compress\n " );
147+ // 3. decompress
148+ rc = grk_codec_compress ((int )args.size (), &args[0 ], image,
149+ outputToBuffer ? &streamParams : nullptr );
150+ if (rc)
151+ fprintf (stderr, " Failed to compress\n " );
147152
148- if (outputToBuffer)
149- printf (" Compressed to memory : %ld bytes\n " , streamParams.buf_compressed_len );
153+ if (outputToBuffer)
154+ printf (" Compressed to memory : %ld bytes\n " , streamParams.buf_compressed_len );
150155
151- // write buffer to file
152- if (outputToBuffer)
153- {
154- auto fp = fopen (outputFile.c_str (), " wb" );
155- if (!fp)
156- {
157- fprintf (stderr, " Buffer compress: failed to open file %s for writing" , outputFile.c_str ());
158- }
159- else
156+ // write buffer to file
157+ if (outputToBuffer)
160158 {
161- size_t written = fwrite (streamParams. buf , 1 , streamParams. buf_compressed_len , fp );
162- if (written != streamParams. buf_compressed_len )
159+ auto fp = fopen (outputFile. c_str (), " wb " );
160+ if (!fp )
163161 {
164- fprintf (stderr, " Buffer compress: only %ld bytes written out of %ld total" ,
165- streamParams.buf_compressed_len , written);
162+ fprintf (stderr, " Buffer compress: failed to open file %s for writing" , outputFile.c_str ());
163+ }
164+ else
165+ {
166+ size_t written = fwrite (streamParams.buf , 1 , streamParams.buf_compressed_len , fp);
167+ if (written != streamParams.buf_compressed_len )
168+ {
169+ fprintf (stderr, " Buffer compress: only %ld bytes written out of %ld total" ,
170+ streamParams.buf_compressed_len , written);
171+ }
172+ fclose (fp);
166173 }
167- fclose (fp);
168174 }
169- }
170175
171- beach:
172- // 4. cleanup
173- for (auto & s : args)
174- delete[] s;
175- delete[] compParams;
176- delete[] streamParams.buf ;
177- grk_object_unref (&image->obj );
176+ beach:
177+ // 4. cleanup
178+ for (auto & s : args)
179+ delete[] s;
180+ delete[] compParams;
181+ delete[] streamParams.buf ;
182+ grk_object_unref (&image->obj );
183+ }
178184
179185 return rc;
180186}
0 commit comments