Skip to content

Commit

Permalink
added customizable error management
Browse files Browse the repository at this point in the history
  • Loading branch information
aous72 committed Oct 10, 2019
1 parent d0c1515 commit 858f44b
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 285 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10.0)

project (openjph VERSION 0.1.0 DESCRIPTION "Open source implementation of JPH" LANGUAGES CXX)
project (openjph DESCRIPTION "Open source implementation of JPH" LANGUAGES CXX)
# set(CMAKE_CXX_STANDARD 11)
if (MSVC)
set(CMAKE_CXX_FLAGS "/EHsc")
Expand Down
Binary file not shown.
16 changes: 12 additions & 4 deletions src/apps/common/ojph_img_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace ojph {
ppm_in(mem_fixed_allocator *p = NULL)
{
fh = 0;
fname = NULL;
alloc_p = p;
temp_buf = NULL;
width = height = num_comps = max_val = max_val_num_bits = 0;
Expand All @@ -104,7 +105,7 @@ namespace ojph {
void open(const char* filename);
void finalize_alloc();
virtual int read(const line_buf* line, int comp_num);
void close() { if(fh) { fclose(fh); fh = NULL; } }
void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
void set_plannar(bool planar) { this->planar = planar; }

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

private:
FILE *fh;
const char *fname;
mem_fixed_allocator *alloc_p;
void *temp_buf;
int width, height, num_comps, max_val, max_val_num_bits;
Expand Down Expand Up @@ -146,6 +148,7 @@ namespace ojph {
yuv_in()
{
fh = NULL;
fname = NULL;
temp_buf = NULL;
for (int i = 0; i < 3; ++i)
{
Expand All @@ -170,7 +173,7 @@ namespace ojph {

void open(const char* filename);
virtual int read(const line_buf* line, int comp_num);
void close() { if(fh) { fclose(fh); fh = NULL; } }
void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }

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

private:
FILE *fh;
const char *fname;
void *temp_buf;
int width[3], height[3], num_com;
int bytes_per_sample[3];
Expand Down Expand Up @@ -225,6 +229,7 @@ namespace ojph {
ppm_out()
{
fh = NULL;
fname = NULL;
buffer = NULL;
width = height = num_components = 0;
bit_depth = bytes_per_sample = 0;
Expand All @@ -241,10 +246,11 @@ namespace ojph {
void open(char* filename);
void configure(int width, int height, int num_components, int bit_depth);
virtual int write(const line_buf* line, int comp_num);
virtual void close() { if(fh) { fclose(fh); fh = NULL; } }
virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }

private:
FILE *fh;
const char *fname;
int width, height, num_components;
int bit_depth, bytes_per_sample;
ui8* buffer;
Expand All @@ -266,6 +272,7 @@ namespace ojph {
yuv_out()
{
fh = NULL;
fname = NULL;
width = num_components = 0;
bit_depth = 0;
downsampling = NULL;
Expand All @@ -279,10 +286,11 @@ namespace ojph {
void configure(int image_x_extent, int image_x_offset,
int bit_depth, int num_components, point *downsampling);
virtual int write(const line_buf* line, int comp_num);
virtual void close() { if(fh) { fclose(fh); fh = NULL; } }
virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }

private:
FILE *fh;
const char *fname;
int width, num_components;
int bit_depth;
point *downsampling;
Expand Down
80 changes: 44 additions & 36 deletions src/apps/ojph_compress/ojph_compress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,23 @@ int main(int argc, char * argv[]) {
codestream.access_qcd().set_irrev_quant(quantization_step);

if (employ_color_transform != -1)
printf("-colour_trans option is not needed and was not used\n");
OJPH_WARN(0x01000001,
"-colour_trans option is not needed and was not used\n");
if (dims.w != -1 || dims.h != -1)
printf("-dims option is not needed and was not used\n");
OJPH_WARN(0x01000002,
"-dims option is not needed and was not used\n");
if (num_components != -1 )
printf("-num_comps is not needed and was not used\n");
OJPH_WARN(0x01000003,
"-num_comps is not needed and was not used\n");
if (is_signed[0] != -1)
printf("-signed is not needed and was not used\n");
OJPH_WARN(0x01000004,
"-signed is not needed and was not used\n");
if (bit_depth[0] != -1)
printf("-bit_depth is not needed and was not used\n");
OJPH_WARN(0x01000005,
"-bit_depth is not needed and was not used\n");
if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
printf("-downsamp is not needed and was not used\n");
OJPH_WARN(0x01000006,
"-downsamp is not needed and was not used\n");

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

if (dims.w != -1 || dims.h != -1)
printf("-dims option is not needed and was not used\n");
OJPH_WARN(0x01000011,
"-dims option is not needed and was not used\n");
if (num_components != -1)
printf("-num_comps is not needed and was not used\n");
OJPH_WARN(0x01000012,
"-num_comps is not needed and was not used\n");
if (is_signed[0] != -1)
printf("-signed is not needed and was not used\n");
OJPH_WARN(0x01000013,
"-signed is not needed and was not used\n");
if (bit_depth[0] != -1)
printf("-bit_depth is not needed and was not used\n");
OJPH_WARN(0x01000014,
"-bit_depth is not needed and was not used\n");
if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
printf("-downsamp is not needed and was not used\n");
OJPH_WARN(0x01000015,
"-downsamp is not needed and was not used\n");

base = &ppm;
}
else if (strncmp(".yuv", v, 4) == 0)
{
ojph::param_siz_t siz = codestream.access_siz();
if (dims.w < 0 || dims.h < 0)
printf("-dims option is missing, and need to be provided\n");
OJPH_ERROR(0x01000021,
"-dims option is missing, and need to be provided\n");
siz.set_image_extent(ojph::point(image_offset.x + dims.w,
image_offset.y + dims.h));
if (num_components <= 0)
printf("-num_comps option is missing and must be provided\n");
OJPH_ERROR(0x01000022,
"-num_comps option is missing and must be provided\n");
if (num_is_signed <= 0)
printf("-signed option is missing and must be provided\n");
OJPH_ERROR(0x01000023,
"-signed option is missing and must be provided\n");
if (num_bit_depths <= 0)
printf("-bit_depth option is missing and must be provided\n");
OJPH_ERROR(0x01000024,
"-bit_depth option is missing and must be provided\n");
if (num_comp_downsamps <= 0)
printf("-downsamp option is missing and must be provided\n");
OJPH_ERROR(0x01000025,
"-downsamp option is missing and must be provided\n");

yuv.set_img_props(dims, num_components, num_comp_downsamps,
comp_downsampling);
Expand Down Expand Up @@ -647,12 +663,13 @@ int main(int argc, char * argv[]) {
if (employ_color_transform == -1)
cod.set_color_transform(false);
else
throw "we currently do not support color transform on yuv files."
" In any case, this not a normal usage scenario. The OpenJPH "
"library however does support that, but ojph_compress.cpp must be "
"modifed to send all lines from one component before moving to "
"the next component; this requires buffering components outside of "
"the OpenJPH library";
OJPH_ERROR(0x01000031,
"we currently do not support color transform on yuv files."
" In any case, this not a normal usage scenario. The OpenJPH "
"library however does support that, but ojph_compress.cpp must be "
"modifed to send all lines from one component before moving to "
"the next component; this requires buffering components outside"
" of the OpenJPH library");
cod.set_reversible(reversible);
if (!reversible && quantization_step != -1)
codestream.access_qcd().set_irrev_quant(quantization_step);
Expand All @@ -662,19 +679,15 @@ int main(int argc, char * argv[]) {
base = &yuv;
}
else
{
printf("unknown input file extension; only (pgm, ppm, and yuv) are"
OJPH_ERROR(0x01000041,
"unknown input file extension; only (pgm, ppm, and yuv) are"
" suppoted\n");
exit(-1);
}

}
else
{
printf("Please supply a proper input filename with a proper three-letter"
" extension\n");
exit(-1);
}
OJPH_ERROR(0x01000051,
"Please supply a proper input filename with a proper three-letter "
"extension\n");

ojph::j2c_outfile j2c_file;
j2c_file.open(output_filename);
Expand Down Expand Up @@ -724,11 +737,6 @@ int main(int argc, char * argv[]) {
delete[] is_signed;
}
}
catch (const char *e)
{
printf("%s\n", e);
exit (-1);
}
catch (const std::exception& e)
{
const char *p = e.what();
Expand Down
61 changes: 25 additions & 36 deletions src/apps/ojph_expand/ojph_expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "ojph_file.h"
#include "ojph_codestream.h"
#include "ojph_params.h"
#include "ojph_message.h"

//////////////////////////////////////////////////////////////////////////////
bool get_arguments(int argc, char *argv[],
Expand Down Expand Up @@ -115,11 +116,9 @@ int main(int argc, char *argv[]) {
{

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

if (siz.get_num_components() != 3)
{
printf("The file has %d color components; this cannot be saved to"
" a .ppm file\n", siz.get_num_components());
exit(-1);
}
OJPH_ERROR(0x020000002,
"The file has %d color components; this cannot be saved to"
" a .ppm file\n", siz.get_num_components());
bool all_same = true;
ojph::point p = siz.get_downsampling(0);
for (int i = 1; i < siz.get_num_components(); ++i)
Expand All @@ -145,11 +142,9 @@ int main(int argc, char *argv[]) {
all_same = all_same && (p1.x == p.x) && (p1.y == p.y);
}
if (!all_same)
{
printf("To save an image to ppm, all the components must have the "
"downsampling ratio\n");
exit(-1);
}
OJPH_ERROR(0x020000003,
"To save an image to ppm, all the components must have the "
"downsampling ratio\n");
ppm.configure(siz.get_image_extent().x - siz.get_image_offset().x,
siz.get_image_extent().y - siz.get_image_offset().y,
siz.get_num_components(), siz.get_bit_depth(0));
Expand All @@ -162,19 +157,15 @@ int main(int argc, char *argv[]) {
ojph::param_siz_t siz = codestream.access_siz();

if (siz.get_num_components() != 3 && siz.get_num_components() != 1)
{
printf("The file has %d color components; this cannot be saved to"
" a .yuv file\n", siz.get_num_components());
exit(-1);
}
OJPH_ERROR(0x020000004,
"The file has %d color components; this cannot be saved to"
" a .yuv file\n", siz.get_num_components());
ojph::param_cod_t cod = codestream.access_cod();
if (cod.is_using_color_transform())
{
printf("The current implementation of yuv file object does not "
OJPH_ERROR(0x020000005,
"The current implementation of yuv file object does not "
"support saving a file when conversion from yuv to rgb is needed; "
"In any case, this is not the normal usage of a yuv file");
exit(-1);
}
ojph::point points[3];
int max_bit_depth = 0;
for (int i = 0; i < siz.get_num_components(); ++i)
Expand All @@ -189,18 +180,14 @@ int main(int argc, char *argv[]) {
base = &yuv;
}
else
{
printf("unknown output file extension; only (pgm, ppm, and yuv) are"
OJPH_ERROR(0x020000006,
"unknown output file extension; only (pgm, ppm, and yuv) are"
" suppoted\n");
exit(-1);
}
}
else
{
printf("Please supply a proper output filename with a proper three-letter"
" extension\n");
exit(-1);
}
OJPH_ERROR(0x020000007,
"Please supply a proper output filename with a proper three-letter"
" extension\n");

codestream.create();

Expand Down Expand Up @@ -240,12 +227,14 @@ int main(int argc, char *argv[]) {
base->close();
codestream.close();
}
catch (const char *e)
catch (const std::exception& e)
{
printf("%s\n", e);
exit (-1);
const char *p = e.what();
if (strncmp(p, "ojph error", 10) != 0)
printf("%s\n", p);
exit(-1);
}

clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("Elapsed time = %f\n", elapsed_secs);
Expand Down
Loading

0 comments on commit 858f44b

Please sign in to comment.