diff --git a/Makefile b/Makefile index 6c7e261..2d6aee7 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ fun: all clean: rm -f encode decode debug *.o {8000,11025,16000,40000,44100,48000}.{ppm,wav,dat} -encode: encode.o mmap_file.o pcm.o wav.o alsa.o yuv.o +encode: encode.o mmap_file.o pcm.o wav.o alsa.o yuv.o img.o ppm.o decode: decode.o mmap_file.o pcm.o wav.o alsa.o window.o ddc.o delay.o yuv.o diff --git a/decode.c b/decode.c index 60668d4..b730c95 100644 --- a/decode.c +++ b/decode.c @@ -58,10 +58,8 @@ int main(int argc, char **argv) if (argc == 3) ppm_name = argv[2]; - if (!open_pcm_read(&pcm, pcm_name)) { - fprintf(stderr, "couldnt open %s\n", pcm_name); + if (!open_pcm_read(&pcm, pcm_name)) return 1; - } info_pcm(pcm); diff --git a/encode.c b/encode.c index c6da4d6..a5e2692 100644 --- a/encode.c +++ b/encode.c @@ -12,20 +12,18 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include #include #include -#include "mmap_file.h" #include "yuv.h" #include "utils.h" #include "pcm.h" +#include "img.h" +img_t *img; pcm_t *pcm; complex float nco; float hz2rad; int channels; short *buff; -uint8_t *pixel; float rate = 48000; -const int width = 320; -const int height = 240; int add_sample(float val) { @@ -70,14 +68,14 @@ void y_scan(int y) float xf = fclampf((320.0 * (float)ticks) / (0.088 * rate), 0.0, 319.0); int x0 = xf; int x1 = fclampf(x0 + 1, 0.0, 319.0); - int off0 = 3 * y * width + 3 * x0; - int off1 = 3 * y * width + 3 * x1; - uint8_t R0 = pixel[off0 + 0]; - uint8_t G0 = pixel[off0 + 1]; - uint8_t B0 = pixel[off0 + 2]; - uint8_t R1 = pixel[off1 + 0]; - uint8_t G1 = pixel[off1 + 1]; - uint8_t B1 = pixel[off1 + 2]; + int off0 = 3 * y * img->width + 3 * x0; + int off1 = 3 * y * img->width + 3 * x1; + uint8_t R0 = img->pixel[off0 + 0]; + uint8_t G0 = img->pixel[off0 + 1]; + uint8_t B0 = img->pixel[off0 + 2]; + uint8_t R1 = img->pixel[off1 + 0]; + uint8_t G1 = img->pixel[off1 + 1]; + uint8_t B1 = img->pixel[off1 + 2]; uint8_t R = flerpf(R0, R1, xf - (float)x0); uint8_t G = flerpf(G0, G1, xf - (float)x0); uint8_t B = flerpf(B0, B1, xf - (float)x0); @@ -91,16 +89,16 @@ void v_scan(int y) float xf = fclampf((160.0 * (float)ticks) / (0.044 * rate), 0.0, 159.0); int x0 = xf; int x1 = fclampf(x0 + 1, 0.0, 159.0); - int evn0 = 3 * y * width + 6 * x0; - int evn1 = 3 * y * width + 6 * x1; - int odd0 = 3 * (y + 1) * width + 6 * x0; - int odd1 = 3 * (y + 1) * width + 6 * x1; - uint8_t R0 = (pixel[evn0 + 0] + pixel[odd0 + 0] + pixel[evn0 + 3] + pixel[odd0 + 3]) / 4; - uint8_t G0 = (pixel[evn0 + 1] + pixel[odd0 + 1] + pixel[evn0 + 4] + pixel[odd0 + 4]) / 4; - uint8_t B0 = (pixel[evn0 + 2] + pixel[odd0 + 2] + pixel[evn0 + 5] + pixel[odd0 + 5]) / 4; - uint8_t R1 = (pixel[evn1 + 0] + pixel[odd1 + 0] + pixel[evn1 + 3] + pixel[odd1 + 3]) / 4; - uint8_t G1 = (pixel[evn1 + 1] + pixel[odd1 + 1] + pixel[evn1 + 4] + pixel[odd1 + 4]) / 4; - uint8_t B1 = (pixel[evn1 + 2] + pixel[odd1 + 2] + pixel[evn1 + 5] + pixel[odd1 + 5]) / 4; + int evn0 = 3 * y * img->width + 6 * x0; + int evn1 = 3 * y * img->width + 6 * x1; + int odd0 = 3 * (y + 1) * img->width + 6 * x0; + int odd1 = 3 * (y + 1) * img->width + 6 * x1; + uint8_t R0 = (img->pixel[evn0 + 0] + img->pixel[odd0 + 0] + img->pixel[evn0 + 3] + img->pixel[odd0 + 3]) / 4; + uint8_t G0 = (img->pixel[evn0 + 1] + img->pixel[odd0 + 1] + img->pixel[evn0 + 4] + img->pixel[odd0 + 4]) / 4; + uint8_t B0 = (img->pixel[evn0 + 2] + img->pixel[odd0 + 2] + img->pixel[evn0 + 5] + img->pixel[odd0 + 5]) / 4; + uint8_t R1 = (img->pixel[evn1 + 0] + img->pixel[odd1 + 0] + img->pixel[evn1 + 3] + img->pixel[odd1 + 3]) / 4; + uint8_t G1 = (img->pixel[evn1 + 1] + img->pixel[odd1 + 1] + img->pixel[evn1 + 4] + img->pixel[odd1 + 4]) / 4; + uint8_t B1 = (img->pixel[evn1 + 2] + img->pixel[odd1 + 2] + img->pixel[evn1 + 5] + img->pixel[odd1 + 5]) / 4; uint8_t R = flerpf(R0, R1, xf - (float)x0); uint8_t G = flerpf(G0, G1, xf - (float)x0); uint8_t B = flerpf(B0, B1, xf - (float)x0); @@ -113,16 +111,16 @@ void u_scan(int y) float xf = fclampf((160.0 * (float)ticks) / (0.044 * rate), 0.0, 159.0); int x0 = xf; int x1 = fclampf(x0 + 1, 0.0, 159.0); - int evn0 = 3 * (y - 1) * width + 6 * x0; - int evn1 = 3 * (y - 1) * width + 6 * x1; - int odd0 = 3 * y * width + 6 * x0; - int odd1 = 3 * y * width + 6 * x1; - uint8_t R0 = (pixel[evn0 + 0] + pixel[odd0 + 0] + pixel[evn0 + 3] + pixel[odd0 + 3]) / 4; - uint8_t G0 = (pixel[evn0 + 1] + pixel[odd0 + 1] + pixel[evn0 + 4] + pixel[odd0 + 4]) / 4; - uint8_t B0 = (pixel[evn0 + 2] + pixel[odd0 + 2] + pixel[evn0 + 5] + pixel[odd0 + 5]) / 4; - uint8_t R1 = (pixel[evn1 + 0] + pixel[odd1 + 0] + pixel[evn1 + 3] + pixel[odd1 + 3]) / 4; - uint8_t G1 = (pixel[evn1 + 1] + pixel[odd1 + 1] + pixel[evn1 + 4] + pixel[odd1 + 4]) / 4; - uint8_t B1 = (pixel[evn1 + 2] + pixel[odd1 + 2] + pixel[evn1 + 5] + pixel[odd1 + 5]) / 4; + int evn0 = 3 * (y - 1) * img->width + 6 * x0; + int evn1 = 3 * (y - 1) * img->width + 6 * x1; + int odd0 = 3 * y * img->width + 6 * x0; + int odd1 = 3 * y * img->width + 6 * x1; + uint8_t R0 = (img->pixel[evn0 + 0] + img->pixel[odd0 + 0] + img->pixel[evn0 + 3] + img->pixel[odd0 + 3]) / 4; + uint8_t G0 = (img->pixel[evn0 + 1] + img->pixel[odd0 + 1] + img->pixel[evn0 + 4] + img->pixel[odd0 + 4]) / 4; + uint8_t B0 = (img->pixel[evn0 + 2] + img->pixel[odd0 + 2] + img->pixel[evn0 + 5] + img->pixel[odd0 + 5]) / 4; + uint8_t R1 = (img->pixel[evn1 + 0] + img->pixel[odd1 + 0] + img->pixel[evn1 + 3] + img->pixel[odd1 + 3]) / 4; + uint8_t G1 = (img->pixel[evn1 + 1] + img->pixel[odd1 + 1] + img->pixel[evn1 + 4] + img->pixel[odd1 + 4]) / 4; + uint8_t B1 = (img->pixel[evn1 + 2] + img->pixel[odd1 + 2] + img->pixel[evn1 + 5] + img->pixel[odd1 + 5]) / 4; uint8_t R = flerpf(R0, R1, xf - (float)x0); uint8_t G = flerpf(G0, G1, xf - (float)x0); uint8_t B = flerpf(B0, B1, xf - (float)x0); @@ -137,61 +135,22 @@ int main(int argc, char **argv) return 1; } - size_t ppm_size; - void *ppm_p; - if (!mmap_file_ro(&ppm_p, argv[1], &ppm_size)) { - fprintf(stderr, "couldnt open ppm file\n"); + if (!open_img_read(&img, argv[1])) + return 1; + + if (320 != img->width || 240 != img->height) { + fprintf(stderr, "image not 320x240\n"); + close_img(img); return 1; } - char *ppm_c = (char *)ppm_p; - size_t ppm_i = 0; - if (ppm_size < width * height * 3 || ppm_c[ppm_i++] != 'P' || ppm_c[ppm_i++] != '6') { - fprintf(stderr, "unsupported image file\n"); - return 1; - } - - char ppm_buff[16]; - int ppm_int[3]; - for (int n = 0; n < 3; n++) { - for (; ppm_i < ppm_size; ppm_i++) { - if (ppm_c[ppm_i] >= '0' && ppm_c[ppm_i] <= '9') - break; - if (ppm_c[ppm_i] == '#') - for (; ppm_i < ppm_size && ppm_c[ppm_i] != '\n'; ppm_i++); - } - for (int i = 0; i < 16; i++) - ppm_buff[i] = 0; - for (int i = 0; ppm_i < ppm_size && i < 15; i++, ppm_i++) { - ppm_buff[i] = ppm_c[ppm_i]; - if (ppm_buff[i] < '0' || ppm_buff[i] > '9') { - ppm_buff[i] = 0; - break; - } - } - if (ppm_i >= ppm_size) { - fprintf(stderr, "broken image file\n"); - return 1; - } - ppm_int[n] = atoi(ppm_buff); - ppm_i++; - } - - if (ppm_int[0] != width || ppm_int[1] != height || ppm_int[2] != 255) { - fprintf(stderr, "unsupported image file\n"); - return 1; - } - - pixel = (uint8_t *)ppm_p + ppm_i; char *pcm_name = "default"; if (argc > 2) pcm_name = argv[2]; if (argc > 3) rate = atoi(argv[3]); - if (!open_pcm_write(&pcm, pcm_name, rate, 1, 37.5)) { - fprintf(stderr, "couldnt open output %s\n", pcm_name); + if (!open_pcm_write(&pcm, pcm_name, rate, 1, 37.5)) return 1; - } rate = rate_pcm(pcm); channels = channels_pcm(pcm); @@ -217,7 +176,7 @@ int main(int argc, char **argv) for (int ticks = 0; ticks < (int)(seq_time[i] * rate); ticks++) add_freq(seq_freq[i]); - for (int y = 0; y < height; y++) { + for (int y = 0; y < img->height; y++) { // EVEN LINES hor_sync(); sync_porch(); @@ -238,7 +197,7 @@ int main(int argc, char **argv) while (add_sample(0.0)); close_pcm(pcm); - munmap_file(ppm_p, ppm_size); + close_img(img); return 0; } diff --git a/img.c b/img.c new file mode 100644 index 0000000..32887d2 --- /dev/null +++ b/img.c @@ -0,0 +1,33 @@ +/* +robot36 - encode and decode images using SSTV in Robot 36 mode +Written in 2011 by +To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. +You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . +*/ + +#include +#include +#include "img.h" +#include "ppm.h" + +void close_img(img_t *img) +{ + img->close(img); + free(img); +} + +int open_img_read(img_t **p, char *name) +{ + if (strstr(name, ".ppm") == (name + (strlen(name) - strlen(".ppm")))) + return open_ppm_read(p, name); + return 0; +} + +/* +int open_img_write(img_t **p, char *name, int width, int height) +{ + if (strstr(name, ".ppm") == (name + (strlen(name) - strlen(".ppm")))) + return open_ppm_write(p, name, width, height); + return 0; +} +*/ diff --git a/img.h b/img.h new file mode 100644 index 0000000..11006d4 --- /dev/null +++ b/img.h @@ -0,0 +1,26 @@ +/* +robot36 - encode and decode images using SSTV in Robot 36 mode +Written in 2011 by +To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. +You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . +*/ + + +#ifndef IMG_H +#define IMG_H + +#include + +typedef struct img { + void (*close)(struct img *); + uint8_t *pixel; + int width; + int height; +} img_t; + +void close_img(img_t *); +int open_img_read(img_t **, char *); +//int open_img_write(img_t **, char *, int, int); + +#endif + diff --git a/ppm.c b/ppm.c new file mode 100644 index 0000000..fcfe44d --- /dev/null +++ b/ppm.c @@ -0,0 +1,92 @@ +/* +robot36 - encode and decode images using SSTV in Robot 36 mode +Written in 2011 by +To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. +You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . +*/ + +#include +#include +#include +#include +#include "mmap_file.h" +#include "img.h" + +typedef struct { + void (*close)(img_t *); + uint8_t *pixel; + int width; + int height; + void *p; + size_t size; +} ppm_t; + +void close_ppm(img_t *img) +{ + ppm_t *ppm = (ppm_t *)img; + munmap_file(ppm->p, ppm->size); +} + +int open_ppm_read(img_t **p, char *name) { + ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); + ppm->close = close_ppm; + + if (!mmap_file_ro(&(ppm->p), name, &(ppm->size))) { + fprintf(stderr, "couldnt open image file %s\n", name); + free(ppm); + return 0; + } + + char *chr = (char *)ppm->p; + size_t index = 0; + if (ppm->size < 10 || chr[index++] != 'P' || chr[index++] != '6') { + fprintf(stderr, "unsupported image file\n"); + munmap_file(ppm->p, ppm->size); + free(ppm); + return 0; + } + + char buff[16]; + int integer[3]; + for (int n = 0; n < 3; n++) { + for (; index < ppm->size; index++) { + if (chr[index] >= '0' && chr[index] <= '9') + break; + if (chr[index] == '#') + for (; index < ppm->size && chr[index] != '\n'; index++); + } + for (int i = 0; i < 16; i++) + buff[i] = 0; + for (int i = 0; index < ppm->size && i < 15; i++, index++) { + buff[i] = chr[index]; + if (buff[i] < '0' || buff[i] > '9') { + buff[i] = 0; + break; + } + } + if (index >= ppm->size) { + fprintf(stderr, "broken image file\n"); + munmap_file(ppm->p, ppm->size); + free(ppm); + return 0; + } + integer[n] = atoi(buff); + index++; + } + + if (0 == integer[0] || 0 == integer[1] || integer[2] != 255) { + fprintf(stderr, "unsupported image file\n"); + munmap_file(ppm->p, ppm->size); + free(ppm); + return 0; + } + ppm->width = integer[0]; + ppm->height = integer[1]; + + ppm->pixel = (uint8_t *)ppm->p + index; + + *p = (img_t *)ppm; + + return 1; +} + diff --git a/ppm.h b/ppm.h new file mode 100644 index 0000000..290e117 --- /dev/null +++ b/ppm.h @@ -0,0 +1,14 @@ +/* +robot36 - encode and decode images using SSTV in Robot 36 mode +Written in 2011 by +To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. +You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . +*/ + + +#ifndef PPM_H +#define PPM_H +#include "img.h" +int open_ppm_read(img_t **, char *); +//int open_ppm_write(img_t **, char *, int, int); +#endif