Browse Source

* image.[ch]

master
Alex 'AdUser' Z 11 years ago
parent
commit
c502c145b6
  1. 127
      src/image.c
  2. 15
      src/image.h
  3. 18
      src/test-image.c

127
src/image.c

@ -14,79 +14,64 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/ */
#include <gd.h>
#include <magic.h>
#include "common.h" #include "common.h"
#include "bitmap.h" #include <magick/api.h>
#include "database.h"
#include "image.h" #include "image.h"
image_t * int image_load(image_t *img, const char *path)
image_from_file(const char *path, const char **errstr)
{ {
const char *mimetype; ExceptionInfo ex;
magic_t magic;
char err[256] = "\0"; GetExceptionInfo(&ex);
FILE *in; img.info = CloneImageInfo((ImageInfo *) NULL);
gdImagePtr (*cb)(FILE *fd); strcpy(img.info->filename, argv[1]);
image_t *img = NULL;
img.data = ReadImage(img.info, &ex);
CALLOC(img, 1, sizeof(image_t));
if (ex.severity != UndefinedException)
if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) { CatchException(&ex);
snprintf(err, 256, "%s", magic_error(magic));
*errstr = err; return 0;
FREE(img); }
return NULL;
} int image_sample(rec_t *sample, image_t *img)
{
if (magic_load(magic, NULL) < 0) { ExceptionInfo ex;
*errstr = magic_error(magic); QuantizeInfo qi;
magic_close(magic);
FREE(img); ImageInfo info;
return NULL; Image *sampled;
} Image *blurred;
mimetype = magic_file(magic, path); GetExceptionInfo(&ex);
if (mimetype == NULL) {
snprintf(err, 256, "%s", magic_error(magic)); memset(img, 0x0, sizeof(image_t));
*errstr = err;
magic_close(magic); sampled = SampleImage(img.data, 160, 160, &ex);
FREE(img);
return NULL; GetQuantizeInfo(&qi);
} QuantizeImage(&qi, sampled)
if (strncmp(mimetype, "image/png", 9) == 0) { blurred = BlurImage(sampled, 3.0, 99, &ex); /* reduce sigma? */
cb = gdImageCreateFromPng; DestroyImage(sampled);
} else
if (strncmp(mimetype, "image/gif", 9) == 0) { ret = GetImageStatistics(blurred, &stat, &ex);
cb = gdImageCreateFromGif;
} else NormalizeImage(blurred);
if (strncmp(mimetype, "image/jpeg", 10) == 0) { EqualizeImage(blurred);
cb = gdImageCreateFromJpeg;
} else { sampled = SampleImage(img.data, 16, 16, &ex);
snprintf(err, 256, "Can't handle image type '%s'", mimetype); DestroyImage(blurred);
*errstr = err;
FREE(img); ret = ThresholdImage(sampled, /* const double threshold */ 0);
return NULL;
} GetImageInfo(&info);
STRNDUP(img->mime, mimetype, 16);
magic_close(magic); SetImageAttribute(sampled, "magick", "mono");
SetImageType(sampled, BilevelType);
in = fopen(path, "rb"); (sample.data + OFF_BITMAP) = ImageToBlob(&info, sampled, BITMAP_SIZE, &ex);
if (in == NULL) {
snprintf(err, 256, "Can't open file: %s", strerror(errno)); return 0;
*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;
} }

15
src/image.h

@ -1,15 +1,12 @@
#ifndef HAS_IMAGE_H #ifndef HAS_IMAGE_H
typedef struct typedef struct
{ {
gdImagePtr data; ImageInfo *info
uint16_t res_x; Image *data;
uint16_t res_y; const char *errstr;
char *mime;
} image_t; } image_t;
/**
* @returns: image_t * on success, NULL otherwise int image_load(image_t *img, const char *path);
*/
image_t *
image_from_file(const char *path, const char **errstr);
#endif #endif
#define HAS_IMAGE_H 1 #define HAS_IMAGE_H 1

18
src/test-image.c

@ -1,18 +0,0 @@
#include <gd.h>
#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);
}
Loading…
Cancel
Save