diff --git a/src/image.c b/src/image.c index 2b4a081..a01c128 100644 --- a/src/image.c +++ b/src/image.c @@ -14,79 +14,64 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA */ -#include -#include - #include "common.h" -#include "bitmap.h" +#include +#include "database.h" #include "image.h" -image_t * -image_from_file(const char *path, const char **errstr) +int image_load(image_t *img, const char *path) { - const char *mimetype; - magic_t magic; - char err[256] = "\0"; - - FILE *in; - gdImagePtr (*cb)(FILE *fd); - image_t *img = NULL; - - CALLOC(img, 1, sizeof(image_t)); - - if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) { - snprintf(err, 256, "%s", magic_error(magic)); - *errstr = err; - FREE(img); - return NULL; - } - - if (magic_load(magic, NULL) < 0) { - *errstr = magic_error(magic); - magic_close(magic); - FREE(img); - return NULL; - } - - mimetype = magic_file(magic, path); - if (mimetype == NULL) { - snprintf(err, 256, "%s", magic_error(magic)); - *errstr = err; - magic_close(magic); - FREE(img); - return NULL; - } - - if (strncmp(mimetype, "image/png", 9) == 0) { - cb = gdImageCreateFromPng; - } else - if (strncmp(mimetype, "image/gif", 9) == 0) { - cb = gdImageCreateFromGif; - } else - if (strncmp(mimetype, "image/jpeg", 10) == 0) { - cb = gdImageCreateFromJpeg; - } else { - snprintf(err, 256, "Can't handle image type '%s'", mimetype); - *errstr = err; - FREE(img); - return NULL; - } - STRNDUP(img->mime, mimetype, 16); - magic_close(magic); - - in = fopen(path, "rb"); - if (in == NULL) { - snprintf(err, 256, "Can't open file: %s", strerror(errno)); - *errstr = err; - FREE(img); - return NULL; - } - - img->data = cb(in); - fclose(in); - - img->res_x = gdImageSX(img->data); - img->res_y = gdImageSY(img->data); - - return img; + ExceptionInfo ex; + + GetExceptionInfo(&ex); + + img.info = CloneImageInfo((ImageInfo *) NULL); + strcpy(img.info->filename, argv[1]); + + img.data = ReadImage(img.info, &ex); + + if (ex.severity != UndefinedException) + CatchException(&ex); + + return 0; +} + +int image_sample(rec_t *sample, image_t *img) +{ + ExceptionInfo ex; + QuantizeInfo qi; + + ImageInfo info; + Image *sampled; + Image *blurred; + + GetExceptionInfo(&ex); + + memset(img, 0x0, sizeof(image_t)); + + sampled = SampleImage(img.data, 160, 160, &ex); + + GetQuantizeInfo(&qi); + QuantizeImage(&qi, sampled) + + blurred = BlurImage(sampled, 3.0, 99, &ex); /* reduce sigma? */ + DestroyImage(sampled); + + ret = GetImageStatistics(blurred, &stat, &ex); + + NormalizeImage(blurred); + EqualizeImage(blurred); + + sampled = SampleImage(img.data, 16, 16, &ex); + DestroyImage(blurred); + + ret = ThresholdImage(sampled, /* const double threshold */ 0); + + GetImageInfo(&info); + + SetImageAttribute(sampled, "magick", "mono"); + SetImageType(sampled, BilevelType); + (sample.data + OFF_BITMAP) = ImageToBlob(&info, sampled, BITMAP_SIZE, &ex); + + return 0; } diff --git a/src/image.h b/src/image.h index 4805cb0..1db1c9c 100644 --- a/src/image.h +++ b/src/image.h @@ -1,15 +1,12 @@ #ifndef HAS_IMAGE_H typedef struct { - gdImagePtr data; - uint16_t res_x; - uint16_t res_y; - char *mime; + ImageInfo *info + Image *data; + const char *errstr; } image_t; -/** - * @returns: image_t * on success, NULL otherwise - */ -image_t * -image_from_file(const char *path, const char **errstr); + +int image_load(image_t *img, const char *path); + #endif #define HAS_IMAGE_H 1 diff --git a/src/test-image.c b/src/test-image.c deleted file mode 100644 index 79eb0e5..0000000 --- a/src/test-image.c +++ /dev/null @@ -1,18 +0,0 @@ -#include - -#include "common.h" -#include "bitmap.h" -#include "image.h" - -int main(int argc, char **argv) -{ - image_t *img = NULL; - const char *err = NULL; - - img = image_from_file("test.png", &err); - if (img == NULL) { - printf("%s\n", err); - exit(EXIT_FAILURE); - } - exit(EXIT_SUCCESS); -}