void rectangle_united(rectangle_t *dst, rectangle_t *src) { int leftX = IM_MIN(dst->x, src->x); int topY = IM_MIN(dst->y, src->y); int rightX = IM_MAX(dst->x + dst->w, src->x + src->w); int bottomY = IM_MAX(dst->y + dst->h, src->y + src->h); dst->x = leftX; dst->y = topY; dst->w = rightX - leftX; dst->h = bottomY - topY; }
static void imlib_add_line_op(image_t *img, int line, void *other, void *data, bool vflipped) { image_t *mask = (image_t *) data; switch(img->bpp) { case IMAGE_BPP_BINARY: { uint32_t *data = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, line); for (int i = 0, j = img->w; i < j; i++) { if ((!mask) || image_get_mask_pixel(mask, i, line)) { int dataPixel = IMAGE_GET_BINARY_PIXEL_FAST(data, i); int otherPixel = IMAGE_GET_BINARY_PIXEL_FAST(((uint32_t *) other), i); int p = dataPixel + otherPixel; p = IM_MIN(p, COLOR_BINARY_MAX); IMAGE_PUT_BINARY_PIXEL_FAST(data, i, p); } } break; } case IMAGE_BPP_GRAYSCALE: { uint8_t *data = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, line); for (int i = 0, j = img->w; i < j; i++) { if ((!mask) || image_get_mask_pixel(mask, i, line)) { int dataPixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(data, i); int otherPixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(((uint8_t *) other), i); int p = dataPixel + otherPixel; p = IM_MIN(p, COLOR_GRAYSCALE_MAX); IMAGE_PUT_GRAYSCALE_PIXEL_FAST(data, i, p); } } break; } case IMAGE_BPP_RGB565: { uint16_t *data = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, line); for (int i = 0, j = img->w; i < j; i++) { if ((!mask) || image_get_mask_pixel(mask, i, line)) { int dataPixel = IMAGE_GET_RGB565_PIXEL_FAST(data, i); int otherPixel = IMAGE_GET_RGB565_PIXEL_FAST(((uint16_t *) other), i); int r = COLOR_RGB565_TO_R5(dataPixel) + COLOR_RGB565_TO_R5(otherPixel); int g = COLOR_RGB565_TO_G6(dataPixel) + COLOR_RGB565_TO_G6(otherPixel); int b = COLOR_RGB565_TO_B5(dataPixel) + COLOR_RGB565_TO_B5(otherPixel); r = IM_MIN(r, COLOR_R5_MAX); g = IM_MIN(g, COLOR_G6_MAX); b = IM_MIN(b, COLOR_B5_MAX); IMAGE_PUT_RGB565_PIXEL_FAST(data, i, COLOR_R5_G6_B5_TO_RGB565(r, g, b)); } } break; } default: { break; } } }
static int set_auto_gain(sensor_t *sensor, int enable, float gain_db, float gain_db_ceiling) { uint8_t reg; int ret = cambus_readb(sensor->slv_addr, REG_COM8, ®); ret |= cambus_writeb(sensor->slv_addr, REG_COM8, (reg & (~REG_COM8_AGC)) | ((enable != 0) ? REG_COM8_AGC : 0)); if ((enable == 0) && (!isnanf(gain_db)) && (!isinf(gain_db))) { float gain = IM_MAX(IM_MIN(fast_expf((gain_db / 20.0) * fast_log(10.0)), 128.0), 1.0); int gain_temp = fast_roundf(fast_log2(IM_MAX(gain / 2.0, 1.0))); int gain_hi = 0x3F >> (6 - gain_temp); int gain_lo = IM_MIN(fast_roundf(((gain / (1 << gain_temp)) - 1.0) * 16.0), 15); ret |= cambus_writeb(sensor->slv_addr, REG_GAIN, ((gain_hi & 0x0F) << 4) | (gain_lo << 0)); ret |= cambus_readb(sensor->slv_addr, REG_VREF, ®); ret |= cambus_writeb(sensor->slv_addr, REG_VREF, ((gain_hi & 0x30) << 2) | (reg & 0x3F)); } else if ((enable != 0) && (!isnanf(gain_db_ceiling)) && (!isinf(gain_db_ceiling))) {
static int set_auto_gain(sensor_t *sensor, int enable, float gain_db, float gain_db_ceiling) { uint8_t reg; int ret = cambus_readb(sensor->slv_addr, BANK_SEL, ®); ret |= cambus_writeb(sensor->slv_addr, BANK_SEL, reg | BANK_SEL_SENSOR); ret |= cambus_readb(sensor->slv_addr, COM8, ®); ret |= cambus_writeb(sensor->slv_addr, COM8, (reg & (~COM8_AGC_EN)) | ((enable != 0) ? COM8_AGC_EN : 0)); if ((enable == 0) && (!isnanf(gain_db)) && (!isinff(gain_db))) { float gain = IM_MAX(IM_MIN(fast_expf((gain_db / 20.0) * fast_log(10.0)), 32.0), 1.0); int gain_temp = fast_roundf(fast_log2(IM_MAX(gain / 2.0, 1.0))); int gain_hi = 0xF >> (4 - gain_temp); int gain_lo = IM_MIN(fast_roundf(((gain / (1 << gain_temp)) - 1.0) * 16.0), 15); ret |= cambus_writeb(sensor->slv_addr, GAIN, (gain_hi << 4) | (gain_lo << 0)); } else if ((enable != 0) && (!isnanf(gain_db_ceiling)) && (!isinff(gain_db_ceiling))) {
void imlib_chrominvar(image_t *img) { switch(img->bpp) { case IMAGE_BPP_BINARY: { break; } case IMAGE_BPP_GRAYSCALE: { break; } case IMAGE_BPP_RGB565: { for (int y = 0, yy = img->h; y < yy; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); for (int x = 0, xx = img->w; x < xx; x++) { int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x); float r_lin = xyz_table[COLOR_RGB565_TO_R8(pixel)]; float g_lin = xyz_table[COLOR_RGB565_TO_G8(pixel)]; float b_lin = xyz_table[COLOR_RGB565_TO_B8(pixel)]; float lin_sum = r_lin + g_lin + b_lin; float r_lin_div = 0.0f; float g_lin_div = 0.0f; float b_lin_div = 0.0f; if (lin_sum > 0.0f) { lin_sum = 1.0f / lin_sum; r_lin_div = r_lin * lin_sum; g_lin_div = g_lin * lin_sum; b_lin_div = b_lin * lin_sum; } int r_lin_div_int = IM_MAX(IM_MIN(r_lin_div * 255.0f, COLOR_R8_MAX), COLOR_R8_MIN); int g_lin_div_int = IM_MAX(IM_MIN(g_lin_div * 255.0f, COLOR_G8_MAX), COLOR_G8_MIN); int b_lin_div_int = IM_MAX(IM_MIN(b_lin_div * 255.0f, COLOR_B8_MAX), COLOR_B8_MIN); IMAGE_PUT_RGB565_PIXEL_FAST(row_ptr, x, COLOR_R8_G8_B8_TO_RGB565(r_lin_div_int, g_lin_div_int, b_lin_div_int)); } } break; } default: { break; } } }
/* Set output area of trn so that it just holds all of our input pels. */ void im__transform_set_area( Transformation *trn ) { double xA, xB, xC, xD; double yA, yB, yC, yD; int xmin, xmax, ymin, ymax; im__transform_forward( trn, trn->iarea.left, trn->iarea.top, &xA, &yA ); im__transform_forward( trn, IM_RECT_RIGHT( &trn->iarea ) - 1, trn->iarea.top, &xB, &yB ); im__transform_forward( trn, trn->iarea.left, IM_RECT_BOTTOM( &trn->iarea ) - 1, &xC, &yC ); im__transform_forward( trn, IM_RECT_RIGHT( &trn->iarea ) - 1, IM_RECT_BOTTOM( &trn->iarea ) - 1, &xD, &yD ); xmin = IM_MIN( xA, IM_MIN( xB, IM_MIN( xC, xD ) ) ); ymin = IM_MIN( yA, IM_MIN( yB, IM_MIN( yC, yD ) ) ); xmax = IM_MAX( xA, IM_MAX( xB, IM_MAX( xC, xD ) ) ); ymax = IM_MAX( yA, IM_MAX( yB, IM_MAX( yC, yD ) ) ); trn->oarea.left = xmin; trn->oarea.top = ymin; trn->oarea.width = xmax - xmin + 1; trn->oarea.height = ymax - ymin + 1; }
/* Transform a rect using a point transformer. */ static void transform_rect( const Transformation *trn, transform_fn transform, const Rect *in, /* In input space */ Rect *out ) /* In output space */ { double x1, y1; /* Map corners */ double x2, y2; double x3, y3; double x4, y4; double left, right, top, bottom; /* Map input Rect. */ transform( trn, in->left, in->top, &x1, &y1 ); transform( trn, in->left, IM_RECT_BOTTOM( in ), &x3, &y3 ); transform( trn, IM_RECT_RIGHT( in ), in->top, &x2, &y2 ); transform( trn, IM_RECT_RIGHT( in ), IM_RECT_BOTTOM( in ), &x4, &y4 ); /* Find bounding box for these four corners. Round-to-nearest to try * to stop rounding errors growing images. */ left = IM_MIN( x1, IM_MIN( x2, IM_MIN( x3, x4 ) ) ); right = IM_MAX( x1, IM_MAX( x2, IM_MAX( x3, x4 ) ) ); top = IM_MIN( y1, IM_MIN( y2, IM_MIN( y3, y4 ) ) ); bottom = IM_MAX( y1, IM_MAX( y2, IM_MAX( y3, y4 ) ) ); out->left = IM_RINT( left ); out->top = IM_RINT( top ); out->width = IM_RINT( right - left ); out->height = IM_RINT( bottom - top ); }
// https://en.wikipedia.org/wiki/Lab_color_space -> CIELAB-CIEXYZ conversions // https://en.wikipedia.org/wiki/SRGB -> Specification of the transformation uint16_t imlib_lab_to_rgb(uint8_t l, int8_t a, int8_t b) { float x = ((l + 16) * 0.008621f) + (a * 0.002f); float y = ((l + 16) * 0.008621f); float z = ((l + 16) * 0.008621f) - (b * 0.005f); x = ((x > 0.206897f) ? (x*x*x) : ((0.128419f * x) - 0.017713f)) * 095.047f; y = ((y > 0.206897f) ? (y*y*y) : ((0.128419f * y) - 0.017713f)) * 100.000f; z = ((z > 0.206897f) ? (z*z*z) : ((0.128419f * z) - 0.017713f)) * 108.883f; float r_lin = ((x * +3.2406f) + (y * -1.5372f) + (z * -0.4986f)) / 100.0f; float g_lin = ((x * -0.9689f) + (y * +1.8758f) + (z * +0.0415f)) / 100.0f; float b_lin = ((x * +0.0557f) + (y * -0.2040f) + (z * +1.0570f)) / 100.0f; r_lin = (r_lin>0.0031308f) ? ((1.055f*powf(r_lin, 0.416666f))-0.055f) : (r_lin*12.92f); g_lin = (g_lin>0.0031308f) ? ((1.055f*powf(g_lin, 0.416666f))-0.055f) : (g_lin*12.92f); b_lin = (b_lin>0.0031308f) ? ((1.055f*powf(b_lin, 0.416666f))-0.055f) : (b_lin*12.92f); uint32_t red = IM_MAX(IM_MIN(fast_floorf(r_lin * COLOR_R8_MAX), COLOR_R8_MAX), COLOR_R8_MIN); uint32_t green = IM_MAX(IM_MIN(fast_floorf(g_lin * COLOR_G8_MAX), COLOR_G8_MAX), COLOR_G8_MIN); uint32_t blue = IM_MAX(IM_MIN(fast_floorf(b_lin * COLOR_B8_MAX), COLOR_B8_MAX), COLOR_B8_MIN); return COLOR_R8_G8_B8_TO_RGB565(red, green, blue); }
/** * im_tbjoin: * @top: image to go on top * @bottom: image to go on bottom * @out: output image * * Join @top and @bottom together, up-down. If one is wider than the * other, @out will be has wide as the smaller. * * If the number of bands differs, one of the images * must have one band. In this case, an n-band image is formed from the * one-band image by joining n copies of the one-band image together, and then * the two n-band images are operated upon. * * The two input images are cast up to the smallest common type (see table * Smallest common format in * <link linkend="VIPS-arithmetic">arithmetic</link>). * * See also: im_insert(), im_tbjoin(). * * Returns: 0 on success, -1 on error */ int im_tbjoin( IMAGE *top, IMAGE *bottom, IMAGE *out ) { IMAGE *t1; /* Paste top and bottom together, cut off any leftovers. */ if( !(t1 = im_open_local( out, "im_tbjoin:1", "p" )) || im_insert( top, bottom, t1, 0, top->Ysize ) || im_extract_area( t1, out, 0, 0, IM_MIN( top->Xsize, bottom->Xsize ), t1->Ysize ) ) return( -1 ); out->Xoffset = 0; out->Yoffset = top->Ysize; return( 0 ); }
/* Merge the sequence value back into the per-call state. */ static int stop_fn( void *vseq, void *a, void *b ) { Seq *seq = (Seq *) vseq; MinInfo *inf = (MinInfo *) a; if( seq->valid ) { if( !inf->valid ) /* Just copy. */ inf->value = seq->value; else /* Merge. */ inf->value = IM_MIN( inf->value, seq->value ); inf->valid = 1; } im_free( seq ); return( 0 ); }
static void imlib_remove_shadows_sub_line_op(image_t *img, int line, void *data, bool vflipped) { imlib_remove_shadows_line_op_state_t *state = (imlib_remove_shadows_line_op_state_t *) data; if (state->lines_processed >= imlib_remove_shadows_kernel_rank) { int y = vflipped ? (line + imlib_remove_shadows_kernel_rank) : (line - imlib_remove_shadows_kernel_rank); int index = y % imlib_remove_shadows_kernel_size; for (int x = 0, xx = img->w; x < xx; x++) { int img_pixel = IMAGE_GET_RGB565_PIXEL_FAST(state->img_lines[index], x); int img_r = COLOR_RGB565_TO_R8(img_pixel); int img_g = COLOR_RGB565_TO_G8(img_pixel); int img_b = COLOR_RGB565_TO_B8(img_pixel); float img_v = IM_MAX(IM_MAX(img_r, img_g), img_b); int other_pixel = IMAGE_GET_RGB565_PIXEL_FAST(state->other_lines[index], x); int other_r = COLOR_RGB565_TO_R8(other_pixel); int other_g = COLOR_RGB565_TO_G8(other_pixel); int other_b = COLOR_RGB565_TO_B8(other_pixel); float other_v = IM_MAX(IM_MAX(other_r, other_g), other_b); float ratio = img_v / other_v; if ((0.3f < ratio) && (ratio < 1.0f)) { int minY = IM_MAX(y - imlib_remove_shadows_kernel_rank, 0); int maxY = IM_MIN(y + imlib_remove_shadows_kernel_rank, img->h - 1); int minX = IM_MAX(x - imlib_remove_shadows_kernel_rank, 0); int maxX = IM_MIN(x + imlib_remove_shadows_kernel_rank, img->w - 1); int windowArea = (maxX - minX + 1) * (maxY - minY + 1); int hDiffSum = 0; int sDiffSum = 0; for (int k_y = minY; k_y <= maxY; k_y++) { int k_index = k_y % imlib_remove_shadows_kernel_size; for (int k_x = minX; k_x <= maxX; k_x++) { int k_img_pixel = IMAGE_GET_RGB565_PIXEL_FAST(state->img_lines[k_index], k_x); int k_img_r = COLOR_RGB565_TO_R8(k_img_pixel); int k_img_g = COLOR_RGB565_TO_G8(k_img_pixel); int k_img_b = COLOR_RGB565_TO_B8(k_img_pixel); int k_img_cmax = IM_MAX(IM_MAX(k_img_r, k_img_g), k_img_b); int k_img_cmin = IM_MAX(IM_MAX(k_img_r, k_img_g), k_img_b); float k_img_cdel = k_img_cmax - k_img_cmin; float k_img_h = 0; float k_img_s = k_img_cmax ? (k_img_cdel / k_img_cmax) : 0; int k_other_pixel = IMAGE_GET_RGB565_PIXEL_FAST(state->other_lines[k_index], k_x); int k_other_r = COLOR_RGB565_TO_R8(k_other_pixel); int k_other_g = COLOR_RGB565_TO_G8(k_other_pixel); int k_other_b = COLOR_RGB565_TO_B8(k_other_pixel); int k_other_cmax = IM_MAX(IM_MAX(k_other_r, k_other_g), k_other_b); int k_other_cmin = IM_MAX(IM_MAX(k_other_r, k_other_g), k_other_b); float k_other_cdel = k_other_cmax - k_other_cmin; float k_other_h = 0; float k_other_s = k_other_cmax ? (k_other_cdel / k_other_cmax) : 0; if (k_img_cdel) { if (k_img_cmax == k_img_r) { k_img_h = ((k_img_g - k_img_b) / k_img_cdel) + 0; } else if (k_img_cmax == k_img_g) { k_img_h = ((k_img_b - k_img_r) / k_img_cdel) + 2; } else if (k_img_cmax == k_img_b) { k_img_h = ((k_img_r - k_img_g) / k_img_cdel) + 4; } k_img_h *= 60; if (k_img_h < 0) k_img_h += 360.0; } if (k_other_cdel) { if (k_other_cmax == k_other_r) { k_other_h = ((k_other_g - k_other_b) / k_other_cdel) + 0; } else if (k_other_cmax == k_other_g) { k_other_h = ((k_other_b - k_other_r) / k_other_cdel) + 2; } else if (k_other_cmax == k_other_b) { k_other_h = ((k_other_r - k_other_g) / k_other_cdel) + 4; } k_other_h *= 60; if (k_other_h < 0) k_other_h += 360.0; } int hDiff = abs(k_img_h - k_other_h); hDiffSum += (hDiff >= 90) ? (180 - hDiff) : hDiff; sDiffSum += k_img_s - k_other_s; } } bool hIsShadow = (hDiffSum / windowArea) < 48; bool sIsShadow = (sDiffSum / windowArea) < 40; IMAGE_PUT_RGB565_PIXEL_FAST(state->out_lines[index], x, (hIsShadow && sIsShadow) ? other_pixel : img_pixel); } else { IMAGE_PUT_RGB565_PIXEL_FAST(state->out_lines[index], x, img_pixel); } } } imlib_remove_shadows_sub_sub_line_op(img, line, data, vflipped); }
static void imlib_div_line_op(image_t *img, int line, void *other, void *data, bool vflipped) { bool invert = ((imlib_div_line_op_state_t *) data)->invert; bool mod = ((imlib_div_line_op_state_t *) data)->mod; image_t *mask = ((imlib_div_line_op_state_t *) data)->mask; switch(img->bpp) { case IMAGE_BPP_BINARY: { uint32_t *data = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, line); int pScale = COLOR_BINARY_MAX - COLOR_BINARY_MIN; for (int i = 0, j = img->w; i < j; i++) { if ((!mask) || image_get_mask_pixel(mask, i, line)) { int dataPixel = IMAGE_GET_BINARY_PIXEL_FAST(data, i); int otherPixel = IMAGE_GET_BINARY_PIXEL_FAST(((uint32_t *) other), i); int p = mod ? IM_MOD((invert?otherPixel:dataPixel) * pScale, (invert?dataPixel:otherPixel)) : IM_DIV((invert?otherPixel:dataPixel) * pScale, (invert?dataPixel:otherPixel)); p = IM_MIN(p, COLOR_BINARY_MAX); IMAGE_PUT_BINARY_PIXEL_FAST(data, i, p); } } break; } case IMAGE_BPP_GRAYSCALE: { uint8_t *data = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, line); int pScale = COLOR_GRAYSCALE_MAX - COLOR_GRAYSCALE_MIN; for (int i = 0, j = img->w; i < j; i++) { if ((!mask) || image_get_mask_pixel(mask, i, line)) { int dataPixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(data, i); int otherPixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(((uint8_t *) other), i); int p = mod ? IM_MOD((invert?otherPixel:dataPixel) * pScale, (invert?dataPixel:otherPixel)) : IM_DIV((invert?otherPixel:dataPixel) * pScale, (invert?dataPixel:otherPixel)); p = IM_MIN(p, COLOR_GRAYSCALE_MAX); IMAGE_PUT_GRAYSCALE_PIXEL_FAST(data, i, p); } } break; } case IMAGE_BPP_RGB565: { uint16_t *data = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, line); int rScale = COLOR_R5_MAX - COLOR_R5_MIN; int gScale = COLOR_G6_MAX - COLOR_G6_MIN; int bScale = COLOR_B5_MAX - COLOR_B5_MIN; for (int i = 0, j = img->w; i < j; i++) { if ((!mask) || image_get_mask_pixel(mask, i, line)) { int dataPixel = IMAGE_GET_RGB565_PIXEL_FAST(data, i); int otherPixel = IMAGE_GET_RGB565_PIXEL_FAST(((uint16_t *) other), i); int dR = COLOR_RGB565_TO_R5(dataPixel); int dG = COLOR_RGB565_TO_G6(dataPixel); int dB = COLOR_RGB565_TO_B5(dataPixel); int oR = COLOR_RGB565_TO_R5(otherPixel); int oG = COLOR_RGB565_TO_G6(otherPixel); int oB = COLOR_RGB565_TO_B5(otherPixel); int r = mod ? IM_MOD((invert?oR:dR) * rScale, (invert?dR:oR)) : IM_DIV((invert?oR:dR) * rScale, (invert?dR:oR)); int g = mod ? IM_MOD((invert?oG:dG) * gScale, (invert?dG:oG)) : IM_DIV((invert?oG:dG) * gScale, (invert?dG:oG)); int b = mod ? IM_MOD((invert?oB:dB) * bScale, (invert?dB:oB)) : IM_DIV((invert?oB:dB) * bScale, (invert?dB:oB)); r = IM_MIN(r, COLOR_R5_MAX); g = IM_MIN(g, COLOR_G6_MAX); b = IM_MIN(b, COLOR_B5_MAX); IMAGE_PUT_RGB565_PIXEL_FAST(data, i, COLOR_R5_G6_B5_TO_RGB565(r, g, b)); } } break; } default: { break; } } }
void imlib_gamma_corr(image_t *img, float gamma, float contrast, float brightness) { gamma = IM_DIV(1.0, gamma); switch(img->bpp) { case IMAGE_BPP_BINARY: { float pScale = COLOR_BINARY_MAX - COLOR_BINARY_MIN; float pDiv = 1 / pScale; int *p_lut = fb_alloc((COLOR_BINARY_MAX - COLOR_BINARY_MIN + 1) * sizeof(int)); for (int i = COLOR_BINARY_MIN; i <= COLOR_BINARY_MAX; i++) { int p = ((fast_powf(i * pDiv, gamma) * contrast) + brightness) * pScale; p_lut[i] = IM_MIN(IM_MAX(p , COLOR_BINARY_MIN), COLOR_BINARY_MAX); } for (int y = 0, yy = img->h; y < yy; y++) { uint32_t *data = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y); for (int x = 0, xx = img->w; x < xx; x++) { int dataPixel = IMAGE_GET_BINARY_PIXEL_FAST(data, x); int p = p_lut[dataPixel]; IMAGE_PUT_BINARY_PIXEL_FAST(data, x, p); } } fb_free(); break; } case IMAGE_BPP_GRAYSCALE: { float pScale = COLOR_GRAYSCALE_MAX - COLOR_GRAYSCALE_MIN; float pDiv = 1 / pScale; int *p_lut = fb_alloc((COLOR_GRAYSCALE_MAX - COLOR_GRAYSCALE_MIN + 1) * sizeof(int)); for (int i = COLOR_GRAYSCALE_MIN; i <= COLOR_GRAYSCALE_MAX; i++) { int p = ((fast_powf(i * pDiv, gamma) * contrast) + brightness) * pScale; p_lut[i] = IM_MIN(IM_MAX(p , COLOR_GRAYSCALE_MIN), COLOR_GRAYSCALE_MAX); } for (int y = 0, yy = img->h; y < yy; y++) { uint8_t *data = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y); for (int x = 0, xx = img->w; x < xx; x++) { int dataPixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(data, x); int p = p_lut[dataPixel]; IMAGE_PUT_GRAYSCALE_PIXEL_FAST(data, x, p); } } fb_free(); break; } case IMAGE_BPP_RGB565: { float rScale = COLOR_R5_MAX - COLOR_R5_MIN; float gScale = COLOR_G6_MAX - COLOR_G6_MIN; float bScale = COLOR_B5_MAX - COLOR_B5_MIN; float rDiv = 1 / rScale; float gDiv = 1 / gScale; float bDiv = 1 / bScale; int *r_lut = fb_alloc((COLOR_R5_MAX - COLOR_R5_MIN + 1) * sizeof(int)); int *g_lut = fb_alloc((COLOR_G6_MAX - COLOR_G6_MIN + 1) * sizeof(int)); int *b_lut = fb_alloc((COLOR_B5_MAX - COLOR_B5_MIN + 1) * sizeof(int)); for (int i = COLOR_R5_MIN; i <= COLOR_R5_MAX; i++) { int r = ((fast_powf(i * rDiv, gamma) * contrast) + brightness) * rScale; r_lut[i] = IM_MIN(IM_MAX(r , COLOR_R5_MIN), COLOR_R5_MAX); } for (int i = COLOR_G6_MIN; i <= COLOR_G6_MAX; i++) { int g = ((fast_powf(i * gDiv, gamma) * contrast) + brightness) * gScale; g_lut[i] = IM_MIN(IM_MAX(g , COLOR_G6_MIN), COLOR_G6_MAX); } for (int i = COLOR_B5_MIN; i <= COLOR_B5_MAX; i++) { int b = ((fast_powf(i * bDiv, gamma) * contrast) + brightness) * bScale; b_lut[i] = IM_MIN(IM_MAX(b , COLOR_B5_MIN), COLOR_B5_MAX); } for (int y = 0, yy = img->h; y < yy; y++) { uint16_t *data = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); for (int x = 0, xx = img->w; x < xx; x++) { int dataPixel = IMAGE_GET_RGB565_PIXEL_FAST(data, x); int r = r_lut[COLOR_RGB565_TO_R5(dataPixel)]; int g = g_lut[COLOR_RGB565_TO_G6(dataPixel)]; int b = b_lut[COLOR_RGB565_TO_B5(dataPixel)]; IMAGE_PUT_RGB565_PIXEL_FAST(data, x, COLOR_R5_G6_B5_TO_RGB565(r, g, b)); } } fb_free(); fb_free(); fb_free(); break; } default: { break; } } }
static void imlib_erode_dilate(image_t *img, int ksize, int threshold, int e_or_d, image_t *mask) { int brows = ksize + 1; image_t buf; buf.w = img->w; buf.h = brows; buf.bpp = img->bpp; switch(img->bpp) { case IMAGE_BPP_BINARY: { buf.data = fb_alloc(IMAGE_BINARY_LINE_LEN_BYTES(img) * brows); for (int y = 0, yy = img->h; y < yy; y++) { uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y); uint32_t *buf_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&buf, (y % brows)); for (int x = 0, xx = img->w; x < xx; x++) { int pixel = IMAGE_GET_BINARY_PIXEL_FAST(row_ptr, x); IMAGE_PUT_BINARY_PIXEL_FAST(buf_row_ptr, x, pixel); if ((mask && (!image_get_mask_pixel(mask, x, y))) || (pixel == e_or_d)) { continue; // Short circuit. } int acc = e_or_d ? 0 : -1; // Don't count center pixel... for (int j = -ksize; j <= ksize; j++) { uint32_t *k_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, IM_MIN(IM_MAX(y + j, 0), (img->h - 1))); for (int k = -ksize; k <= ksize; k++) { acc += IMAGE_GET_BINARY_PIXEL_FAST(k_row_ptr, IM_MIN(IM_MAX(x + k, 0), (img->w - 1))); } } if (!e_or_d) { // Preserve original pixel value... or clear it. if (acc < threshold) IMAGE_CLEAR_BINARY_PIXEL_FAST(buf_row_ptr, x); } else { // Preserve original pixel value... or set it. if (acc > threshold) IMAGE_SET_BINARY_PIXEL_FAST(buf_row_ptr, x); } } if (y >= ksize) { // Transfer buffer lines... memcpy(IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, (y - ksize)), IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&buf, ((y - ksize) % brows)), IMAGE_BINARY_LINE_LEN_BYTES(img)); } } // Copy any remaining lines from the buffer image... for (int y = img->h - ksize, yy = img->h; y < yy; y++) { memcpy(IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y), IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&buf, (y % brows)), IMAGE_BINARY_LINE_LEN_BYTES(img)); } fb_free(); break; } case IMAGE_BPP_GRAYSCALE: { buf.data = fb_alloc(IMAGE_GRAYSCALE_LINE_LEN_BYTES(img) * brows); for (int y = 0, yy = img->h; y < yy; y++) { uint8_t *row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y); uint8_t *buf_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(&buf, (y % brows)); for (int x = 0, xx = img->w; x < xx; x++) { int pixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(row_ptr, x); IMAGE_PUT_GRAYSCALE_PIXEL_FAST(buf_row_ptr, x, pixel); if ((mask && (!image_get_mask_pixel(mask, x, y))) || (COLOR_GRAYSCALE_TO_BINARY(pixel) == e_or_d)) { continue; // Short circuit. } int acc = e_or_d ? 0 : -1; // Don't count center pixel... for (int j = -ksize; j <= ksize; j++) { uint8_t *k_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, IM_MIN(IM_MAX(y + j, 0), (img->h - 1))); for (int k = -ksize; k <= ksize; k++) { acc += COLOR_GRAYSCALE_TO_BINARY(IMAGE_GET_GRAYSCALE_PIXEL_FAST(k_row_ptr, IM_MIN(IM_MAX(x + k, 0), (img->w - 1)))); } } if (!e_or_d) { // Preserve original pixel value... or clear it. if (acc < threshold) IMAGE_PUT_GRAYSCALE_PIXEL_FAST(buf_row_ptr, x, COLOR_GRAYSCALE_BINARY_MIN); } else { // Preserve original pixel value... or set it. if (acc > threshold) IMAGE_PUT_GRAYSCALE_PIXEL_FAST(buf_row_ptr, x, COLOR_GRAYSCALE_BINARY_MAX); } } if (y >= ksize) { // Transfer buffer lines... memcpy(IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, (y - ksize)), IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(&buf, ((y - ksize) % brows)), IMAGE_GRAYSCALE_LINE_LEN_BYTES(img)); } } // Copy any remaining lines from the buffer image... for (int y = img->h - ksize, yy = img->h; y < yy; y++) { memcpy(IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y), IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(&buf, (y % brows)), IMAGE_GRAYSCALE_LINE_LEN_BYTES(img)); } fb_free(); break; } case IMAGE_BPP_RGB565: { buf.data = fb_alloc(IMAGE_RGB565_LINE_LEN_BYTES(img) * brows); for (int y = 0, yy = img->h; y < yy; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); uint16_t *buf_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(&buf, (y % brows)); for (int x = 0, xx = img->w; x < xx; x++) { int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x); IMAGE_PUT_RGB565_PIXEL_FAST(buf_row_ptr, x, pixel); if ((mask && (!image_get_mask_pixel(mask, x, y))) || (COLOR_RGB565_TO_BINARY(pixel) == e_or_d)) { continue; // Short circuit. } int acc = e_or_d ? 0 : -1; // Don't count center pixel... for (int j = -ksize; j <= ksize; j++) { uint16_t *k_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, IM_MIN(IM_MAX(y + j, 0), (img->h - 1))); for (int k = -ksize; k <= ksize; k++) { acc += COLOR_RGB565_TO_BINARY(IMAGE_GET_RGB565_PIXEL_FAST(k_row_ptr, IM_MIN(IM_MAX(x + k, 0), (img->w - 1)))); } } if (!e_or_d) { // Preserve original pixel value... or clear it. if (acc < threshold) IMAGE_PUT_RGB565_PIXEL_FAST(buf_row_ptr, x, COLOR_RGB565_BINARY_MIN); } else { // Preserve original pixel value... or set it. if (acc > threshold) IMAGE_PUT_RGB565_PIXEL_FAST(buf_row_ptr, x, COLOR_RGB565_BINARY_MAX); } } if (y >= ksize) { // Transfer buffer lines... memcpy(IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, (y - ksize)), IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(&buf, ((y - ksize) % brows)), IMAGE_RGB565_LINE_LEN_BYTES(img)); } } // Copy any remaining lines from the buffer image... for (int y = img->h - ksize, yy = img->h; y < yy; y++) { memcpy(IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y), IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(&buf, (y % brows)), IMAGE_RGB565_LINE_LEN_BYTES(img)); } fb_free(); break; } default: { break; } } }
/* Output to a memory area. Might be im_setbuf(), im_mmapin()/im_makerw() or * im_mmapinrw(). */ static int eval_to_memory( im_threadgroup_t *tg, REGION *or ) { int y, chunk; IMAGE *im = or->im; int result; result = 0; #ifdef DEBUG_IO int ntiles = 0; printf( "eval_to_memory: partial image output to memory area\n" ); #endif /*DEBUG_IO*/ /* Signal start of eval. */ if( im__start_eval( im ) ) return( -1 ); /* Choose a chunk size ... 1/100th of the height of the image, about. * This sets the granularity of user feedback on eval progress, but * does not affect mem requirements etc. */ chunk = (im->Ysize / 100) + 1; /* Loop down the output image, evaling each chunk. */ for( y = 0; y < im->Ysize; y += chunk ) { Rect pos; /* Attach or to this position in image. */ pos.left = 0; pos.top = y; pos.width = im->Xsize; pos.height = IM_MIN( chunk, im->Ysize - y ); if( (result = im_region_image( or, &pos )) ) break; /* Ask for evaluation of this area. */ if( (result = eval_to_region( or, tg )) ) break; /* Trigger any eval callbacks on our source image. */ if( (result = im__handle_eval( im, pos.width, pos.height )) ) break; #ifdef DEBUG_IO ntiles++; #endif /*DEBUG_IO*/ } /* Signal end of eval. */ result |= im__end_eval( im ); #ifdef DEBUG_IO printf( "eval_to_memory: %d patches written\n", ntiles ); #endif /*DEBUG_IO*/ return( result ); }
/* Break the mask into a set of lines. */ static int boxes_break( Boxes *boxes ) { DOUBLEMASK *mask = boxes->mask; const int size = mask->xsize * mask->ysize; double max; double min; double depth; int layers_above; int layers_below; int z, n, x, y; /* Find mask range. We must always include the zero axis in the mask. */ max = 0; min = 0; for( n = 0; n < size; n++ ) { max = IM_MAX( max, mask->coeff[n] ); min = IM_MIN( min, mask->coeff[n] ); } VIPS_DEBUG_MSG( "boxes_new: min = %g, max = %g\n", min, max ); /* The zero axis must fall on a layer boundary. Estimate the * depth, find n-lines-above-zero, get exact depth, then calculate a * fixed n-lines which includes any negative parts. */ depth = (max - min) / boxes->n_layers; layers_above = ceil( max / depth ); depth = max / layers_above; layers_below = floor( min / depth ); boxes->n_layers = layers_above - layers_below; VIPS_DEBUG_MSG( "boxes_new: depth = %g, n_layers = %d\n", depth, boxes->n_layers ); /* For each layer, generate a set of lines which are inside the * perimeter. Work down from the top. */ for( z = 0; z < boxes->n_layers; z++ ) { /* How deep we are into the mask, as a double we can test * against. Add half the layer depth so we can easily find >50% * mask elements. */ double z_ph = max - (1 + z) * depth + depth / 2; /* Odd, but we must avoid rounding errors that make us miss 0 * in the line above. */ int z_positive = z < layers_above; for( y = 0; y < mask->ysize; y++ ) { int inside; /* Start outside the perimeter. */ inside = 0; for( x = 0; x < mask->xsize; x++ ) { double coeff = MASK( mask, x, y ); /* The vertical line from mask[x, y] to 0 is * inside. Is our current square (x, y) part * of that line? */ if( (z_positive && coeff >= z_ph) || (!z_positive && coeff <= z_ph) ) { if( !inside ) { boxes_start( boxes, x ); inside = 1; } } else { if( inside ) { if( boxes_end( boxes, x, y, z_positive ? 1 : -1 ) ) return( -1 ); inside = 0; } } } if( inside && boxes_end( boxes, mask->xsize, y, z_positive ? 1 : -1 ) ) return( -1 ); } } #ifdef DEBUG VIPS_DEBUG_MSG( "boxes_new: generated %d boxes\n", boxes->n_hline ); boxes_hprint( boxes ); #endif /*DEBUG*/ return( 0 ); }
/* Process a buffer of data. */ void imb_XYZ2disp( float *p, PEL *q, int n, struct im_col_display *d ) { struct im_col_tab_disp *table = im_col_display_get_table( d ); float rstep = (d->d_YCR - d->d_Y0R) / 1500.0; float gstep = (d->d_YCG - d->d_Y0G) / 1500.0; float bstep = (d->d_YCB - d->d_Y0B) / 1500.0; int x; for( x = 0; x < n; x++ ) { float Yr, Yg, Yb; int i; int r, g, b; float X = p[0]; float Y = p[1]; float Z = p[2]; p += 3; /* Multiply through the matrix to get luminosity values. */ Yr = table->mat_XYZ2lum[0][0] * X + table->mat_XYZ2lum[0][1] * Y + table->mat_XYZ2lum[0][2] * Z; Yg = table->mat_XYZ2lum[1][0] * X + table->mat_XYZ2lum[1][1] * Y + table->mat_XYZ2lum[1][2] * Z; Yb = table->mat_XYZ2lum[2][0] * X + table->mat_XYZ2lum[2][1] * Y + table->mat_XYZ2lum[2][2] * Z; /* Clip -ves. */ Yr = IM_MAX( Yr, d->d_Y0R ); Yg = IM_MAX( Yg, d->d_Y0G ); Yb = IM_MAX( Yb, d->d_Y0B ); /* Turn luminosity to colour value. */ i = IM_MIN( 1500, (Yr - d->d_Y0R) / rstep ); r = table->t_Yr2r[i]; i = IM_MIN( 1500, (Yg - d->d_Y0G) / gstep ); g = table->t_Yg2g[i]; i = IM_MIN( 1500, (Yb - d->d_Y0B) / bstep ); b = table->t_Yb2b[i]; /* Clip output. */ r = IM_MIN( r, d->d_Vrwr ); g = IM_MIN( g, d->d_Vrwg ); b = IM_MIN( b, d->d_Vrwb ); q[0] = r; q[1] = g; q[2] = b; q += 3; } }
static LGrab * lgrab_new( const char *device ) { LGrab *lg = IM_NEW( NULL, LGrab ); int i; if( !lg ) return( NULL ); lg->device = NULL; lg->capture_buffer = NULL; lg->capture_size = 0; lg->fd = -1; lg->c_channel = -1; lg->c_width = -1; lg->c_height = -1; lg->c_ngrabs = 1; SETSTR( lg->device, device ); if( !lg->device || (lg->fd = open( lg->device, O_RDWR )) == -1 ) { im_error( "lgrab_new", _( "cannot open video device \"%s\"" ), lg->device ); lgrab_destroy( lg ); return( NULL ); } if( lgrab_ioctl( lg, VIDIOCGCAP, &lg->capability ) ) { im_error( "lgrab_new", "%s", _( "cannot get video capability" ) ); lgrab_destroy( lg ); return( NULL ); } /* Check that it can capture to memory. */ if( !(lg->capability.type & VID_TYPE_CAPTURE) ) { im_error( "lgrab_new", "%s", _( "card cannot capture to memory" ) ); lgrab_destroy( lg ); return( NULL ); } /* Read channel info. */ for( i = 0; i < IM_MIN( lg->capability.channels, IM_MAXCHANNELS ); i++ ) { lg->channel[i].channel = i; if( lgrab_ioctl( lg, VIDIOCGCHAN, &lg->channel[i] ) ) { lgrab_destroy( lg ); return( NULL ); } } /* Get other props. */ if( lgrab_ioctl( lg, VIDIOCGWIN, &lg->window) || lgrab_ioctl( lg, VIDIOCGPICT, &lg->picture) ) { lgrab_destroy( lg ); return( NULL ); } /* Set 24 bit mode. */ lg->picture.depth = 24; lg->picture.palette = VIDEO_PALETTE_RGB24; if( lgrab_ioctl( lg, VIDIOCSPICT, &lg->picture ) ) { lgrab_destroy( lg ); return( NULL ); } return( lg ); }
void imlib_illuminvar(image_t *img) // http://ai.stanford.edu/~alireza/publication/cic15.pdf { switch(img->bpp) { case IMAGE_BPP_BINARY: { break; } case IMAGE_BPP_GRAYSCALE: { break; } case IMAGE_BPP_RGB565: { for (int y = 0, yy = img->h; y < yy; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); for (int x = 0, xx = img->w; x < xx; x++) { int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x); #ifdef IMLIB_ENABLE_INVARIANT_TABLE int rgb565 = invariant_table[pixel]; #else float r_lin = xyz_table[COLOR_RGB565_TO_R8(pixel)] + 1.0; float g_lin = xyz_table[COLOR_RGB565_TO_G8(pixel)] + 1.0; float b_lin = xyz_table[COLOR_RGB565_TO_B8(pixel)] + 1.0; float r_lin_sharp = (r_lin * 0.9968f) + (g_lin * 0.0228f) + (b_lin * 0.0015f); float g_lin_sharp = (r_lin * -0.0071f) + (g_lin * 0.9933f) + (b_lin * 0.0146f); float b_lin_sharp = (r_lin * 0.0103f) + (g_lin * -0.0161f) + (b_lin * 0.9839f); float lin_sharp_avg = r_lin_sharp * g_lin_sharp * b_lin_sharp; lin_sharp_avg = (lin_sharp_avg > 0.0f) ? fast_cbrtf(lin_sharp_avg) : 0.0f; float r_lin_sharp_div = 0.0f; float g_lin_sharp_div = 0.0f; float b_lin_sharp_div = 0.0f; if (lin_sharp_avg > 0.0f) { lin_sharp_avg = 1.0f / lin_sharp_avg; r_lin_sharp_div = r_lin_sharp * lin_sharp_avg; g_lin_sharp_div = g_lin_sharp * lin_sharp_avg; b_lin_sharp_div = b_lin_sharp * lin_sharp_avg; } float r_lin_sharp_div_log = (r_lin_sharp_div > 0.0f) ? fast_log(r_lin_sharp_div) : 0.0f; float g_lin_sharp_div_log = (g_lin_sharp_div > 0.0f) ? fast_log(g_lin_sharp_div) : 0.0f; float b_lin_sharp_div_log = (b_lin_sharp_div > 0.0f) ? fast_log(b_lin_sharp_div) : 0.0f; float chi_x = (r_lin_sharp_div_log * 0.7071f) + (g_lin_sharp_div_log * -0.7071f) + (b_lin_sharp_div_log * 0.0000f); float chi_y = (r_lin_sharp_div_log * 0.4082f) + (g_lin_sharp_div_log * 0.4082f) + (b_lin_sharp_div_log * -0.8164f); float e_t_x = 0.9326f; float e_t_y = -0.3609f; float p_th_00 = e_t_x * e_t_x; float p_th_01 = e_t_x * e_t_y; float p_th_10 = e_t_y * e_t_x; float p_th_11 = e_t_y * e_t_y; float x_th_x = (p_th_00 * chi_x) + (p_th_01 * chi_y); float x_th_y = (p_th_10 * chi_x) + (p_th_11 * chi_y); float r_chi = (x_th_x * 0.7071f) + (x_th_y * 0.4082f); float g_chi = (x_th_x * -0.7071f) + (x_th_y * 0.4082f); float b_chi = (x_th_x * 0.0000f) + (x_th_y * -0.8164f); float r_chi_invariant = fast_expf(r_chi); float g_chi_invariant = fast_expf(g_chi); float b_chi_invariant = fast_expf(b_chi); float chi_invariant_sum = r_chi_invariant + g_chi_invariant + b_chi_invariant; float r_chi_invariant_m = 0.0f; float g_chi_invariant_m = 0.0f; float b_chi_invariant_m = 0.0f; if (chi_invariant_sum > 0.0f) { chi_invariant_sum = 1.0f / chi_invariant_sum; r_chi_invariant_m = r_chi_invariant * chi_invariant_sum; g_chi_invariant_m = g_chi_invariant * chi_invariant_sum; b_chi_invariant_m = b_chi_invariant * chi_invariant_sum; } int r_chi_invariant_m_int = IM_MAX(IM_MIN(r_chi_invariant_m * 255.0f, COLOR_R8_MAX), COLOR_R8_MIN); int g_chi_invariant_m_int = IM_MAX(IM_MIN(g_chi_invariant_m * 255.0f, COLOR_G8_MAX), COLOR_G8_MIN); int b_chi_invariant_m_int = IM_MAX(IM_MIN(b_chi_invariant_m * 255.0f, COLOR_B8_MAX), COLOR_B8_MIN); int rgb565 = COLOR_R8_G8_B8_TO_RGB565(r_chi_invariant_m_int, g_chi_invariant_m_int, b_chi_invariant_m_int); #endif IMAGE_PUT_RGB565_PIXEL_FAST(row_ptr, x, rgb565); } } break; } default: { break; } } }
/* Subsample a REGION. We fetch in IM_MAX_WIDTH pixel-wide strips, left-to-right * across the input. */ static int line_shrink_gen( REGION *or, void *seq, void *a, void *b ) { REGION *ir = (REGION *) seq; IMAGE *in = (IMAGE *) a; SubsampleInfo *st = (SubsampleInfo *) b; Rect *r = &or->valid; int le = r->left; int ri = IM_RECT_RIGHT( r ); int to = r->top; int bo = IM_RECT_BOTTOM(r); int ps = IM_IMAGE_SIZEOF_PEL( in ); int owidth = IM_MAX_WIDTH / st->xshrink; Rect s; int x, y; int z, k; /* Loop down the region. */ for( y = to; y < bo; y++ ) { char *q = IM_REGION_ADDR( or, le, y ); char *p; /* Loop across the region, in owidth sized pieces. */ for( x = le; x < ri; x += owidth ) { /* How many pixels do we make this time? */ int ow = IM_MIN( owidth, ri - x ); /* Ask for this many from input ... can save a * little here! */ int iw = ow * st->xshrink - (st->xshrink - 1); /* Ask for input. */ s.left = x * st->xshrink; s.top = y * st->yshrink; s.width = iw; s.height = 1; if( im_prepare( ir, &s ) ) return( -1 ); /* Append new pels to output. */ p = IM_REGION_ADDR( ir, s.left, s.top ); for( z = 0; z < ow; z++ ) { for( k = 0; k < ps; k++ ) q[k] = p[k]; q += ps; p += ps * st->xshrink; } } } return( 0 ); }
/* Loop over region, adding to seq. */ static int scan_fn( REGION *reg, void *vseq, void *a, void *b ) { Seq *seq = (Seq *) vseq; Rect *r = ®->valid; IMAGE *im = reg->im; int le = r->left; int to = r->top; int bo = IM_RECT_BOTTOM(r); int nel = IM_REGION_N_ELEMENTS( reg ); int x, y; double m; #ifdef DEBUG printf( "im_min: left = %d, top = %d, width = %d, height = %d\n", r->left, r->top, r->width, r->height ); #endif /*DEBUG*/ #define loop(TYPE) { \ m = *((TYPE *) IM_REGION_ADDR( reg, le, to )); \ \ for( y = to; y < bo; y++ ) { \ TYPE *p = (TYPE *) IM_REGION_ADDR( reg, le, y ); \ \ for( x = 0; x < nel; x++ ) { \ double v = p[x]; \ \ if( v < m ) \ m = v; \ } \ } \ } #define complex_loop(TYPE) { \ TYPE *p = (TYPE *) IM_REGION_ADDR( reg, le, to ); \ double real = p[0]; \ double imag = p[1]; \ \ m = real * real + imag * imag; \ \ for( y = to; y < bo; y++ ) { \ TYPE *p = (TYPE *) IM_REGION_ADDR( reg, le, y ); \ \ for( x = 0; x < nel * 2; x += 2 ) { \ double mod; \ \ real = p[x]; \ imag = p[x + 1]; \ mod = real * real + imag * imag; \ \ if( mod < m ) \ m = mod; \ } \ } \ } switch( im->BandFmt ) { case IM_BANDFMT_UCHAR: loop( unsigned char ); break; case IM_BANDFMT_CHAR: loop( signed char ); break; case IM_BANDFMT_USHORT: loop( unsigned short ); break; case IM_BANDFMT_SHORT: loop( signed short ); break; case IM_BANDFMT_UINT: loop( unsigned int ); break; case IM_BANDFMT_INT: loop( signed int ); break; case IM_BANDFMT_FLOAT: loop( float ); break; case IM_BANDFMT_DOUBLE: loop( double ); break; case IM_BANDFMT_COMPLEX: complex_loop( float ); break; case IM_BANDFMT_DPCOMPLEX: complex_loop( double ); break; default: assert( 0 ); } if( seq->valid ) { seq->value = IM_MIN( seq->value, m ); } else { seq->value = m; seq->valid = 1; } return( 0 ); }
void imlib_remove_shadows(image_t *img, const char *path, image_t *other, int scalar, bool single) { if (!single) { imlib_remove_shadows_line_op_state_t state; for (int i = 0; i < imlib_remove_shadows_kernel_size; i++) { state.img_lines[i] = fb_alloc(img->w * sizeof(uint16_t)); state.other_lines[i] = fb_alloc(img->w * sizeof(uint16_t)); state.out_lines[i] = fb_alloc(img->w * sizeof(uint16_t)); } state.lines_processed = 0; imlib_image_operation(img, path, other, scalar, imlib_remove_shadows_line_op, &state); for (int i = 0; i < imlib_remove_shadows_kernel_size; i++) { fb_free(); fb_free(); fb_free(); } } else { // Create Shadow Mask image_t temp_image; temp_image.w = img->w; temp_image.h = img->h; temp_image.bpp = img->bpp; temp_image.data = fb_alloc(image_size(img)); memcpy(temp_image.data, img->data, image_size(img)); rectangle_t r; r.x = 0; r.y = 0; r.w = temp_image.w; r.h = temp_image.h; histogram_t h; h.LBinCount = COLOR_L_MAX - COLOR_L_MIN + 1; h.ABinCount = COLOR_A_MAX - COLOR_A_MIN + 1; h.BBinCount = COLOR_B_MAX - COLOR_B_MIN + 1; h.LBins = fb_alloc(h.LBinCount * sizeof(float)); h.ABins = fb_alloc(h.ABinCount * sizeof(float)); h.BBins = fb_alloc(h.BBinCount * sizeof(float)); imlib_get_histogram(&h, &temp_image, &r, NULL, false); statistics_t s; imlib_get_statistics(&s, temp_image.bpp, &h); int sum = 0; int mean = s.LMean * 0.8f; for (int y = 0, yy = temp_image.h; y < yy; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(&temp_image, y); for (int x = 0, xx = temp_image.w; x < xx; x++) { sum += COLOR_RGB565_TO_L(IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x)) < mean; } } if (sum > ((temp_image.w * temp_image.h) / 20)) { // Don't do anything if the image is mostly flat. threshold_t t; imlib_get_threshold(&t, temp_image.bpp, &h); list_t thresholds; list_init(&thresholds, sizeof(color_thresholds_list_lnk_data_t)); color_thresholds_list_lnk_data_t lnk_data; lnk_data.LMin = COLOR_L_MIN; lnk_data.AMin = COLOR_A_MIN; lnk_data.BMin = COLOR_B_MIN; lnk_data.LMax = t.LValue; lnk_data.AMax = COLOR_A_MAX; lnk_data.BMax = COLOR_B_MAX; list_push_back(&thresholds, &lnk_data); imlib_binary(&temp_image, &temp_image, &thresholds, false, false, NULL); list_free(&thresholds); imlib_erode(&temp_image, 3, 30, NULL); imlib_dilate(&temp_image, 1, 1, NULL); // Get Shadow Average image_t temp_image_2; temp_image_2.w = temp_image.w; temp_image_2.h = temp_image.h; temp_image_2.bpp = temp_image.bpp; temp_image_2.data = fb_alloc(image_size(&temp_image)); memcpy(temp_image_2.data, temp_image.data, image_size(&temp_image)); imlib_erode(&temp_image_2, 3, 48, NULL); int shadow_r_sum = 0; int shadow_g_sum = 0; int shadow_b_sum = 0; int shadow_count = 0; for (int y = 0, yy = temp_image_2.h; y < yy; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(&temp_image_2, y); uint16_t *row_ptr_2 = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); for (int x = 0, xx = temp_image_2.w; x < xx; x++) { if (IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x)) { int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr_2, x); int r = COLOR_RGB565_TO_R8(pixel); int g = COLOR_RGB565_TO_G8(pixel); int b = COLOR_RGB565_TO_R8(pixel); shadow_r_sum += r; shadow_g_sum += g; shadow_b_sum += b; shadow_count += 1; } } } memcpy(temp_image_2.data, temp_image.data, image_size(&temp_image)); imlib_invert(&temp_image_2); imlib_erode(&temp_image_2, 5, 120, NULL); imlib_invert(&temp_image_2); imlib_b_xor(&temp_image_2, NULL, &temp_image, 0, NULL); imlib_erode(&temp_image_2, 2, 24, NULL); int not_shadow_r_sum = 0; int not_shadow_g_sum = 0; int not_shadow_b_sum = 0; int not_shadow_count = 0; for (int y = 0, yy = temp_image_2.h; y < yy; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(&temp_image_2, y); uint16_t *row_ptr_2 = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); for (int x = 0, xx = temp_image_2.w; x < xx; x++) { if (IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x)) { int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr_2, x); int r = COLOR_RGB565_TO_R8(pixel); int g = COLOR_RGB565_TO_G8(pixel); int b = COLOR_RGB565_TO_R8(pixel); not_shadow_r_sum += r; not_shadow_g_sum += g; not_shadow_b_sum += b; not_shadow_count += 1; } } } // Fill in the umbra... (inner part of the shadow)... memcpy(temp_image_2.data, temp_image.data, image_size(&temp_image)); imlib_mean_filter(&temp_image, 2, false, 0, false, NULL); if (shadow_count && not_shadow_count) { float shadow_r_average = ((float) shadow_r_sum) / ((float) shadow_count); float shadow_g_average = ((float) shadow_g_sum) / ((float) shadow_count); float shadow_b_average = ((float) shadow_b_sum) / ((float) shadow_count); float not_shadow_r_average = ((float) not_shadow_r_sum) / ((float) not_shadow_count); float not_shadow_g_average = ((float) not_shadow_g_sum) / ((float) not_shadow_count); float not_shadow_b_average = ((float) not_shadow_b_sum) / ((float) not_shadow_count); float diff_r = not_shadow_r_average - shadow_r_average; float diff_g = not_shadow_g_average - shadow_g_average; float diff_b = not_shadow_b_average - shadow_b_average; for (int y = 0; y < img->h; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(&temp_image, y); uint16_t *row_ptr_2 = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); for (int x = 0; x < img->w; x++) { float alpha = ((float) (COLOR_RGB565_TO_Y(IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, x)) - COLOR_Y_MIN)) / ((float) (COLOR_Y_MAX - COLOR_Y_MIN)); int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr_2, x); int r = COLOR_RGB565_TO_R8(pixel); int g = COLOR_RGB565_TO_G8(pixel); int b = COLOR_RGB565_TO_B8(pixel); int r_new = IM_MIN(IM_MAX(r + (diff_r * alpha), COLOR_R8_MIN), COLOR_R8_MAX); int g_new = IM_MIN(IM_MAX(g + (diff_g * alpha), COLOR_G8_MIN), COLOR_G8_MAX); int b_new = IM_MIN(IM_MAX(b + (diff_b * alpha), COLOR_B8_MIN), COLOR_B8_MAX); IMAGE_PUT_RGB565_PIXEL_FAST(row_ptr_2, x, COLOR_R8_G8_B8_TO_RGB565(r_new, g_new, b_new)); } } } // Fill in the penumbra... (outer part of the shadow)... memcpy(temp_image.data, temp_image_2.data, image_size(&temp_image_2)); imlib_erode(&temp_image_2, 1, 8, NULL); imlib_b_xor(&temp_image, NULL, &temp_image_2, 0, NULL); imlib_dilate(&temp_image, 3, 0, NULL); imlib_median_filter(img, 2, 12, false, 0, false, &temp_image); fb_free(); // temp_image_2 } fb_free(); // BBins fb_free(); // ABins fb_free(); // LBins fb_free(); // temp_image } }