Browse Source

Standardise warning/error format

tags/v1.8.0
Xerbo 2 years ago
parent
commit
d2b3ae6be8
No known key found for this signature in database GPG Key ID: 34103F6D8F11CEB0
6 changed files with 98 additions and 42 deletions
  1. +2
    -2
      CMakeLists.txt
  2. +4
    -3
      src/image.c
  3. +5
    -4
      src/main.c
  4. +28
    -24
      src/pngio.c
  5. +51
    -0
      src/util.c
  6. +8
    -9
      src/util.h

+ 2
- 2
CMakeLists.txt View File

@@ -11,8 +11,8 @@ find_package(PNG)
# libsndfile # libsndfile
find_package(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) set(LIB_C_HEADER_FILES src/apt.h)


# Link with static library for aptdec executable, so we don't need to set the path # Link with static library for aptdec executable, so we don't need to set the path


+ 4
- 3
src/image.c View File

@@ -25,6 +25,7 @@
#include "apt.h" #include "apt.h"
#include "algebra.h" #include "algebra.h"
#include "image.h" #include "image.h"
#include "util.h"


static linear_t compute_regression(float *wedges) { 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 } // { 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 // The minimum rows required to decode a full frame
if (nrow < APT_CALIBRATION_ROWS) { 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; 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 // Make sure that theres at least one full frame in the image
if (nrow < telestart + APT_FRAME_LEN) { 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; return APT_CHANNEL_UNKNOWN;
} }


@@ -214,7 +215,7 @@ apt_channel_t apt_calibrate(float **prow, int nrow, int offset, int width) {
} }


if(bestFrame == -1){ 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; return APT_CHANNEL_UNKNOWN;
} }




+ 5
- 4
src/main.c View File

@@ -37,6 +37,7 @@
#include "pngio.h" #include "pngio.h"
#include "image.h" #include "image.h"
#include "color.h" #include "color.h"
#include "util.h"


// Audio file // Audio file
static SNDFILE *audioFile; static SNDFILE *audioFile;
@@ -184,7 +185,7 @@ static int processAudio(char *filename, options_t *opts){
// Fallback for detecting the zenith // Fallback for detecting the zenith
// TODO: encode metadata in raw images // TODO: encode metadata in raw images
if(opts->map != NULL && opts->map[0] != '\0' && img.zenith == 0){ 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; img.zenith = img.nrow / 2;
} }


@@ -278,17 +279,17 @@ static int initsnd(char *filename) {
infwav.format = 0; infwav.format = 0;
audioFile = sf_open(filename, SFM_READ, &infwav); audioFile = sf_open(filename, SFM_READ, &infwav);
if (audioFile == NULL) { if (audioFile == NULL) {
fprintf(stderr, "Could not open %s\n", filename);
error_noexit("Could not file");
return 0; return 0;
} }


res = apt_init(infwav.samplerate); res = apt_init(infwav.samplerate);
printf("Input file: %s\n", filename); printf("Input file: %s\n", filename);
if(res < 0) { if(res < 0) {
fprintf(stderr, "Input sample rate too low: %d\n", infwav.samplerate);
error_noexit("Input sample rate too low");
return 0; return 0;
}else if(res > 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; return 0;
} }
printf("Input sample rate: %d\n", infwav.samplerate); printf("Input sample rate: %d\n", infwav.samplerate);


+ 28
- 24
src/pngio.c View File

@@ -23,13 +23,14 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <math.h> #include <math.h>
#include "util.h"


#include "pngio.h" #include "pngio.h"


int mapOverlay(char *filename, apt_rgb_t **crow, int nrow, int zenith, int MCIR) { int mapOverlay(char *filename, apt_rgb_t **crow, int nrow, int zenith, int MCIR) {
FILE *fp = fopen(filename, "rb"); FILE *fp = fopen(filename, "rb");
if(!fp) { if(!fp) {
fprintf(stderr, "Cannot open %s\n", filename);
error_noexit("Cannot open map");
return 0; return 0;
} }


@@ -55,16 +56,16 @@ int mapOverlay(char *filename, apt_rgb_t **crow, int nrow, int zenith, int MCIR)


// Check the image // Check the image
if(width != 1040){ if(width != 1040){
fprintf(stderr, "Map must be 1040px wide.\n");
error_noexit("Map must be 1040px wide");
return 0; return 0;
}else if(bit_depth != 16){ }else if(bit_depth != 16){
fprintf(stderr, "Map must be 16 bit color.\n");
error_noexit("Map must be 16 bit color");
return 0; return 0;
}else if(color_type != PNG_COLOR_TYPE_RGB){ }else if(color_type != PNG_COLOR_TYPE_RGB){
fprintf(stderr, "Map must be RGB.\n");
error_noexit("Map must be RGB");
return 0; return 0;
}else if(zenith > height/2 || nrow-zenith > height/2){ }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 // Create row buffers
@@ -145,7 +146,7 @@ int readRawImage(char *filename, float **prow, int *nrow) {
FILE *fp = fopen(filename, "rb"); FILE *fp = fopen(filename, "rb");
printf("%s", filename); printf("%s", filename);
if(!fp) { if(!fp) {
fprintf(stderr, "Cannot open %s\n", filename);
error_noexit("Cannot open image");
return 0; return 0;
} }


@@ -171,13 +172,13 @@ int readRawImage(char *filename, float **prow, int *nrow) {


// Check the image // Check the image
if(width != APT_IMG_WIDTH){ 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; return 0;
}else if(bit_depth != 8){ }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; return 0;
}else if(color_type != PNG_COLOR_TYPE_GRAY){ }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; return 0;
} }


@@ -214,7 +215,7 @@ int readPalette(char *filename, apt_rgb_t **pixels) {
sprintf(buffer, PALETTE_DIR"/%s", filename); sprintf(buffer, PALETTE_DIR"/%s", filename);
fp = fopen(buffer, "rb"); fp = fopen(buffer, "rb");
if(!fp){ if(!fp){
fprintf(stderr, "Cannot open %s\n", filename);
error_noexit("Cannot open palette");
return 0; return 0;
} }
} }
@@ -241,13 +242,13 @@ int readPalette(char *filename, apt_rgb_t **pixels) {


// Check the image // Check the image
if(width != 256 && height != 256){ if(width != 256 && height != 256){
fprintf(stderr, "Palette must be 256x256.\n");
error_noexit("Palette must be 256x256");
return 0; return 0;
}else if(bit_depth != 8){ }else if(bit_depth != 8){
fprintf(stderr, "Palette must be 8 bit color.\n");
error_noexit("Palette must be 8 bit color");
return 0; return 0;
}else if(color_type != PNG_COLOR_TYPE_RGB){ }else if(color_type != PNG_COLOR_TYPE_RGB){
fprintf(stderr, "Palette must be RGB.\n");
error_noexit("Palette must be RGB");
return 0; 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){ int applyUserPalette(float **prow, int nrow, char *filename, apt_rgb_t **crow){
apt_rgb_t *pal_row[256]; apt_rgb_t *pal_row[256];
if(!readPalette(filename, pal_row)){ if(!readPalette(filename, pal_row)){
fprintf(stderr, "Could not read palette\n");
error_noexit("Could not read palette");
return 0; 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 Histogram_Equalise: break;
case Linear_Equalise: break; case Linear_Equalise: break;
case Crop_Noise: 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; 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); png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) { if (!png_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp) NULL); 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; return 0;
} }
png_infop info_ptr = png_create_info_struct(png_ptr); png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) { if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp) NULL); 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; return 0;
} }


@@ -402,7 +406,7 @@ int ImageOut(options_t *opts, apt_image_t *img, int offset, int width, char *des
// Init I/O // Init I/O
pngfile = fopen(outName, "wb"); pngfile = fopen(outName, "wb");
if (!pngfile) { if (!pngfile) {
fprintf(stderr, "Could not open %s for writing\n", outName);
error_noexit("Could not open PNG for writing");
return 1; return 1;
} }
png_init_io(png_ptr, pngfile); 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 // Map stuff
if(opts->map != NULL && opts->map[0] != '\0'){ if(opts->map != NULL && opts->map[0] != '\0'){
if(!mapOverlay(opts->map, crow, img->nrow, img->zenith+opts->mapOffset, CONTAINS(opts->type, MCIR))){ 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; return 0;
} }
}else if(CONTAINS(opts->type, MCIR)){ }else if(CONTAINS(opts->type, MCIR)){
fprintf(stderr, "Skipping MCIR generation; no map provided.\n");
warning("Skipping MCIR generation; no map provided");
return 0; 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); rt_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!rt_png_ptr) { if (!rt_png_ptr) {
png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL); 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; return 0;
} }
rt_info_ptr = png_create_info_struct(rt_png_ptr); rt_info_ptr = png_create_info_struct(rt_png_ptr);
if (!rt_info_ptr) { if (!rt_info_ptr) {
png_destroy_write_struct(&rt_png_ptr, (png_infopp) NULL); 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; return 0;
} }


@@ -525,7 +529,7 @@ int initWriter(options_t *opts, apt_image_t *img, int width, int height, char *d
// Init I/O // Init I/O
rt_pngfile = fopen(outName, "wb"); rt_pngfile = fopen(outName, "wb");
if (!rt_pngfile) { if (!rt_pngfile) {
fprintf(stderr, "Could not open %s for writing\n", outName);
error_noexit("Could not open PNG for writing");
return 0; return 0;
} }
png_init_io(rt_png_ptr, rt_pngfile); png_init_io(rt_png_ptr, rt_pngfile);


+ 51
- 0
src/util.c View File

@@ -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 <https://www.gnu.org/licenses/>.
*/

#include "util.h"

#include <stdio.h>
#include <stdlib.h>

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);
}

+ 8
- 9
src/util.h View File

@@ -17,19 +17,18 @@
*/ */


#include <stdio.h> #include <stdio.h>

#define M_PIf 3.14159265358979323846f #define M_PIf 3.14159265358979323846f
#define M_TAUf (M_PIf * 2.0f) #define M_TAUf (M_PIf * 2.0f)


#define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(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

Loading…
Cancel
Save