#include "fie.h"
#include "oal_aloc.h"
INT fnSMP_mul_single_color(FHANDLE hsrc, FHANDLE hdst, INT ch, DOUBLE val);
INT fnSMP_edge_detection_single_color(FHANDLE hsrc, FHANDLE hdst, INT ch, DOUBLE* f_color, F_EDGE_SOBEL_PARAMS* params);
INT fnSMP_binarize_packing_color_img(FHANDLE hsrc, FHANDLE hdst, DOUBLE thresh);
INT fnSMP_mul_single_color(FHANDLE hsrc, FHANDLE hdst, INT ch, DOUBLE val)
{
INT err;
FHANDLE hsrc_unpacked = NULL;
FHANDLE hdst_unpacked = NULL;
FHANDLE hsrc_child = NULL;
FHANDLE hdst_child = NULL;
INT channels_src, channels_dst;
INT type_src, type_dst;
INT width_src, width_dst;
INT height_src, height_dst;
INT offset_x, offset_y;
err = fnFIE_img_get_params(hsrc, &channels_src, &type_src, NULL, &width_src, &height_src);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_img_get_params(hdst, &channels_dst, &type_dst, NULL, &width_dst, &height_dst);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_INVALID_IMAGE;
if (channels_src != 1) goto exit;
if (channels_dst != 1) goto exit;
if (type_src != F_IMG_RGBQUAD && type_src != F_IMG_RGBTRIPLE) goto exit;
if (type_dst != F_IMG_RGBQUAD && type_dst != F_IMG_RGBTRIPLE) goto exit;
if (width_src != width_dst) goto exit;
if (height_src != height_dst) goto exit;
err = F_ERR_INVALID_PARAM;
if (ch < 0 || 2 < ch) goto exit;
err = F_ERR_NOMEMORY;
hsrc_unpacked = fnFIE_img_root_alloc(F_IMG_UC8, 3, width_src, height_src);
if (hsrc_unpacked == NULL) goto exit;
hdst_unpacked = fnFIE_img_root_alloc(F_IMG_UC8, 3, width_dst, height_dst);
if (hdst_unpacked == NULL) goto exit;
err = fnFIE_unpacking_rgb(hsrc, hsrc_unpacked);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_img_copy(hsrc_unpacked, hdst_unpacked);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_NOMEMORY;
offset_x = 0;
offset_y = 0;
hsrc_child = fnFIE_img_child_alloc_single_ch(hsrc_unpacked, ch, offset_x, offset_y, width_src, height_src);
if (hsrc_child == NULL) goto exit;
hdst_child = fnFIE_img_child_alloc_single_ch(hdst_unpacked, ch, offset_x, offset_y, width_dst, height_dst);
if (hdst_child == NULL) goto exit;
err = fnFIE_img_mul_const(hsrc_child, val, hdst_child);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_packing_rgb(hdst_unpacked, hdst);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_NONE;
exit:
fnFIE_free_object(hsrc_child);
fnFIE_free_object(hdst_child);
fnFIE_free_object(hsrc_unpacked);
fnFIE_free_object(hdst_unpacked);
return err;
}
INT fnSMP_edge_detection_single_color(
FHANDLE hsrc, FHANDLE hdst, INT ch, DOUBLE* f_color, F_EDGE_SOBEL_PARAMS* params)
{
INT err;
FHANDLE hsrc_unpacked = NULL;
FHANDLE hsrc_child = NULL;
INT channels_src, channels_dst;
INT type_src, type_dst;
INT width_src, width_dst;
INT height_src, height_dst;
UINT feat_mode;
INT border_mode;
DPNT_T offset;
F_DEDGE * pedges = NULL;
INT edge_num;
INT i;
DPNT_T pnt;
err = fnFIE_img_get_params(hsrc, &channels_src, &type_src, NULL, &width_src, &height_src);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_img_get_params(hdst, &channels_dst, &type_dst, NULL, &width_dst, &height_dst);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_INVALID_IMAGE;
if (channels_src != 1) goto exit;
if (channels_dst != 1) goto exit;
if (type_src != F_IMG_RGBQUAD && type_src != F_IMG_RGBTRIPLE) goto exit;
if (type_dst != F_IMG_RGBQUAD && type_dst != F_IMG_RGBTRIPLE) goto exit;
if (width_src != width_dst) goto exit;
if (height_src != height_dst) goto exit;
err = F_ERR_INVALID_PARAM;
if (ch < 0 || 2 < ch) goto exit;
if (f_color == NULL) goto exit;
if (params == NULL) goto exit;
err = fnFIE_img_copy(hsrc, hdst);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_NOMEMORY;
hsrc_unpacked = fnFIE_img_root_alloc(F_IMG_UC8, 3, width_src, height_src);
if (hsrc_unpacked == NULL) goto exit;
err = fnFIE_unpacking_rgb(hsrc, hsrc_unpacked);
if (err != F_ERR_NONE) goto exit;
hsrc_child = fnFIE_img_child_alloc_single_ch(hsrc_unpacked, ch, 0, 0, width_src, height_src);
if (hsrc_child == NULL) goto exit;
feat_mode = F_EDGE_FEAT_NONE;
border_mode = F_BORDER_NONE;
offset.x = 0.0;
offset.y = 0.0;
err = fnFIE_edge_sobel_subpix(hsrc_child, NULL, params, feat_mode, border_mode, offset, &pedges, &edge_num);
if (err != F_ERR_NONE) goto exit;
for (i = 0; i < edge_num; i++) {
pnt.x = pedges[i].x;
pnt.y = pedges[i].y;
err = fnFIE_draw_point(hdst, f_color, pnt);
if (err != F_ERR_NONE) goto exit;
}
err = F_ERR_NONE;
exit:
fnOAL_free(pedges);
fnFIE_free_object(hsrc_child);
fnFIE_free_object(hsrc_unpacked);
return err;
}
INT fnSMP_binarize_packing_color_img(FHANDLE hsrc, FHANDLE hdst, DOUBLE thresh)
{
INT err;
FHANDLE hsrc_rgbq = NULL;
FHANDLE hsrc_gray = NULL;
INT channels_src, channels_dst;
INT type_src, type_dst;
INT width_src, width_dst;
INT height_src, height_dst;
DOUBLE coeff_r, coeff_g, coeff_b;
INT mode;
err = fnFIE_img_get_params(hsrc, &channels_src, &type_src, NULL, &width_src, &height_src);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_img_get_params(hdst, &channels_dst, &type_dst, NULL, &width_dst, &height_dst);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_INVALID_IMAGE;
if (channels_src != 1) goto exit;
if (channels_dst != 1) goto exit;
if (type_src != F_IMG_RGBQUAD && type_src != F_IMG_RGBTRIPLE) goto exit;
if (type_dst != F_IMG_BIN) goto exit;
if (width_src != width_dst) goto exit;
if (height_src != height_dst) goto exit;
if (type_src == F_IMG_RGBTRIPLE) {
err = F_ERR_NOMEMORY;
hsrc_rgbq = fnFIE_img_root_alloc(F_IMG_RGBQUAD, 1, width_src, height_src);
if (hsrc_rgbq == NULL) goto exit;
err = fnFIE_img_copy(hsrc, hsrc_rgbq);
if (err != F_ERR_NONE) goto exit;
hsrc = hsrc_rgbq;
}
err = F_ERR_NOMEMORY;
hsrc_gray = fnFIE_img_root_alloc(F_IMG_UC8, 1, width_src, height_src);
if (hsrc_gray == NULL) goto exit;
coeff_r = 0.299;
coeff_g = 0.587;
coeff_b = 0.114;
mode = 0;
err = fnFIE_img_rgb_to_gray(hsrc, hsrc_gray, coeff_r, coeff_g, coeff_b, mode);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_binarize(hsrc_gray, hdst, thresh);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_NONE;
exit:
fnFIE_free_object(hsrc_rgbq);
fnFIE_free_object(hsrc_gray);
return err;
}
INT execute()
{
INT err;
FHANDLE hsrc = NULL;
FHANDLE hdst_amp_r = NULL;
FHANDLE hdst_edge = NULL;
FHANDLE hdst_bin = NULL;
INT width, height;
INT channels;
INT ch;
DOUBLE val;
DOUBLE f_color[3] = {0, 0, 0};
DOUBLE thresh;
F_EDGE_SOBEL_PARAMS params;
err = fnFIE_load_png("test.png", &hsrc, F_COLOR_IMG_TYPE_RGBQ);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_img_get_params(hsrc, NULL, NULL, NULL, &width, &height);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_NOMEMORY;
channels = 1;
hdst_amp_r = fnFIE_img_root_alloc(F_IMG_RGBQUAD, channels, width, height);
if (hdst_amp_r == NULL) goto exit;
hdst_edge = fnFIE_img_root_alloc(F_IMG_RGBQUAD, channels, width, height);
if (hdst_edge == NULL) goto exit;
hdst_bin = fnFIE_img_root_alloc(F_IMG_BIN, channels, width, height);
if (hdst_bin == NULL) goto exit;
ch = 0;
val = 2;
err = fnSMP_mul_single_color(hsrc, hdst_amp_r, ch, val);
if (err != F_ERR_NONE) goto exit;
params.mag_threshold = 40;
params.nms_length = 1;
ch = 1;
f_color[1] = UC8_MAX;
err = fnSMP_edge_detection_single_color(hsrc, hdst_edge, ch, f_color, ¶ms);
if (err != F_ERR_NONE) goto exit;
thresh = 128;
err = fnSMP_binarize_packing_color_img(hsrc, hdst_bin, thresh);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_save_png("result_amp_r.png", hdst_amp_r, -1);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_save_png("result_edge.png", hdst_edge, -1);
if (err != F_ERR_NONE) goto exit;
err = fnFIE_save_png("result_bin.png", hdst_bin, -1);
if (err != F_ERR_NONE) goto exit;
err = F_ERR_NONE;
exit:
fnFIE_free_object(hsrc);
fnFIE_free_object(hdst_amp_r);
fnFIE_free_object(hdst_edge);
fnFIE_free_object(hdst_bin);
return err;
}
INT main()
{
INT err;
fnFIE_setup();
err = execute();
fnFIE_teardown();
return err;
}