From 3b30b6b78e3081d9a86737521e5aa33626690e25 Mon Sep 17 00:00:00 2001 From: George Vardakis Date: Sun, 9 Apr 2017 17:20:02 +0300 Subject: [PATCH] Add split image functionality --- grc/satnogs_noaa_apt_sync.xml | 17 ++- include/satnogs/noaa_apt_sync.h | 2 +- lib/noaa_apt_sync_impl.cc | 262 ++++++++++++++++++++++++-------- lib/noaa_apt_sync_impl.h | 3 +- 4 files changed, 221 insertions(+), 63 deletions(-) diff --git a/grc/satnogs_noaa_apt_sync.xml b/grc/satnogs_noaa_apt_sync.xml index 635a13a..4936518 100644 --- a/grc/satnogs_noaa_apt_sync.xml +++ b/grc/satnogs_noaa_apt_sync.xml @@ -4,7 +4,7 @@ satnogs_noaa_apt_sync [satnogs] import satnogs - satnogs.noaa_apt_sync($*filename,$*filename_png, $width, $height) + satnogs.noaa_apt_sync($*filename,$*filename_png, $width, $height, $split) Output Data Filename filename @@ -27,6 +27,21 @@ height int + + Split Images + split + False + bool + part + + + in float diff --git a/include/satnogs/noaa_apt_sync.h b/include/satnogs/noaa_apt_sync.h index c06a5c6..4fc2718 100644 --- a/include/satnogs/noaa_apt_sync.h +++ b/include/satnogs/noaa_apt_sync.h @@ -703,7 +703,7 @@ namespace gr { * class. satnogs::noaa_apt_sync::make is the public interface for * creating new instances. */ - static sptr make( const char *filename, const char *filename_png, size_t width, size_t height); + static sptr make( const char *filename, const char *filename_png, size_t width, size_t height, bool split); }; } // namespace satnogs diff --git a/lib/noaa_apt_sync_impl.cc b/lib/noaa_apt_sync_impl.cc index f887b68..6e86c23 100644 --- a/lib/noaa_apt_sync_impl.cc +++ b/lib/noaa_apt_sync_impl.cc @@ -694,10 +694,10 @@ namespace gr noaa_apt_sync::sptr noaa_apt_sync::make (const char *filename, const char *filename_png, - size_t width, size_t height) + size_t width, size_t height, bool split) { return gnuradio::get_initial_sptr ( - new noaa_apt_sync_impl (filename, filename_png, width, height)); + new noaa_apt_sync_impl (filename, filename_png, width, height, split)); } /* @@ -705,7 +705,8 @@ namespace gr */ noaa_apt_sync_impl::noaa_apt_sync_impl (const char *filename, const char *filename_png, - size_t width, size_t height) : + size_t width, size_t height, + bool split) : gr::sync_block ("noaa_apt_sync", gr::io_signature::make (1, 1, sizeof(float)), gr::io_signature::make (0, 0, 0)), @@ -722,7 +723,8 @@ namespace gr d_temp_min_value (100), d_filename_png (filename_png), d_width (width), - d_height (height) + d_height (height), + d_split (split) { d_out = fopen (filename, "wb"); } @@ -744,6 +746,8 @@ namespace gr png_byte color_type; png_byte bit_depth; png_bytep * row_pointers; + png_bytep * row_pointers_left; + png_bytep * row_pointers_right; size_t num_pictures = 0; int height = size / d_width; int picture_modulo = 0; @@ -761,77 +765,216 @@ namespace gr if (size % d_width > 0) { row_modulo = size % d_width; } - for (int image = 0; image < num_pictures; image++) { - if (file.read (buffer.data (), size)) { - FILE *fp; - std::string fn; - if (num_pictures == 1) { - fp = fopen (d_filename_png, "wb"); + if (file.read (buffer.data (), size)) { + for (int image = 0; image < num_pictures; image++) { + if (d_split == false) { + FILE *fp; + std::string fn; + if (num_pictures == 1) { + fp = fopen (d_filename_png, "wb"); + } + else { + std::string fn (d_filename_png); + std::size_t found = fn.find ("."); + if (found == std::string::npos) + fn.append (std::to_string (image)); + else { + fn.insert (found, std::to_string (image)); + } + fp = fopen (fn.c_str (), "wb"); + + if ((image == num_pictures - 1) && (picture_modulo > 0)) { + height = picture_modulo; + } + else { + height = d_height; + } + } + if (!fp) + printf ("Error opening file\n"); + png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, + NULL, + NULL); + + if (!png_ptr) + printf ("[write_png_file] png_create_write_struct failed"); + + info_ptr = png_create_info_struct (png_ptr); + if (!info_ptr) + printf ("[write_png_file] png_create_info_struct failed"); + + if (setjmp(png_jmpbuf(png_ptr))) + printf ("[write_png_file] Error during init_io"); + + png_init_io (png_ptr, fp); + if (setjmp(png_jmpbuf(png_ptr))) + printf ("[write_png_file] Error during writing header"); + png_set_IHDR (png_ptr, info_ptr, d_width, height, 8, + PNG_COLOR_TYPE_GRAY, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE); + + png_write_info (png_ptr, info_ptr); + + if (setjmp(png_jmpbuf(png_ptr))) + printf ("[write_png_file] Error during writing bytes"); + row_pointers = (png_bytep*) malloc (sizeof(png_bytep) * height); + for (int y = 0; y < height; y++) { + row_pointers[y] = (png_byte*) malloc ( + png_get_rowbytes (png_ptr, info_ptr)); + if ((image == num_pictures - 1) && (y == height - 1) + && (row_modulo != 0)) { + memcpy ( + row_pointers[y], + buffer.data () + image * d_width * d_height + y * d_width, + row_modulo * sizeof(uint8_t)); + memset (row_pointers[y] + row_modulo, 0, + (d_width - row_modulo) * sizeof(uint8_t)); + } + else { + memcpy ( + row_pointers[y], + buffer.data () + image * d_width * d_height + y * d_width, + d_width * sizeof(uint8_t)); + } + } + png_write_image (png_ptr, row_pointers); + if (setjmp(png_jmpbuf(png_ptr))) + printf ("[write_png_file] Error during end of write"); + png_write_end (png_ptr, NULL); + fclose (fp); } else { + png_structp png_ptr_left; + png_structp png_ptr_right; + png_infop info_ptr_left; + png_infop info_ptr_right; + FILE *fp_left; + FILE *fp_right; std::string fn (d_filename_png); - fn.append (std::to_string (image)); - fp = fopen (fn.c_str (), "wb"); - if ((image == num_pictures - 1) && (picture_modulo > 0)) { - height = picture_modulo; + std::string fn_left (d_filename_png); + std::string fn_right (d_filename_png); + if (num_pictures == 1) { + std::size_t found = fn.find ("."); + if (found == std::string::npos) { + fn_left.append ("_left"); + fn_right.append ("_right"); + } + else { + fn_left.insert (found, "_left"); + fn_right.insert (found, "_right"); + } } else { - height = d_height; + std::size_t found = fn.find ("."); + if (found == std::string::npos) { + fn_left.append (std::to_string (image).append ("_left")); + fn_right.append (std::to_string (image).append ("_right")); + } + else { + fn_left.insert (found, std::to_string (image).append ("_left")); + fn_right.insert (found, + std::to_string (image).append ("_right")); + } + if ((image == num_pictures - 1) && (picture_modulo > 0)) { + height = picture_modulo; + } + else { + height = d_height; + } } - } - if (!fp) - printf ("Error opening file\n"); - png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, - NULL); + fp_left = fopen (fn_left.c_str (), "wb"); + fp_right = fopen (fn_right.c_str (), "wb"); + if ((!fp_left) && (!fp_right)) + printf ("Error opening file\n"); + png_ptr_left = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, + NULL, + NULL); + png_ptr_right = png_create_write_struct (PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); - if (!png_ptr) - printf ("[write_png_file] png_create_write_struct failed"); + if ((!png_ptr_left) && (!png_ptr_right)) + printf ("[write_png_file] png_create_write_struct failed"); - info_ptr = png_create_info_struct (png_ptr); - if (!info_ptr) - printf ("[write_png_file] png_create_info_struct failed"); + info_ptr_left = png_create_info_struct (png_ptr_left); + info_ptr_right = png_create_info_struct (png_ptr_right); + if ((!info_ptr_left) && (!info_ptr_right) && (!info_ptr)) + printf ("[write_png_file] png_create_info_struct failed"); - if (setjmp(png_jmpbuf(png_ptr))) - printf ("[write_png_file] Error during init_io"); + if (setjmp( + png_jmpbuf(png_ptr_left)) && (setjmp(png_jmpbuf(png_ptr_right)))) + printf ("[write_png_file] Error during init_io"); - png_init_io (png_ptr, fp); - if (setjmp(png_jmpbuf(png_ptr))) - printf ("[write_png_file] Error during writing header"); - png_set_IHDR (png_ptr, info_ptr, d_width, height, 8, - PNG_COLOR_TYPE_GRAY, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, - PNG_FILTER_TYPE_BASE); + png_init_io (png_ptr_left, fp_left); + png_init_io (png_ptr_right, fp_right); - png_write_info (png_ptr, info_ptr); + if (setjmp( + png_jmpbuf(png_ptr_left)) && (setjmp(png_jmpbuf(png_ptr_right)))) + printf ("[write_png_file] Error during writing header"); + png_set_IHDR (png_ptr_left, info_ptr_left, d_width / 2, height, 8, + PNG_COLOR_TYPE_GRAY, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE); + png_set_IHDR (png_ptr_right, info_ptr_right, d_width / 2, height, 8, + PNG_COLOR_TYPE_GRAY, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE); - if (setjmp(png_jmpbuf(png_ptr))) - printf ("[write_png_file] Error during writing bytes"); - row_pointers = (png_bytep*) malloc (sizeof(png_bytep) * height); - for (int y = 0; y < height; y++) { - row_pointers[y] = (png_byte*) malloc ( - png_get_rowbytes (png_ptr, info_ptr)); - if ((image == num_pictures - 1) && (y == height - 1) - && (row_modulo != 0)) { - memcpy (row_pointers[y], + png_write_info (png_ptr_left, info_ptr_left); + png_write_info (png_ptr_right, info_ptr_right); + + if (setjmp(png_jmpbuf(png_ptr_left)) + && (setjmp(png_jmpbuf(png_ptr_right)))) + printf ("[write_png_file] Error during writing bytes"); + row_pointers_left = (png_bytep*) malloc ( + sizeof(png_bytep) * height); + row_pointers_right = (png_bytep*) malloc ( + sizeof(png_bytep) * height); + for (int y = 0; y < height; y++) { + row_pointers_left[y] = (png_byte*) malloc ( + (d_width / 2) * sizeof(png_byte)); + row_pointers_right[y] = (png_byte*) malloc ( + (d_width / 2) * sizeof(png_byte)); + if ((image == num_pictures - 1) && (y == height - 1) + && (row_modulo != 0)) { + if (row_modulo < (d_width / 2)) { + memcpy ( + row_pointers_left[y], buffer.data () + image * d_width * d_height + y * d_width, row_modulo * sizeof(uint8_t)); - memset ( - buffer.data () + image * d_width * d_height + y * d_width - + row_modulo, - 0, (d_width - row_modulo) * sizeof(uint8_t)); - } - else { - memcpy (row_pointers[y], - buffer.data () + image * d_width * d_height + y * d_width, - d_width * sizeof(uint8_t)); + memset (row_pointers_left[y] + row_modulo, 0, + ((d_width / 2) - row_modulo) * sizeof(uint8_t)); + memset (row_pointers_right[y] + row_modulo, 0, + (d_width / 2) * sizeof(uint8_t)); + } + } + else { + memcpy ( + row_pointers_left[y], + buffer.data () + image * d_width * d_height + y * d_width, + (d_width / 2) * sizeof(uint8_t)); + memcpy ( + row_pointers_right[y], + buffer.data () + image * d_width * d_height + y * d_width + + (d_width / 2), + (d_width / 2) * sizeof(uint8_t)); + } } + png_write_image (png_ptr_left, row_pointers_left); + png_write_image (png_ptr_right, row_pointers_right); + if (setjmp( + png_jmpbuf(png_ptr_left)) && setjmp(png_jmpbuf(png_ptr_right))) + printf ("[write_png_file] Error during end of write"); + png_write_end (png_ptr_left, NULL); + png_write_end (png_ptr_right, NULL); + fclose (fp_left); + fclose (fp_right); } - png_write_image (png_ptr, row_pointers); - if (setjmp(png_jmpbuf(png_ptr))) - printf ("[write_png_file] Error during end of write"); - png_write_end (png_ptr, NULL); - fclose (fp); } } } @@ -881,7 +1024,6 @@ namespace gr } } } - return noutput_items; } diff --git a/lib/noaa_apt_sync_impl.h b/lib/noaa_apt_sync_impl.h index 6abe73c..8005530 100644 --- a/lib/noaa_apt_sync_impl.h +++ b/lib/noaa_apt_sync_impl.h @@ -701,9 +701,10 @@ namespace gr { const char* d_filename_png; size_t d_width; size_t d_height; + bool d_split; public: - noaa_apt_sync_impl( const char *filename, const char *filename_png, size_t width, size_t height); + noaa_apt_sync_impl( const char *filename, const char *filename_png, size_t width, size_t height, bool split); ~noaa_apt_sync_impl(); void produce_image();