32 #ifndef CACHE_LINE_LENGTH
33 #define CACHE_LINE_LENGTH 64
63 #if __GLIBC__ > 2 || (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 16)
77 if (img->
buf != NULL) {
98 output->
ts = input->
ts;
116 memcpy(&old_a, a,
sizeof(
struct image_t));
119 memcpy(a,
b,
sizeof(
struct image_t));
122 memcpy(
b, &old_a,
sizeof(
struct image_t));
138 output->
ts = input->
ts;
143 for (
int y = 0; y < output->
h; y++) {
144 for (
int x = 0; x < output->
w; x++) {
174 output->
ts = input->
ts;
177 for (
uint16_t y = 0; y < output->
h; y++) {
178 for (
uint16_t x = 0; x < output->
w; x += 2) {
196 char u = source[0] - 127;
235 if (x % 2 == 1) { x--; }
238 if (x < 0 || x >= im->
w || y < 0 || y >= im->
h) {
244 buf += 2 * (y * (im->
w) + x);
277 if (x % 2 == 1) { x--; }
280 if (x < 0 || x >= im->
w || y < 0 || y >= im->
h) {
286 buf += 2 * (y * (im->
w) + x);
318 if((downsample & (downsample - 1)) != 0){
319 for(
int8_t i = 7; i > 0; i--){
320 if(downsample & (1<<i)){
321 downsample &= (1<<(i));
330 uint16_t pixelskip = (downsample - 1) * 2;
332 output->
w = input->
w / downsample;
333 output->
h = input->
h / downsample;
337 output->
ts = input->
ts;
340 for (
uint16_t y = 0; y < output->
h; y++) {
341 for (
uint16_t x = 0; x < output->
w; x += 2) {
350 source += pixelskip * input->
w;
364 image_create(output, input->
w + 2 * border_size, input->
h + 2 * border_size, input->
type);
370 for (
uint16_t i = border_size; i != (output->
h - border_size); i++) {
373 for (
uint8_t j = 0; j != border_size; j++) {
374 output_buf[i * output->
w + (border_size - 1 - j)] = input_buf[(i - border_size) * input->
w + j];
378 memcpy(&output_buf[i * output->
w + border_size], &input_buf[(i - border_size) * input->
w],
sizeof(
uint8_t) * input->
w);
381 for (
uint8_t j = 0; j != border_size; j++) {
382 output_buf[i * output->
w + output->
w - border_size + j] = output_buf[i * output->
w + output->
w - border_size - 1 - j];
387 for (
uint8_t i = 0; i != border_size; i++) {
388 memcpy(&output_buf[(border_size - 1) * output->
w - i * output->
w], &output_buf[border_size * output->
w + i * output->
w],
390 memcpy(&output_buf[(output->
h - border_size) * output->
w + i * output->
w],
391 &output_buf[(output->
h - border_size - 1) * output->
w - i * output->
w],
sizeof(
uint8_t) * output->
w);
409 image_create(output, (input->
w + 1 - 2 * border_size) / 2, (input->
h + 1 - 2 * border_size) / 2, input->
type);
418 for (
uint16_t i = 0; i != output->
h; i++) {
420 for (
uint16_t j = 0; j != output->
w; j++) {
421 row = border_size + 2 * i;
422 col = border_size + 2 * j;
424 sum = 39 * (input_buf[(row - 2) *
w + (col - 2)] + input_buf[(row - 2) *
w + (col + 2)] +
425 input_buf[(row + 2) *
w + (col - 2)] + input_buf[(row + 2) *
w + (col + 2)]);
426 sum += 156 * (input_buf[(row - 2) *
w + (col - 1)] + input_buf[(row - 2) *
w + (col + 1)] +
427 input_buf[(row - 1) *
w + (col + 2)] + input_buf[(row + 1) *
w + (col - 2)]
428 + input_buf[(row + 1) *
w + (col + 2)] + input_buf[(row + 2) *
w + (col - 1)] + input_buf[(row + 2) *
w + (col + 1)] +
429 input_buf[(row - 1) *
w + (col - 2)]);
430 sum += 234 * (input_buf[(row - 2) *
w + (col)] + input_buf[(row) *
w + (col - 2)] +
431 input_buf[(row) *
w + (col + 2)] + input_buf[(row + 2) *
w + (col)]);
432 sum += 625 * (input_buf[(row - 1) *
w + (col - 1)] + input_buf[(row - 1) *
w + (col + 1)] +
433 input_buf[(row + 1) *
w + (col - 1)] + input_buf[(row + 1) *
w + (col + 1)]);
434 sum += 938 * (input_buf[(row - 1) *
w + (col)] + input_buf[(row) *
w + (col - 1)] +
435 input_buf[(row) *
w + (col + 1)] + input_buf[(row + 1) *
w + (col)]);
436 sum += 1406 * input_buf[(row) *
w + (col)];
438 output_buf[i * output->
w + j] = sum / 10000;
461 for (
uint8_t i = 1; i != pyr_level + 1; i++) {
489 uint32_t subpixel_w = (input->
w - 2) * subpixel_factor;
490 uint32_t subpixel_h = (input->
h - 2) * subpixel_factor;
493 for (
uint16_t i = 0; i < output->
w; i++) {
494 for (
uint16_t j = 0; j < output->
h; j++) {
496 uint32_t x = center->
x + border_size * subpixel_factor + (i - half_window) * subpixel_factor;
497 uint32_t y = center->
y + border_size * subpixel_factor + (j - half_window) * subpixel_factor;
499 BoundUpper(x, subpixel_w);
500 BoundUpper(y, subpixel_h);
503 uint16_t orig_x = x / subpixel_factor;
504 uint16_t orig_y = y / subpixel_factor;
507 uint32_t tl_x = orig_x * subpixel_factor;
508 uint32_t tl_y = orig_y * subpixel_factor;
511 if (tl_x == x && tl_y == y) {
512 output_buf[output->
w * j + i] = input_buf[input->
w * orig_y + orig_x];
519 uint32_t blend = (subpixel_factor - alpha_x) * (subpixel_factor - alpha_y) * input_buf[input->
w * orig_y + orig_x];
520 blend += alpha_x * (subpixel_factor - alpha_y) * input_buf[input->
w * orig_y + (orig_x + 1)];
521 blend += (subpixel_factor - alpha_x) * alpha_y * input_buf[input->
w * (orig_y + 1) + orig_x];
522 blend += alpha_x * alpha_y * input_buf[input->
w * (orig_y + 1) + (orig_x + 1)];
525 output_buf[output->
w * j + i] = blend / (subpixel_factor * subpixel_factor);
546 for (
uint16_t x = 1; x < input->
w - 1; x++) {
547 for (
uint16_t y = 1; y < input->
h - 1; y++) {
548 dx_buf[(y - 1)*dx->
w + (x - 1)] = (
int16_t)input_buf[y * input->
w + x + 1] - (
int16_t)input_buf[y * input->
w + x - 1];
549 dy_buf[(y - 1)*dy->
w + (x - 1)] = (
int16_t)input_buf[(y + 1) * input->
w + x] - (
int16_t)
550 input_buf[(y - 1) * input->
w + x];
564 int32_t sum_dxx = 0, sum_dxy = 0, sum_dyy = 0;
573 sum_dxx += ((
int32_t)dx_buf[y * dx->
w + x] * dx_buf[y * dx->
w + x]);
574 sum_dxy += ((
int32_t)dx_buf[y * dx->
w + x] * dy_buf[y * dy->
w + x]);
575 sum_dyy += ((
int32_t)dy_buf[y * dy->
w + x] * dy_buf[y * dy->
w + x]);
580 g[0] = sum_dxx / 255;
581 g[1] = sum_dxy / 255;
583 g[3] = sum_dyy / 255;
609 for (
uint16_t x = 0; x < img_b->
w; x++) {
610 for (
uint16_t y = 0; y < img_b->
h; y++) {
611 int16_t diff_c = img_a_buf[(y + 1) * img_a->
w + (x + 1)] - img_b_buf[y * img_b->
w + x];
612 sum_diff2 += diff_c * diff_c;
615 if (diff_buf != NULL) {
616 diff_buf[y * diff->
w + x] = diff_c;
645 for (
uint16_t x = 0; x < img_a->
w; x++) {
646 for (
uint16_t y = 0; y < img_a->
h; y++) {
647 int32_t mult_c = img_a_buf[y * img_a->
w + x] * img_b_buf[y * img_b->
w + x];
651 if (mult_buf != NULL) {
652 mult_buf[y * mult->
w + x] = mult_c;
696 int size_crosshair = 5;
699 for (
int i = 0; i < points_cnt; i++) {
701 uint32_t idx = pixel_width * points[i].
y * img->
w + points[i].
x * pixel_width;
717 static uint8_t color[4] = {255, 255, 255, 255};
718 static uint8_t bad_color[4] = {0, 0, 0, 0};
735 static int size_crosshair = 5;
738 for (
uint16_t i = 0; i < points_cnt; i++) {
741 .
x = vectors[i].
pos.
x / subpixel_factor,
742 .y = vectors[i].
pos.
y / subpixel_factor
745 .
x = (
uint32_t)roundf(((
float)vectors[i].pos.x + vectors[i].
flow_x) / subpixel_factor),
746 .
y = (
uint32_t)roundf(((
float)vectors[i].pos.y + vectors[i].
flow_y) / subpixel_factor)
770 int gradient_x, gradient_y, index;
777 uint8_t add_ind = pixel_width - 1;
780 if (loc->
x >= 1 && (loc->
x + 1) < img->
w && loc->
y >= 1 && (loc->
y + 1) < img->
h) {
788 index = loc->
y * img->
w * pixel_width + (loc->
x - 1) * pixel_width;
789 gradient_x -= (int) img_buf[index + add_ind];
790 index = loc->
y * img->
w * pixel_width + (loc->
x + 1) * pixel_width;
791 gradient_x += (int) img_buf[index + add_ind];
793 index = (loc->
y - 1) * img->
w * pixel_width + loc->
x * pixel_width;
794 gradient_y -= (
int) img_buf[index + add_ind];
795 index = (loc->
y + 1) * img->
w * pixel_width + loc->
x * pixel_width;
796 gradient_y += (
int) img_buf[index + add_ind];
802 static int Sobel[9] = { -1, 0, 1, -2, 0, 2, -1, 0, 1};
803 static int total_sobel = 8;
807 for (
int x = -1;
x <= 1;
x++) {
808 for (
int y = -1;
y <= 1;
y++) {
809 index = (loc->
y +
y) * img->
w * pixel_width + (loc->
x +
x) * pixel_width;
811 filt_ind_x = (
x + 1) % 3 + (
y + 1) * 3;
812 gradient_x += Sobel[filt_ind_x] * (int) img_buf[index + add_ind];
815 gradient_y += Sobel[filt_ind_y] * (int) img_buf[index + add_ind];
820 gradient_x /= total_sobel;
884 if (loc->
x >= size_crosshair && loc->
x < img->
w - size_crosshair
885 && loc->
y >= size_crosshair && loc->
y < img->
h - size_crosshair) {
887 from.
x = loc->
x - size_crosshair;
889 to.
x = loc->
x + size_crosshair;
893 from.
y = loc->
y - size_crosshair;
895 to.
y = loc->
y + size_crosshair;
908 static uint8_t color[4] = {255, 255, 255, 255};
923 int xerr = 0, yerr = 0;
929 uint8_t temp_color[4] = {color[0], color[1], color[2], color[3]};
940 if (delta_x > 0) { incx = 1; }
941 else if (delta_x == 0) { incx = 0; }
944 if (delta_y > 0) { incy = 1; }
945 else if (delta_y == 0) { incy = 0; }
950 delta_x = abs(delta_x);
951 delta_y = abs(delta_y);
952 if (delta_x > delta_y) { distance = delta_x * 20; }
953 else { distance = delta_y * 20; }
956 for (
uint16_t t = 0; starty < img->
h && startx < img->
w
957 && t <= distance + 1; t++) {
960 if (startx % 2 == 1) {
961 temp_color[0] = color[2];
962 temp_color[2] = color[0];
964 temp_color[0] = color[0];
965 temp_color[2] = color[2];
967 uint32_t buf_loc = img->
w * pixel_width * starty + startx * pixel_width;
968 img_buf[buf_loc] = temp_color[0];
971 img_buf[buf_loc + 1] = temp_color[1];
973 if (startx + 1 < img->
w) {
974 img_buf[buf_loc + 2] = temp_color[2];
975 img_buf[buf_loc + 3] = temp_color[3];
981 if (xerr > distance) {
985 if (yerr > distance) {