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