You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.0 KiB
113 lines
3.0 KiB
7 years ago
|
/* Copyright 2014-2017 Alex 'AdUser' Z (ad_user@runbox.com)
|
||
|
*
|
||
|
* This file is part of libsimdb
|
||
|
*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License version 2 as
|
||
|
* published by the Free Software Foundation.
|
||
|
*/
|
||
|
|
||
7 years ago
|
#include "../common.h"
|
||
|
#include "../bitmap.h"
|
||
|
#include "../record.h"
|
||
7 years ago
|
#include "../simdb.h"
|
||
9 years ago
|
|
||
|
#include <wand/magick_wand.h>
|
||
|
|
||
7 years ago
|
simdb_urec_t *
|
||
|
simdb_record_create(const char * const path) {
|
||
9 years ago
|
MagickWand *wand = NULL;
|
||
|
MagickPassFail status = MagickPass;
|
||
9 years ago
|
uint16_t w = 0, h = 0;
|
||
9 years ago
|
size_t buf_size = 64 * sizeof(char);
|
||
|
unsigned char *buf = NULL;
|
||
7 years ago
|
simdb_urec_t *rec = NULL;
|
||
9 years ago
|
|
||
7 years ago
|
assert(path != NULL);
|
||
7 years ago
|
|
||
7 years ago
|
InitializeMagick("/");
|
||
9 years ago
|
wand = NewMagickWand();
|
||
|
if (status == MagickPass)
|
||
7 years ago
|
status = MagickReadImage(wand, path);
|
||
9 years ago
|
|
||
|
if (status == MagickPass)
|
||
9 years ago
|
w = MagickGetImageWidth(wand);
|
||
9 years ago
|
|
||
|
if (status == MagickPass)
|
||
|
h = MagickGetImageHeight(wand);
|
||
|
|
||
|
/* 2 -> 160 : width, "cols" */
|
||
|
/* 3 -> 160 : width, "rows" */
|
||
|
if (status == MagickPass)
|
||
|
status = MagickSampleImage(wand, 160, 160);
|
||
|
|
||
7 years ago
|
/** TODO: color maps */
|
||
9 years ago
|
|
||
|
/* 2 -> 256 : number of colors */
|
||
9 years ago
|
/* 4 -> 0 : treedepth -> auto */
|
||
|
/* 5 -> 0 : dither -> none */
|
||
|
/* 6 -> 0 : measure_error -> none */
|
||
9 years ago
|
if (status == MagickPass)
|
||
|
status = MagickQuantizeImage(wand, 256, GRAYColorspace, 0, 0, 0);
|
||
|
|
||
|
/* 2 -> 3 : radius, in pixels */
|
||
|
/* 3 -> 2 : "for reasonable results, radius should be larger than sigma" */
|
||
|
if (status == MagickPass)
|
||
|
status = MagickBlurImage(wand, 3, 2);
|
||
|
|
||
|
if (status == MagickPass)
|
||
|
status = MagickNormalizeImage(wand);
|
||
|
|
||
|
if (status == MagickPass)
|
||
|
status = MagickEqualizeImage(wand);
|
||
|
|
||
|
/* 2 -> 16 : width, "cols" */
|
||
|
/* 3 -> 16 : width, "rows" */
|
||
|
if (status == MagickPass)
|
||
|
status = MagickSampleImage(wand, 16, 16);
|
||
|
|
||
|
/* 2 -> 50%, tuned by magick number, see 'magick/image.h' */
|
||
|
if (status == MagickPass)
|
||
|
status = MagickThresholdImage(wand, 50.0 * (MaxRGB / 100));
|
||
|
|
||
|
if (status == MagickPass)
|
||
|
status = MagickSetImageType(wand, BilevelType);
|
||
|
|
||
|
if (status == MagickPass)
|
||
|
status = MagickSetImageFormat(wand, "MONO");
|
||
|
|
||
|
if (status == MagickPass)
|
||
|
buf = MagickWriteImageBlob(wand, &buf_size);
|
||
|
|
||
|
#ifdef DEBUG
|
||
9 years ago
|
if (status == MagickPass) {
|
||
|
fprintf(stderr, "sample H: %lu\n", MagickGetImageWidth(wand));
|
||
|
fprintf(stderr, "sample W: %lu\n", MagickGetImageWidth(wand));
|
||
|
fprintf(stderr, "buf size: %zu\n", buf_size);
|
||
|
for (unsigned int i = 0; i < buf_size; i++)
|
||
|
fprintf(stderr, "%02X", buf[i]);
|
||
|
}
|
||
9 years ago
|
#endif
|
||
|
|
||
|
if (status == MagickPass) {
|
||
7 years ago
|
assert(buf_size == SIMDB_BITMAP_SIZE);
|
||
7 years ago
|
if ((rec = calloc(1, sizeof(simdb_urec_t))) != NULL) {
|
||
|
rec->used = 0xFF;
|
||
|
rec->image_w = w;
|
||
|
rec->image_h = h;
|
||
|
memcpy(rec->bitmap, buf, SIMDB_BITMAP_SIZE);
|
||
|
}
|
||
7 years ago
|
#ifdef DEBUG
|
||
9 years ago
|
} else {
|
||
7 years ago
|
ExceptionType severity = 0;
|
||
|
char *description = MagickGetException(wand, &severity);
|
||
9 years ago
|
fprintf(stderr, "%03d %.1024s\n", severity, description);
|
||
7 years ago
|
#endif
|
||
9 years ago
|
}
|
||
|
|
||
|
DestroyMagickWand(wand);
|
||
|
DestroyMagick();
|
||
|
|
||
7 years ago
|
return rec;
|
||
9 years ago
|
}
|