From 1b875f6d70461afe0526ee90ba19211bd856b254 Mon Sep 17 00:00:00 2001 From: phani Date: Mon, 30 Dec 2019 17:52:13 -0500 Subject: [PATCH] read from stdin, write to stdout --- get_time.h | 2 +- samples/license_plate_detection.c | 59 ++++++++++++++++----------- samples/resize_image.c | 66 ++++++++++++++++++------------- sod_img_writer.h | 12 +++--- 4 files changed, 81 insertions(+), 58 deletions(-) diff --git a/get_time.h b/get_time.h index 89e5d71..8312a56 100644 --- a/get_time.h +++ b/get_time.h @@ -37,7 +37,7 @@ print_time(unsigned long long s, unsigned long long e) #if 0 printf("%llu cycs, %llu us\n", e - s, (e - s) / CPU_FREQ); #else - printf("%llu us\n", e - s); + fprintf(stderr, "%llu us\n", e - s); #endif } diff --git a/samples/license_plate_detection.c b/samples/license_plate_detection.c index d052036..53f60ad 100644 --- a/samples/license_plate_detection.c +++ b/samples/license_plate_detection.c @@ -49,37 +49,49 @@ static int filter_cb(int width, int height) } return 1; /* Accepted region */ } + +#define MAX_IMG_SIZE (1024*1024) + int main(int argc, char *argv[]) { - struct stat stbf; +// struct stat stbf; unsigned char *zInpbuf = NULL; unsigned long long s = get_time(), e; /* Input image (pass a path or use the test image shipped with the samples ZIP archive) */ - const char *zInput = argc > 1 ? argv[1] : "./plate.jpg"; - /* Processed output image path */ - const char *zOut = argc > 2 ? argv[2] : "./out_plate.jpg"; - int r = stat(zInput, &stbf); - if (r < 0) { - perror("stat"); - return -1; - } - zInpbuf = malloc(stbf.st_size); - //memset(zInpbuf, 0, stbf.st_size); - int zIfd = open(zInput, O_RDONLY); - if (zIfd < 0) { - perror("open"); - return -1; - } - r = read(zIfd, zInpbuf, stbf.st_size); - if (r < stbf.st_size) { - perror("read"); +// const char *zInput = argc > 1 ? argv[1] : "./plate.jpg"; +// /* Processed output image path */ +// const char *zOut = argc > 2 ? argv[2] : "./out_plate.jpg"; +// int r = stat(zInput, &stbf); +// if (r < 0) { +// perror("stat"); +// return -1; +// } +// zInpbuf = malloc(stbf.st_size); +// //memset(zInpbuf, 0, stbf.st_size); +// int zIfd = open(zInput, O_RDONLY); +// if (zIfd < 0) { +// perror("open"); +// return -1; +// } +// r = read(zIfd, zInpbuf, stbf.st_size); +// if (r < stbf.st_size) { +// perror("read"); +// return -1; +// } +// close(zIfd); + zInpbuf = malloc(MAX_IMG_SIZE); + if (!zInpbuf) return -1; + + size_t zImgSz = read(0, zInpbuf, MAX_IMG_SIZE); + if (zImgSz <= 0) { + if (zImgSz < 0) perror("read"); + free(zInpbuf); return -1; } - close(zIfd); /* Load the input image in the grayscale colorspace */ //sod_img imgIn = sod_img_load_grayscale(zInput); - sod_img imgIn = sod_img_load_from_mem(zInpbuf, stbf.st_size, SOD_IMG_GRAYSCALE); + sod_img imgIn = sod_img_load_from_mem(zInpbuf, zImgSz, SOD_IMG_GRAYSCALE); if (imgIn.data == 0) { /* Invalid path, unsupported format, memory failure, etc. */ puts("Cannot load input image..exiting"); @@ -89,7 +101,7 @@ int main(int argc, char *argv[]) * marking the plate in question if any. */ //sod_img imgCopy = sod_img_load_color(zInput); - sod_img imgCopy = sod_img_load_from_mem(zInpbuf, stbf.st_size, SOD_IMG_COLOR); + sod_img imgCopy = sod_img_load_from_mem(zInpbuf, zImgSz, SOD_IMG_COLOR); /* Obtain a binary image first */ sod_img binImg = sod_threshold_image(imgIn, 0.5); /* @@ -115,13 +127,14 @@ int main(int argc, char *argv[]) } sod_image_blob_boxes_release(box); /* Finally save the output image to the specified path */ - sod_img_save_as_jpeg(imgCopy, zOut, 0); + sod_img_save_as_jpeg(imgCopy, NULL, 0); /* Cleanup */ sod_free_image(imgIn); sod_free_image(cannyImg); sod_free_image(binImg); sod_free_image(dilImg); sod_free_image(imgCopy); + free(zInpbuf); e = get_time(); print_time(s, e); return 0; diff --git a/samples/resize_image.c b/samples/resize_image.c index c910aa6..0aa97c7 100644 --- a/samples/resize_image.c +++ b/samples/resize_image.c @@ -31,44 +31,53 @@ #include #include "sod.h" #include "get_time.h" +#define MAX_IMG_SZ (1024*1024) //1MB /* * Resize an image (Minify) to half its original size. */ int main(int argc, char *argv[]) { - struct stat stbf; +// struct stat stbf; + size_t imgSz = 0; unsigned char *zInpbuf = NULL; unsigned long long s = get_time(), e; - /* Input image (pass a path or use the test image shipped with the samples ZIP archive) */ - const char *zInput = argc > 1 ? argv[1] : "./flower.jpg"; - /* Processed output image path */ - const char *zOut = argc > 2 ? argv[2] : "./out_rz.png"; - /* Load the input image in full color */ - int r = stat(zInput, &stbf); - if (r < 0) { - perror("stat"); - return -1; - } - zInpbuf = malloc(stbf.st_size); - //memset(zInpbuf, 0, stbf.st_size); - int zIfd = open(zInput, O_RDONLY); - if (zIfd < 0) { - perror("open"); - return -1; - } - r = read(zIfd, zInpbuf, stbf.st_size); - if (r < stbf.st_size) { - perror("read"); - return -1; - } - close(zIfd); +// /* Input image (pass a path or use the test image shipped with the samples ZIP archive) */ +// const char *zInput = argc > 1 ? argv[1] : "./flower.jpg"; +// /* Processed output image path */ +// const char *zOut = argc > 2 ? argv[2] : "./out_rz.png"; +// /* Load the input image in full color */ +// int r = stat(zInput, &stbf); +// if (r < 0) { +// perror("stat"); +// return -1; +// } +// zInpbuf = malloc(stbf.st_size); +// //memset(zInpbuf, 0, stbf.st_size); +// int zIfd = open(zInput, O_RDONLY); +// if (zIfd < 0) { +// perror("open"); +// return -1; +// } +// r = read(zIfd, zInpbuf, stbf.st_size); +// if (r < stbf.st_size) { +// perror("read"); +// return -1; +// } +// close(zIfd); + zInpbuf = malloc(MAX_IMG_SZ); + if (!zInpbuf) return -1; + imgSz = read(0, zInpbuf, MAX_IMG_SZ); + if (imgSz <= 0) return -1; + // sod_img imgIn = sod_img_load_from_file(zInput, SOD_IMG_COLOR /* full color channels */); - sod_img imgIn = sod_img_load_from_mem(zInpbuf, stbf.st_size, SOD_IMG_COLOR /* full color channels */); + sod_img imgIn = sod_img_load_from_mem(zInpbuf, imgSz, SOD_IMG_COLOR /* full color channels */); if (imgIn.data == 0) { /* Invalid path, unsupported format, memory failure, etc. */ - puts("Cannot load input image..exiting"); - return 0; + //puts("Cannot load input image..exiting"); + printf("Error loading input\n"); + free(zInpbuf); + return -1; } /* Resize to half its original size */ int newWidth = imgIn.w / 2; @@ -76,11 +85,12 @@ int main(int argc, char *argv[]) sod_img rz = sod_resize_image(imgIn, newWidth, newHeight); /* Save the resized image to the specified path */ - sod_img_save_as_png(rz, zOut); + sod_img_save_as_png(rz, NULL); //sod_img_save_as_jpeg(rz, zOut, 0); /* Cleanup */ sod_free_image(imgIn); sod_free_image(rz); + free(zInpbuf); e = get_time(); print_time(s, e); return 0; diff --git a/sod_img_writer.h b/sod_img_writer.h index 4de62da..270008e 100644 --- a/sod_img_writer.h +++ b/sod_img_writer.h @@ -293,12 +293,12 @@ static void stbi__stdio_write(void *context, void *data, int size) static int stbi__start_write_file(stbi__write_context *s, const char *filename) { - FILE *f; + FILE *f = stdout; #ifdef STBI_MSC_SECURE_CRT - if (fopen_s(&f, filename, "wb")) + if (filename && fopen_s(&f, filename, "wb")) f = NULL; #else - f = fopen(filename, "wb"); + if (filename) f = fopen(filename, "wb"); #endif stbi__start_write_callbacks(s, stbi__stdio_write, (void *)f); return f != NULL; @@ -1137,15 +1137,15 @@ unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, in #ifndef STBI_WRITE_NO_STDIO STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) { - FILE *f; + FILE *f = stdout; int len; unsigned char *png = stbi_write_png_to_mem((unsigned char *)data, stride_bytes, x, y, comp, &len); if (png == NULL) return 0; #ifdef STBI_MSC_SECURE_CRT - if (fopen_s(&f, filename, "wb")) + if (filename && fopen_s(&f, filename, "wb")) f = NULL; #else - f = fopen(filename, "wb"); + if (filename) f = fopen(filename, "wb"); #endif if (!f) { STBIW_FREE(png); return 0; } fwrite(png, 1, len, f);