Skip to content

Commit 858f44b

Browse files
committed
added customizable error management
1 parent d0c1515 commit 858f44b

File tree

14 files changed

+430
-285
lines changed

14 files changed

+430
-285
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.10.0)
22

3-
project (openjph VERSION 0.1.0 DESCRIPTION "Open source implementation of JPH" LANGUAGES CXX)
3+
project (openjph DESCRIPTION "Open source implementation of JPH" LANGUAGES CXX)
44
# set(CMAKE_CXX_STANDARD 11)
55
if (MSVC)
66
set(CMAKE_CXX_FLAGS "/EHsc")

src/apps/common/ojph_img_io.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ namespace ojph {
8080
ppm_in(mem_fixed_allocator *p = NULL)
8181
{
8282
fh = 0;
83+
fname = NULL;
8384
alloc_p = p;
8485
temp_buf = NULL;
8586
width = height = num_comps = max_val = max_val_num_bits = 0;
@@ -104,7 +105,7 @@ namespace ojph {
104105
void open(const char* filename);
105106
void finalize_alloc();
106107
virtual int read(const line_buf* line, int comp_num);
107-
void close() { if(fh) { fclose(fh); fh = NULL; } }
108+
void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
108109
void set_plannar(bool planar) { this->planar = planar; }
109110

110111
size get_size() { assert(fh); return size(width, height); }
@@ -119,6 +120,7 @@ namespace ojph {
119120

120121
private:
121122
FILE *fh;
123+
const char *fname;
122124
mem_fixed_allocator *alloc_p;
123125
void *temp_buf;
124126
int width, height, num_comps, max_val, max_val_num_bits;
@@ -146,6 +148,7 @@ namespace ojph {
146148
yuv_in()
147149
{
148150
fh = NULL;
151+
fname = NULL;
149152
temp_buf = NULL;
150153
for (int i = 0; i < 3; ++i)
151154
{
@@ -170,7 +173,7 @@ namespace ojph {
170173

171174
void open(const char* filename);
172175
virtual int read(const line_buf* line, int comp_num);
173-
void close() { if(fh) { fclose(fh); fh = NULL; } }
176+
void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
174177

175178
void set_bit_depth(int num_bit_depths, int* bit_depth);
176179
void set_img_props(const size& s, int num_components,
@@ -185,6 +188,7 @@ namespace ojph {
185188

186189
private:
187190
FILE *fh;
191+
const char *fname;
188192
void *temp_buf;
189193
int width[3], height[3], num_com;
190194
int bytes_per_sample[3];
@@ -225,6 +229,7 @@ namespace ojph {
225229
ppm_out()
226230
{
227231
fh = NULL;
232+
fname = NULL;
228233
buffer = NULL;
229234
width = height = num_components = 0;
230235
bit_depth = bytes_per_sample = 0;
@@ -241,10 +246,11 @@ namespace ojph {
241246
void open(char* filename);
242247
void configure(int width, int height, int num_components, int bit_depth);
243248
virtual int write(const line_buf* line, int comp_num);
244-
virtual void close() { if(fh) { fclose(fh); fh = NULL; } }
249+
virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
245250

246251
private:
247252
FILE *fh;
253+
const char *fname;
248254
int width, height, num_components;
249255
int bit_depth, bytes_per_sample;
250256
ui8* buffer;
@@ -266,6 +272,7 @@ namespace ojph {
266272
yuv_out()
267273
{
268274
fh = NULL;
275+
fname = NULL;
269276
width = num_components = 0;
270277
bit_depth = 0;
271278
downsampling = NULL;
@@ -279,10 +286,11 @@ namespace ojph {
279286
void configure(int image_x_extent, int image_x_offset,
280287
int bit_depth, int num_components, point *downsampling);
281288
virtual int write(const line_buf* line, int comp_num);
282-
virtual void close() { if(fh) { fclose(fh); fh = NULL; } }
289+
virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
283290

284291
private:
285292
FILE *fh;
293+
const char *fname;
286294
int width, num_components;
287295
int bit_depth;
288296
point *downsampling;

src/apps/ojph_compress/ojph_compress.cpp

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -543,17 +543,23 @@ int main(int argc, char * argv[]) {
543543
codestream.access_qcd().set_irrev_quant(quantization_step);
544544

545545
if (employ_color_transform != -1)
546-
printf("-colour_trans option is not needed and was not used\n");
546+
OJPH_WARN(0x01000001,
547+
"-colour_trans option is not needed and was not used\n");
547548
if (dims.w != -1 || dims.h != -1)
548-
printf("-dims option is not needed and was not used\n");
549+
OJPH_WARN(0x01000002,
550+
"-dims option is not needed and was not used\n");
549551
if (num_components != -1 )
550-
printf("-num_comps is not needed and was not used\n");
552+
OJPH_WARN(0x01000003,
553+
"-num_comps is not needed and was not used\n");
551554
if (is_signed[0] != -1)
552-
printf("-signed is not needed and was not used\n");
555+
OJPH_WARN(0x01000004,
556+
"-signed is not needed and was not used\n");
553557
if (bit_depth[0] != -1)
554-
printf("-bit_depth is not needed and was not used\n");
558+
OJPH_WARN(0x01000005,
559+
"-bit_depth is not needed and was not used\n");
555560
if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
556-
printf("-downsamp is not needed and was not used\n");
561+
OJPH_WARN(0x01000006,
562+
"-downsamp is not needed and was not used\n");
557563

558564
base = &ppm;
559565
}
@@ -589,33 +595,43 @@ int main(int argc, char * argv[]) {
589595
codestream.set_planar(false);
590596

591597
if (dims.w != -1 || dims.h != -1)
592-
printf("-dims option is not needed and was not used\n");
598+
OJPH_WARN(0x01000011,
599+
"-dims option is not needed and was not used\n");
593600
if (num_components != -1)
594-
printf("-num_comps is not needed and was not used\n");
601+
OJPH_WARN(0x01000012,
602+
"-num_comps is not needed and was not used\n");
595603
if (is_signed[0] != -1)
596-
printf("-signed is not needed and was not used\n");
604+
OJPH_WARN(0x01000013,
605+
"-signed is not needed and was not used\n");
597606
if (bit_depth[0] != -1)
598-
printf("-bit_depth is not needed and was not used\n");
607+
OJPH_WARN(0x01000014,
608+
"-bit_depth is not needed and was not used\n");
599609
if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
600-
printf("-downsamp is not needed and was not used\n");
610+
OJPH_WARN(0x01000015,
611+
"-downsamp is not needed and was not used\n");
601612

602613
base = &ppm;
603614
}
604615
else if (strncmp(".yuv", v, 4) == 0)
605616
{
606617
ojph::param_siz_t siz = codestream.access_siz();
607618
if (dims.w < 0 || dims.h < 0)
608-
printf("-dims option is missing, and need to be provided\n");
619+
OJPH_ERROR(0x01000021,
620+
"-dims option is missing, and need to be provided\n");
609621
siz.set_image_extent(ojph::point(image_offset.x + dims.w,
610622
image_offset.y + dims.h));
611623
if (num_components <= 0)
612-
printf("-num_comps option is missing and must be provided\n");
624+
OJPH_ERROR(0x01000022,
625+
"-num_comps option is missing and must be provided\n");
613626
if (num_is_signed <= 0)
614-
printf("-signed option is missing and must be provided\n");
627+
OJPH_ERROR(0x01000023,
628+
"-signed option is missing and must be provided\n");
615629
if (num_bit_depths <= 0)
616-
printf("-bit_depth option is missing and must be provided\n");
630+
OJPH_ERROR(0x01000024,
631+
"-bit_depth option is missing and must be provided\n");
617632
if (num_comp_downsamps <= 0)
618-
printf("-downsamp option is missing and must be provided\n");
633+
OJPH_ERROR(0x01000025,
634+
"-downsamp option is missing and must be provided\n");
619635

620636
yuv.set_img_props(dims, num_components, num_comp_downsamps,
621637
comp_downsampling);
@@ -647,12 +663,13 @@ int main(int argc, char * argv[]) {
647663
if (employ_color_transform == -1)
648664
cod.set_color_transform(false);
649665
else
650-
throw "we currently do not support color transform on yuv files."
651-
" In any case, this not a normal usage scenario. The OpenJPH "
652-
"library however does support that, but ojph_compress.cpp must be "
653-
"modifed to send all lines from one component before moving to "
654-
"the next component; this requires buffering components outside of "
655-
"the OpenJPH library";
666+
OJPH_ERROR(0x01000031,
667+
"we currently do not support color transform on yuv files."
668+
" In any case, this not a normal usage scenario. The OpenJPH "
669+
"library however does support that, but ojph_compress.cpp must be "
670+
"modifed to send all lines from one component before moving to "
671+
"the next component; this requires buffering components outside"
672+
" of the OpenJPH library");
656673
cod.set_reversible(reversible);
657674
if (!reversible && quantization_step != -1)
658675
codestream.access_qcd().set_irrev_quant(quantization_step);
@@ -662,19 +679,15 @@ int main(int argc, char * argv[]) {
662679
base = &yuv;
663680
}
664681
else
665-
{
666-
printf("unknown input file extension; only (pgm, ppm, and yuv) are"
682+
OJPH_ERROR(0x01000041,
683+
"unknown input file extension; only (pgm, ppm, and yuv) are"
667684
" suppoted\n");
668-
exit(-1);
669-
}
670685

671686
}
672687
else
673-
{
674-
printf("Please supply a proper input filename with a proper three-letter"
675-
" extension\n");
676-
exit(-1);
677-
}
688+
OJPH_ERROR(0x01000051,
689+
"Please supply a proper input filename with a proper three-letter "
690+
"extension\n");
678691

679692
ojph::j2c_outfile j2c_file;
680693
j2c_file.open(output_filename);
@@ -724,11 +737,6 @@ int main(int argc, char * argv[]) {
724737
delete[] is_signed;
725738
}
726739
}
727-
catch (const char *e)
728-
{
729-
printf("%s\n", e);
730-
exit (-1);
731-
}
732740
catch (const std::exception& e)
733741
{
734742
const char *p = e.what();

src/apps/ojph_expand/ojph_expand.cpp

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "ojph_file.h"
4646
#include "ojph_codestream.h"
4747
#include "ojph_params.h"
48+
#include "ojph_message.h"
4849

4950
//////////////////////////////////////////////////////////////////////////////
5051
bool get_arguments(int argc, char *argv[],
@@ -115,11 +116,9 @@ int main(int argc, char *argv[]) {
115116
{
116117

117118
if (siz.get_num_components() != 1)
118-
{
119-
printf("The file has more than one color component, but .pgm can "
119+
OJPH_ERROR(0x020000001,
120+
"The file has more than one color component, but .pgm can "
120121
"contain only on color component\n");
121-
exit(-1);
122-
}
123122
ppm.configure(siz.get_image_extent().x - siz.get_image_offset().x,
124123
siz.get_image_extent().y - siz.get_image_offset().y,
125124
siz.get_num_components(), siz.get_bit_depth(0));
@@ -132,11 +131,9 @@ int main(int argc, char *argv[]) {
132131
ojph::param_siz_t siz = codestream.access_siz();
133132

134133
if (siz.get_num_components() != 3)
135-
{
136-
printf("The file has %d color components; this cannot be saved to"
137-
" a .ppm file\n", siz.get_num_components());
138-
exit(-1);
139-
}
134+
OJPH_ERROR(0x020000002,
135+
"The file has %d color components; this cannot be saved to"
136+
" a .ppm file\n", siz.get_num_components());
140137
bool all_same = true;
141138
ojph::point p = siz.get_downsampling(0);
142139
for (int i = 1; i < siz.get_num_components(); ++i)
@@ -145,11 +142,9 @@ int main(int argc, char *argv[]) {
145142
all_same = all_same && (p1.x == p.x) && (p1.y == p.y);
146143
}
147144
if (!all_same)
148-
{
149-
printf("To save an image to ppm, all the components must have the "
150-
"downsampling ratio\n");
151-
exit(-1);
152-
}
145+
OJPH_ERROR(0x020000003,
146+
"To save an image to ppm, all the components must have the "
147+
"downsampling ratio\n");
153148
ppm.configure(siz.get_image_extent().x - siz.get_image_offset().x,
154149
siz.get_image_extent().y - siz.get_image_offset().y,
155150
siz.get_num_components(), siz.get_bit_depth(0));
@@ -162,19 +157,15 @@ int main(int argc, char *argv[]) {
162157
ojph::param_siz_t siz = codestream.access_siz();
163158

164159
if (siz.get_num_components() != 3 && siz.get_num_components() != 1)
165-
{
166-
printf("The file has %d color components; this cannot be saved to"
167-
" a .yuv file\n", siz.get_num_components());
168-
exit(-1);
169-
}
160+
OJPH_ERROR(0x020000004,
161+
"The file has %d color components; this cannot be saved to"
162+
" a .yuv file\n", siz.get_num_components());
170163
ojph::param_cod_t cod = codestream.access_cod();
171164
if (cod.is_using_color_transform())
172-
{
173-
printf("The current implementation of yuv file object does not "
165+
OJPH_ERROR(0x020000005,
166+
"The current implementation of yuv file object does not "
174167
"support saving a file when conversion from yuv to rgb is needed; "
175168
"In any case, this is not the normal usage of a yuv file");
176-
exit(-1);
177-
}
178169
ojph::point points[3];
179170
int max_bit_depth = 0;
180171
for (int i = 0; i < siz.get_num_components(); ++i)
@@ -189,18 +180,14 @@ int main(int argc, char *argv[]) {
189180
base = &yuv;
190181
}
191182
else
192-
{
193-
printf("unknown output file extension; only (pgm, ppm, and yuv) are"
183+
OJPH_ERROR(0x020000006,
184+
"unknown output file extension; only (pgm, ppm, and yuv) are"
194185
" suppoted\n");
195-
exit(-1);
196-
}
197186
}
198187
else
199-
{
200-
printf("Please supply a proper output filename with a proper three-letter"
201-
" extension\n");
202-
exit(-1);
203-
}
188+
OJPH_ERROR(0x020000007,
189+
"Please supply a proper output filename with a proper three-letter"
190+
" extension\n");
204191

205192
codestream.create();
206193

@@ -240,12 +227,14 @@ int main(int argc, char *argv[]) {
240227
base->close();
241228
codestream.close();
242229
}
243-
catch (const char *e)
230+
catch (const std::exception& e)
244231
{
245-
printf("%s\n", e);
246-
exit (-1);
232+
const char *p = e.what();
233+
if (strncmp(p, "ojph error", 10) != 0)
234+
printf("%s\n", p);
235+
exit(-1);
247236
}
248-
237+
249238
clock_t end = clock();
250239
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
251240
printf("Elapsed time = %f\n", elapsed_secs);

0 commit comments

Comments
 (0)