From d2b3ae6be869f97e5f538a117a6e2aa00246e182 Mon Sep 17 00:00:00 2001 From: Xerbo Date: Sun, 14 Aug 2022 14:27:55 +0100 Subject: [PATCH] Standardise warning/error format --- CMakeLists.txt | 4 ++-- src/image.c | 7 ++++--- src/main.c | 9 +++++---- src/pngio.c | 52 +++++++++++++++++++++++++++----------------------- src/util.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 17 ++++++++--------- 6 files changed, 98 insertions(+), 42 deletions(-) create mode 100644 src/util.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f783b0e..24117c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,8 @@ find_package(PNG) # libsndfile find_package(LibSndFile) -set(LIB_C_SOURCE_FILES src/color.c src/dsp.c src/filter.c src/image.c src/algebra.c src/libs/median.c) -set(EXE_C_SOURCE_FILES src/main.c src/pngio.c src/argparse/argparse.c) +set(LIB_C_SOURCE_FILES src/color.c src/dsp.c src/filter.c src/image.c src/algebra.c src/libs/median.c src/util.c) +set(EXE_C_SOURCE_FILES src/main.c src/pngio.c src/argparse/argparse.c src/util.c) set(LIB_C_HEADER_FILES src/apt.h) # Link with static library for aptdec executable, so we don't need to set the path diff --git a/src/image.c b/src/image.c index 274373b..5e87254 100644 --- a/src/image.c +++ b/src/image.c @@ -25,6 +25,7 @@ #include "apt.h" #include "algebra.h" #include "image.h" +#include "util.h" static linear_t compute_regression(float *wedges) { // { 0.106, 0.215, 0.324, 0.433, 0.542, 0.652, 0.78, 0.87, 0.0 } @@ -116,7 +117,7 @@ apt_channel_t apt_calibrate(float **prow, int nrow, int offset, int width) { // The minimum rows required to decode a full frame if (nrow < APT_CALIBRATION_ROWS) { - fprintf(stderr, "Telemetry decoding error, not enough rows\n"); + error_noexit("Telemetry decoding error, not enough rows"); return APT_CHANNEL_UNKNOWN; } @@ -151,7 +152,7 @@ apt_channel_t apt_calibrate(float **prow, int nrow, int offset, int width) { // Make sure that theres at least one full frame in the image if (nrow < telestart + APT_FRAME_LEN) { - fprintf(stderr, "Telemetry decoding error, not enough rows\n"); + error_noexit("Telemetry decoding error, not enough rows"); return APT_CHANNEL_UNKNOWN; } @@ -214,7 +215,7 @@ apt_channel_t apt_calibrate(float **prow, int nrow, int offset, int width) { } if(bestFrame == -1){ - fprintf(stderr, "Something has gone very wrong, please file a bug report.\n"); + error_noexit("Something has gone very wrong, please file a bug report"); return APT_CHANNEL_UNKNOWN; } diff --git a/src/main.c b/src/main.c index 742b24e..21d06c6 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,7 @@ #include "pngio.h" #include "image.h" #include "color.h" +#include "util.h" // Audio file static SNDFILE *audioFile; @@ -184,7 +185,7 @@ static int processAudio(char *filename, options_t *opts){ // Fallback for detecting the zenith // TODO: encode metadata in raw images if(opts->map != NULL && opts->map[0] != '\0' && img.zenith == 0){ - fprintf(stderr, "Guessing zenith in image, map will most likely be misaligned.\n"); + warning("Guessing zenith in image, map will most likely be misaligned"); img.zenith = img.nrow / 2; } @@ -278,17 +279,17 @@ static int initsnd(char *filename) { infwav.format = 0; audioFile = sf_open(filename, SFM_READ, &infwav); if (audioFile == NULL) { - fprintf(stderr, "Could not open %s\n", filename); + error_noexit("Could not file"); return 0; } res = apt_init(infwav.samplerate); printf("Input file: %s\n", filename); if(res < 0) { - fprintf(stderr, "Input sample rate too low: %d\n", infwav.samplerate); + error_noexit("Input sample rate too low"); return 0; }else if(res > 0) { - fprintf(stderr, "Input sample rate too high: %d\n", infwav.samplerate); + error_noexit("Input sample rate too high"); return 0; } printf("Input sample rate: %d\n", infwav.samplerate); diff --git a/src/pngio.c b/src/pngio.c index 48021b3..70dfbda 100644 --- a/src/pngio.c +++ b/src/pngio.c @@ -23,13 +23,14 @@ #include #include #include +#include "util.h" #include "pngio.h" int mapOverlay(char *filename, apt_rgb_t **crow, int nrow, int zenith, int MCIR) { FILE *fp = fopen(filename, "rb"); if(!fp) { - fprintf(stderr, "Cannot open %s\n", filename); + error_noexit("Cannot open map"); return 0; } @@ -55,16 +56,16 @@ int mapOverlay(char *filename, apt_rgb_t **crow, int nrow, int zenith, int MCIR) // Check the image if(width != 1040){ - fprintf(stderr, "Map must be 1040px wide.\n"); + error_noexit("Map must be 1040px wide"); return 0; }else if(bit_depth != 16){ - fprintf(stderr, "Map must be 16 bit color.\n"); + error_noexit("Map must be 16 bit color"); return 0; }else if(color_type != PNG_COLOR_TYPE_RGB){ - fprintf(stderr, "Map must be RGB.\n"); + error_noexit("Map must be RGB"); return 0; }else if(zenith > height/2 || nrow-zenith > height/2){ - fprintf(stderr, "WARNING: Map is too short to cover entire image\n"); + warning("Map is too short to cover entire image"); } // Create row buffers @@ -145,7 +146,7 @@ int readRawImage(char *filename, float **prow, int *nrow) { FILE *fp = fopen(filename, "rb"); printf("%s", filename); if(!fp) { - fprintf(stderr, "Cannot open %s\n", filename); + error_noexit("Cannot open image"); return 0; } @@ -171,13 +172,13 @@ int readRawImage(char *filename, float **prow, int *nrow) { // Check the image if(width != APT_IMG_WIDTH){ - fprintf(stderr, "Raw image must be %ipx wide.\n", APT_IMG_WIDTH); + error_noexit("Raw image must be 2080px wide"); return 0; }else if(bit_depth != 8){ - fprintf(stderr, "Raw image must have 8 bit color.\n"); + error_noexit("Raw image must have 8 bit color"); return 0; }else if(color_type != PNG_COLOR_TYPE_GRAY){ - fprintf(stderr, "Raw image must be grayscale.\n"); + error_noexit("Raw image must be grayscale"); return 0; } @@ -214,7 +215,7 @@ int readPalette(char *filename, apt_rgb_t **pixels) { sprintf(buffer, PALETTE_DIR"/%s", filename); fp = fopen(buffer, "rb"); if(!fp){ - fprintf(stderr, "Cannot open %s\n", filename); + error_noexit("Cannot open palette"); return 0; } } @@ -241,13 +242,13 @@ int readPalette(char *filename, apt_rgb_t **pixels) { // Check the image if(width != 256 && height != 256){ - fprintf(stderr, "Palette must be 256x256.\n"); + error_noexit("Palette must be 256x256"); return 0; }else if(bit_depth != 8){ - fprintf(stderr, "Palette must be 8 bit color.\n"); + error_noexit("Palette must be 8 bit color"); return 0; }else if(color_type != PNG_COLOR_TYPE_RGB){ - fprintf(stderr, "Palette must be RGB.\n"); + error_noexit("Palette must be RGB"); return 0; } @@ -295,7 +296,7 @@ void prow2crow(float **prow, int nrow, char *palette, apt_rgb_t **crow){ int applyUserPalette(float **prow, int nrow, char *filename, apt_rgb_t **crow){ apt_rgb_t *pal_row[256]; if(!readPalette(filename, pal_row)){ - fprintf(stderr, "Could not read palette\n"); + error_noexit("Could not read palette"); return 0; } @@ -358,9 +359,12 @@ int ImageOut(options_t *opts, apt_image_t *img, int offset, int width, char *des case Histogram_Equalise: break; case Linear_Equalise: break; case Crop_Noise: break; - default: - fprintf(stderr, "NOTICE: Unrecognised effect, \"%c\"\n", opts->effects[i]); + default: { + char text[100]; + sprintf(text, "Unrecognised effect, \"%c\"", opts->effects[i]); + warning(text); break; + } } } @@ -374,13 +378,13 @@ int ImageOut(options_t *opts, apt_image_t *img, int offset, int width, char *des png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { png_destroy_write_struct(&png_ptr, (png_infopp) NULL); - fprintf(stderr, "Could not create a PNG writer\n"); + error_noexit("Could not create a PNG writer"); return 0; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_write_struct(&png_ptr, (png_infopp) NULL); - fprintf(stderr, "Could not create a PNG writer\n"); + error_noexit("Could not create a PNG writer"); return 0; } @@ -402,7 +406,7 @@ int ImageOut(options_t *opts, apt_image_t *img, int offset, int width, char *des // Init I/O pngfile = fopen(outName, "wb"); if (!pngfile) { - fprintf(stderr, "Could not open %s for writing\n", outName); + error_noexit("Could not open PNG for writing"); return 1; } png_init_io(png_ptr, pngfile); @@ -432,11 +436,11 @@ int ImageOut(options_t *opts, apt_image_t *img, int offset, int width, char *des // Map stuff if(opts->map != NULL && opts->map[0] != '\0'){ if(!mapOverlay(opts->map, crow, img->nrow, img->zenith+opts->mapOffset, CONTAINS(opts->type, MCIR))){ - fprintf(stderr, "Skipping MCIR generation.\n"); + warning("Skipping MCIR generation"); return 0; } }else if(CONTAINS(opts->type, MCIR)){ - fprintf(stderr, "Skipping MCIR generation; no map provided.\n"); + warning("Skipping MCIR generation; no map provided"); return 0; } @@ -502,13 +506,13 @@ int initWriter(options_t *opts, apt_image_t *img, int width, int height, char *d rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!rt_png_ptr) { png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL); - fprintf(stderr, "Could not create a PNG writer\n"); + error_noexit("Could not create a PNG writer"); return 0; } rt_info_ptr = png_create_info_struct(rt_png_ptr); if (!rt_info_ptr) { png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL); - fprintf(stderr, "Could not create a PNG writer\n"); + error_noexit("Could not create a PNG writer"); return 0; } @@ -525,7 +529,7 @@ int initWriter(options_t *opts, apt_image_t *img, int width, int height, char *d // Init I/O rt_pngfile = fopen(outName, "wb"); if (!rt_pngfile) { - fprintf(stderr, "Could not open %s for writing\n", outName); + error_noexit("Could not open PNG for writing"); return 0; } png_init_io(rt_png_ptr, rt_pngfile); diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..a808c39 --- /dev/null +++ b/src/util.c @@ -0,0 +1,51 @@ +/* + * aptdec - A lightweight FOSS (NOAA) APT decoder + * Copyright (C) 2019-2022 Xerbo (xerbo@protonmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "util.h" + +#include +#include + +void error_noexit(const char *text) { +#ifdef _WIN32 + fprintf(stderr, "Error: %s\r\n", text); +#else + fprintf(stderr, "\033[31mError: %s\033[0m\n", text); +#endif +} +void error(const char *text) { + error_noexit(text); + exit(1); +} +void warning(const char *text) { +#ifdef _WIN32 + fprintf(stderr, "Warning: %s\r\n", text); +#else + fprintf(stderr, "\033[33mWarning: %s\033[0m\n", text); +#endif +} + +float clamp(float x, float hi, float lo) { + if (x > hi) return hi; + if (x < lo) return lo; + return x; +} + +float clamp_half(float x, float hi) { + return clamp(x, hi, -hi); +} diff --git a/src/util.h b/src/util.h index 6a54d84..cebcaca 100644 --- a/src/util.h +++ b/src/util.h @@ -17,19 +17,18 @@ */ #include - #define M_PIf 3.14159265358979323846f #define M_TAUf (M_PIf * 2.0f) #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b)) -static float clamp(float x, float hi, float lo) { - if (x > hi) return hi; - if (x < lo) return lo; - return x; -} +#ifndef UTIL_H +#define UTIL_H +float clamp(float x, float hi, float lo); +float clamp_half(float x, float hi); -static float clamp_half(float x, float hi) { - return clamp(x, hi, -hi); -} +void error(const char *text); +void error_noexit(const char *text); +void warning(const char *text); +#endif