read from stdin, write to stdout

main
phani 5 years ago
parent 03f70a904b
commit 1b875f6d70

@ -37,7 +37,7 @@ print_time(unsigned long long s, unsigned long long e)
#if 0 #if 0
printf("%llu cycs, %llu us\n", e - s, (e - s) / CPU_FREQ); printf("%llu cycs, %llu us\n", e - s, (e - s) / CPU_FREQ);
#else #else
printf("%llu us\n", e - s); fprintf(stderr, "%llu us\n", e - s);
#endif #endif
} }

@ -49,37 +49,49 @@ static int filter_cb(int width, int height)
} }
return 1; /* Accepted region */ return 1; /* Accepted region */
} }
#define MAX_IMG_SIZE (1024*1024)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct stat stbf; // struct stat stbf;
unsigned char *zInpbuf = NULL; unsigned char *zInpbuf = NULL;
unsigned long long s = get_time(), e; unsigned long long s = get_time(), e;
/* Input image (pass a path or use the test image shipped with the samples ZIP archive) */ /* 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"; // const char *zInput = argc > 1 ? argv[1] : "./plate.jpg";
/* Processed output image path */ // /* Processed output image path */
const char *zOut = argc > 2 ? argv[2] : "./out_plate.jpg"; // const char *zOut = argc > 2 ? argv[2] : "./out_plate.jpg";
int r = stat(zInput, &stbf); // int r = stat(zInput, &stbf);
if (r < 0) { // if (r < 0) {
perror("stat"); // perror("stat");
return -1; // return -1;
} // }
zInpbuf = malloc(stbf.st_size); // zInpbuf = malloc(stbf.st_size);
//memset(zInpbuf, 0, stbf.st_size); // //memset(zInpbuf, 0, stbf.st_size);
int zIfd = open(zInput, O_RDONLY); // int zIfd = open(zInput, O_RDONLY);
if (zIfd < 0) { // if (zIfd < 0) {
perror("open"); // perror("open");
return -1; // return -1;
} // }
r = read(zIfd, zInpbuf, stbf.st_size); // r = read(zIfd, zInpbuf, stbf.st_size);
if (r < stbf.st_size) { // if (r < stbf.st_size) {
perror("read"); // 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; return -1;
} }
close(zIfd);
/* Load the input image in the grayscale colorspace */ /* Load the input image in the grayscale colorspace */
//sod_img imgIn = sod_img_load_grayscale(zInput); //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) { if (imgIn.data == 0) {
/* Invalid path, unsupported format, memory failure, etc. */ /* Invalid path, unsupported format, memory failure, etc. */
puts("Cannot load input image..exiting"); puts("Cannot load input image..exiting");
@ -89,7 +101,7 @@ int main(int argc, char *argv[])
* marking the plate in question if any. * marking the plate in question if any.
*/ */
//sod_img imgCopy = sod_img_load_color(zInput); //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 */ /* Obtain a binary image first */
sod_img binImg = sod_threshold_image(imgIn, 0.5); 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); sod_image_blob_boxes_release(box);
/* Finally save the output image to the specified path */ /* 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 */ /* Cleanup */
sod_free_image(imgIn); sod_free_image(imgIn);
sod_free_image(cannyImg); sod_free_image(cannyImg);
sod_free_image(binImg); sod_free_image(binImg);
sod_free_image(dilImg); sod_free_image(dilImg);
sod_free_image(imgCopy); sod_free_image(imgCopy);
free(zInpbuf);
e = get_time(); e = get_time();
print_time(s, e); print_time(s, e);
return 0; return 0;

@ -31,44 +31,53 @@
#include <stdlib.h> #include <stdlib.h>
#include "sod.h" #include "sod.h"
#include "get_time.h" #include "get_time.h"
#define MAX_IMG_SZ (1024*1024) //1MB
/* /*
* Resize an image (Minify) to half its original size. * Resize an image (Minify) to half its original size.
*/ */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct stat stbf; // struct stat stbf;
size_t imgSz = 0;
unsigned char *zInpbuf = NULL; unsigned char *zInpbuf = NULL;
unsigned long long s = get_time(), e; unsigned long long s = get_time(), e;
/* Input image (pass a path or use the test image shipped with the samples ZIP archive) */ // /* 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"; // const char *zInput = argc > 1 ? argv[1] : "./flower.jpg";
/* Processed output image path */ // /* Processed output image path */
const char *zOut = argc > 2 ? argv[2] : "./out_rz.png"; // const char *zOut = argc > 2 ? argv[2] : "./out_rz.png";
/* Load the input image in full color */ // /* Load the input image in full color */
int r = stat(zInput, &stbf); // int r = stat(zInput, &stbf);
if (r < 0) { // if (r < 0) {
perror("stat"); // perror("stat");
return -1; // return -1;
} // }
zInpbuf = malloc(stbf.st_size); // zInpbuf = malloc(stbf.st_size);
//memset(zInpbuf, 0, stbf.st_size); // //memset(zInpbuf, 0, stbf.st_size);
int zIfd = open(zInput, O_RDONLY); // int zIfd = open(zInput, O_RDONLY);
if (zIfd < 0) { // if (zIfd < 0) {
perror("open"); // perror("open");
return -1; // return -1;
} // }
r = read(zIfd, zInpbuf, stbf.st_size); // r = read(zIfd, zInpbuf, stbf.st_size);
if (r < stbf.st_size) { // if (r < stbf.st_size) {
perror("read"); // perror("read");
return -1; // return -1;
} // }
close(zIfd); // 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_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) { if (imgIn.data == 0) {
/* Invalid path, unsupported format, memory failure, etc. */ /* Invalid path, unsupported format, memory failure, etc. */
puts("Cannot load input image..exiting"); //puts("Cannot load input image..exiting");
return 0; printf("Error loading input\n");
free(zInpbuf);
return -1;
} }
/* Resize to half its original size */ /* Resize to half its original size */
int newWidth = imgIn.w / 2; int newWidth = imgIn.w / 2;
@ -76,11 +85,12 @@ int main(int argc, char *argv[])
sod_img rz = sod_resize_image(imgIn, newWidth, newHeight); sod_img rz = sod_resize_image(imgIn, newWidth, newHeight);
/* Save the resized image to the specified path */ /* 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); //sod_img_save_as_jpeg(rz, zOut, 0);
/* Cleanup */ /* Cleanup */
sod_free_image(imgIn); sod_free_image(imgIn);
sod_free_image(rz); sod_free_image(rz);
free(zInpbuf);
e = get_time(); e = get_time();
print_time(s, e); print_time(s, e);
return 0; return 0;

@ -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) static int stbi__start_write_file(stbi__write_context *s, const char *filename)
{ {
FILE *f; FILE *f = stdout;
#ifdef STBI_MSC_SECURE_CRT #ifdef STBI_MSC_SECURE_CRT
if (fopen_s(&f, filename, "wb")) if (filename && fopen_s(&f, filename, "wb"))
f = NULL; f = NULL;
#else #else
f = fopen(filename, "wb"); if (filename) f = fopen(filename, "wb");
#endif #endif
stbi__start_write_callbacks(s, stbi__stdio_write, (void *)f); stbi__start_write_callbacks(s, stbi__stdio_write, (void *)f);
return f != NULL; 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 #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) 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; int len;
unsigned char *png = stbi_write_png_to_mem((unsigned char *)data, stride_bytes, x, y, comp, &len); unsigned char *png = stbi_write_png_to_mem((unsigned char *)data, stride_bytes, x, y, comp, &len);
if (png == NULL) return 0; if (png == NULL) return 0;
#ifdef STBI_MSC_SECURE_CRT #ifdef STBI_MSC_SECURE_CRT
if (fopen_s(&f, filename, "wb")) if (filename && fopen_s(&f, filename, "wb"))
f = NULL; f = NULL;
#else #else
f = fopen(filename, "wb"); if (filename) f = fopen(filename, "wb");
#endif #endif
if (!f) { STBIW_FREE(png); return 0; } if (!f) { STBIW_FREE(png); return 0; }
fwrite(png, 1, len, f); fwrite(png, 1, len, f);

Loading…
Cancel
Save