diff --git a/Makefile b/Makefile index 2d6aee7..f969650 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ clean: 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 +decode: decode.o mmap_file.o pcm.o wav.o alsa.o window.o ddc.o delay.o yuv.o img.o ppm.o -debug: debug.o mmap_file.o pcm.o wav.o alsa.o window.o ddc.o delay.o yuv.o +debug: debug.o mmap_file.o pcm.o wav.o alsa.o window.o ddc.o delay.o yuv.o img.o ppm.o diff --git a/debug.c b/debug.c index 63a79b0..f4200b7 100644 --- a/debug.c +++ b/debug.c @@ -18,16 +18,17 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include "delay.h" #include "yuv.h" #include "utils.h" +#include "img.h" int main(int argc, char **argv) { pcm_t *pcm; char *pcm_name = "default"; - char *ppm_name = 0; + char *img_name = 0; if (argc != 1) pcm_name = argv[1]; if (argc == 3) - ppm_name = argv[2]; + img_name = argv[2]; if (!open_pcm_read(&pcm, pcm_name)) { fprintf(stderr, "couldnt open %s\n", pcm_name); @@ -129,12 +130,7 @@ int main(int argc, char **argv) const int width = (0.150 + 3.0 * sync_porch_len) * drate + 20; const int height = 256; - - char ppm_head[32]; - snprintf(ppm_head, 32, "P6 %d %d 255\n", width, height); - size_t ppm_size = strlen(ppm_head) + width * height * 3; - void *ppm_p = 0; - uint8_t *pixel = 0; + img_t *img; int hor_ticks = 0; int y_pixel_x = 0; @@ -278,24 +274,24 @@ int main(int argc, char **argv) uv_pixel_x = 0; y = 0; odd = 0; - if (pixel) { - munmap_file(ppm_p, ppm_size); + if (img) { + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); missing_sync = 0; seperator_correction = 0; } - if (ppm_name) - mmap_file_rw(&ppm_p, ppm_name, ppm_size); - else - mmap_file_rw(&ppm_p, string_time("%F_%T.ppm"), ppm_size); - memcpy(ppm_p, ppm_head, strlen(ppm_head)); - pixel = (uint8_t *)ppm_p + strlen(ppm_head); - memset(pixel, 0, width * height * 3); + if (img_name) { + if (!open_img_write(&img, img_name, width, height)) + return 1; + } else { + if (!open_img_write(&img, string_time("%F_%T.ppm"), width, height)) + return 1; + } continue; } if (hor_ticks < width) { - uint8_t *p = pixel + 3 * y * width + 3 * hor_ticks; + uint8_t *p = img->pixel + 3 * y * width + 3 * hor_ticks; #if 1 uint8_t v = fclampf(0.0, 255.0, 255.0 * (dat_freq - 1500.0) / 800.0); #else @@ -309,7 +305,7 @@ int main(int argc, char **argv) // if horizontal sync is too early, we reset to the beginning instead of ignoring if (hor_sync && hor_ticks < (int)((hor_len - sync_porch_len) * drate)) { for (int i = 0; i < 4; i++) { - uint8_t *p = pixel + 3 * y * width + 3 * (width - i - 10); + uint8_t *p = img->pixel + 3 * y * width + 3 * (width - i - 10); p[0] = 255; p[1] = 0; p[2] = 255; @@ -322,15 +318,15 @@ int main(int argc, char **argv) // we always sync if sync pulse is where it should be. if (hor_sync && (hor_ticks >= (int)((hor_len - sync_porch_len) * drate) && hor_ticks < (int)((hor_len + sync_porch_len) * drate))) { - uint8_t *p = pixel + 3 * y * width + 3 * hor_ticks + 6 * (int)(sync_porch_len * drate); + uint8_t *p = img->pixel + 3 * y * width + 3 * hor_ticks + 6 * (int)(sync_porch_len * drate); p[0] = 0; p[1] = 255; p[2] = 255; y++; if (y == height) { - munmap_file(ppm_p, ppm_size); + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); - pixel = 0; + img = 0; dat_mode = 0; missing_sync = 0; seperator_correction = 0; @@ -345,16 +341,16 @@ int main(int argc, char **argv) // if horizontal sync is missing, we extrapolate from last sync if (hor_ticks >= (int)((hor_len + sync_porch_len) * drate)) { for (int i = 0; i < 4; i++) { - uint8_t *p = pixel + 3 * y * width + 3 * (width - i - 5); + uint8_t *p = img->pixel + 3 * y * width + 3 * (width - i - 5); p[0] = 255; p[1] = 255; p[2] = 0; } y++; if (y == height) { - munmap_file(ppm_p, ppm_size); + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); - pixel = 0; + img = 0; dat_mode = 0; missing_sync = 0; seperator_correction = 0; @@ -379,7 +375,7 @@ int main(int argc, char **argv) odd = 0; seperator_correction++; for (int i = 0; i < 4; i++) { - uint8_t *p = pixel + 3 * y * width + 3 * (width - i - 15); + uint8_t *p = img->pixel + 3 * y * width + 3 * (width - i - 15); p[0] = 255; p[1] = 0; p[2] = 0; @@ -390,7 +386,7 @@ int main(int argc, char **argv) odd = 1; seperator_correction++; for (int i = 0; i < 4; i++) { - uint8_t *p = pixel + 3 * y * width + 3 * (width - i - 15); + uint8_t *p = img->pixel + 3 * y * width + 3 * (width - i - 15); p[0] = 0; p[1] = 255; p[2] = 0; @@ -406,15 +402,15 @@ int main(int argc, char **argv) hor_ticks == (int)((fixme + sync_porch_len + y_len + seperator_len) * drate) || hor_ticks == (int)((fixme + sync_porch_len + y_len + seperator_len + porch_len) * drate) || hor_ticks == (int)((fixme + sync_porch_len + y_len + seperator_len + porch_len + uv_len) * drate)) { - uint8_t *p = pixel + 3 * y * width + 3 * hor_ticks; + uint8_t *p = img->pixel + 3 * y * width + 3 * hor_ticks; p[0] = 255; p[1] = 0; p[2] = 0; } } - if (pixel) { - munmap_file(ppm_p, ppm_size); + if (img) { + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); missing_sync = 0; seperator_correction = 0; diff --git a/decode.c b/decode.c index b730c95..4d4a8b6 100644 --- a/decode.c +++ b/decode.c @@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include "delay.h" #include "yuv.h" #include "utils.h" +#include "img.h" void process_line(uint8_t *pixel, uint8_t *y_pixel, uint8_t *uv_pixel, int y_width, int uv_width, int width, int height, int n) { @@ -52,11 +53,11 @@ int main(int argc, char **argv) { pcm_t *pcm; char *pcm_name = "default"; - char *ppm_name = 0; + char *img_name = 0; if (argc != 1) pcm_name = argv[1]; if (argc == 3) - ppm_name = argv[2]; + img_name = argv[2]; if (!open_pcm_read(&pcm, pcm_name)) return 1; @@ -156,12 +157,7 @@ int main(int argc, char **argv) const int width = 320; const int height = 240; - - char ppm_head[32]; - snprintf(ppm_head, 32, "P6 %d %d 255\n", width, height); - size_t ppm_size = strlen(ppm_head) + width * height * 3; - void *ppm_p = 0; - uint8_t *pixel = 0; + img_t *img; int hor_ticks = 0; int y_pixel_x = 0; @@ -298,26 +294,26 @@ int main(int argc, char **argv) uv_pixel_x = 0; y = 0; odd = 0; - if (pixel) { - munmap_file(ppm_p, ppm_size); + if (img) { + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); missing_sync = 0; seperator_correction = 0; } - if (ppm_name) - mmap_file_rw(&ppm_p, ppm_name, ppm_size); - else - mmap_file_rw(&ppm_p, string_time("%F_%T.ppm"), ppm_size); - memcpy(ppm_p, ppm_head, strlen(ppm_head)); - pixel = (uint8_t *)ppm_p + strlen(ppm_head); - memset(pixel, 0, width * height * 3); + if (img_name) { + if (!open_img_write(&img, img_name, width, height)) + return 1; + } else { + if (!open_img_write(&img, string_time("%F_%T.ppm"), width, height)) + return 1; + } continue; } // if horizontal sync is too early, we reset to the beginning instead of ignoring if (hor_sync && hor_ticks < (int)((hor_len - sync_porch_len) * drate)) { for (int i = 0; i < 4; i++) { - uint8_t *p = pixel + 3 * y * width + 3 * (width - i - 10); + uint8_t *p = img->pixel + 3 * y * width + 3 * (width - i - 10); p[0] = 255; p[1] = 0; p[2] = 255; @@ -330,11 +326,11 @@ int main(int argc, char **argv) // we always sync if sync pulse is where it should be. if (hor_sync && (hor_ticks >= (int)((hor_len - sync_porch_len) * drate) && hor_ticks < (int)((hor_len + sync_porch_len) * drate))) { - process_line(pixel, y_pixel, uv_pixel, y_width, uv_width, width, height, y++); + process_line(img->pixel, y_pixel, uv_pixel, y_width, uv_width, width, height, y++); if (y == height) { - munmap_file(ppm_p, ppm_size); + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); - pixel = 0; + img = 0; dat_mode = 0; missing_sync = 0; seperator_correction = 0; @@ -348,11 +344,11 @@ int main(int argc, char **argv) // if horizontal sync is missing, we extrapolate from last sync if (hor_ticks >= (int)((hor_len + sync_porch_len) * drate)) { - process_line(pixel, y_pixel, uv_pixel, y_width, uv_width, width, height, y++); + process_line(img->pixel, y_pixel, uv_pixel, y_width, uv_width, width, height, y++); if (y == height) { - munmap_file(ppm_p, ppm_size); + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); - pixel = 0; + img = 0; dat_mode = 0; missing_sync = 0; seperator_correction = 0; @@ -394,8 +390,8 @@ int main(int argc, char **argv) uv_pixel[uv_pixel_x++ + odd * uv_width] = fclampf(255.0 * (dat_freq - 1500.0) / 800.0, 0.0, 255.0); } - if (pixel) { - munmap_file(ppm_p, ppm_size); + if (img) { + close_img(img); fprintf(stderr, "%d missing sync's and %d corrections from seperator\n", missing_sync, seperator_correction); missing_sync = 0; seperator_correction = 0; diff --git a/img.c b/img.c index 32887d2..0507dd8 100644 --- a/img.c +++ b/img.c @@ -23,11 +23,10 @@ int open_img_read(img_t **p, char *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 index 11006d4..c31117e 100644 --- a/img.h +++ b/img.h @@ -20,7 +20,7 @@ typedef struct img { void close_img(img_t *); int open_img_read(img_t **, char *); -//int open_img_write(img_t **, char *, int, int); +int open_img_write(img_t **, char *, int, int); #endif diff --git a/ppm.c b/ppm.c index fcfe44d..8dfee4d 100644 --- a/ppm.c +++ b/ppm.c @@ -90,3 +90,27 @@ int open_ppm_read(img_t **p, char *name) { return 1; } +int open_ppm_write(img_t **p, char *name, int width, int height) +{ + ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); + ppm->close = close_ppm; + + char head[32]; + snprintf(head, 32, "P6 %d %d 255\n", width, height); + ppm->size = strlen(head) + width * height * 3; + + if (!mmap_file_rw(&(ppm->p), name, ppm->size)) { + fprintf(stderr, "couldnt open image file %s\n", name); + free(ppm); + return 0; + } + + memcpy(ppm->p, head, strlen(head)); + ppm->pixel = (uint8_t *)ppm->p + strlen(head); + memset(ppm->pixel, 0, width * height * 3); + + *p = (img_t *)ppm; + + return 1; +} + diff --git a/ppm.h b/ppm.h index 290e117..921b7da 100644 --- a/ppm.h +++ b/ppm.h @@ -10,5 +10,5 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #define PPM_H #include "img.h" int open_ppm_read(img_t **, char *); -//int open_ppm_write(img_t **, char *, int, int); +int open_ppm_write(img_t **, char *, int, int); #endif