-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.c
More file actions
1672 lines (1545 loc) · 44.2 KB
/
cli.c
File metadata and controls
1672 lines (1545 loc) · 44.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
// Already included in corner_detection.c when debugging
#ifndef DEBUG_LOGGING
#define STB_IMAGE_WRITE_IMPLEMENTATION
#endif
#include "stb_image_write.h"
#include "binary_closing_disk.h"
#include "conversion.h"
#include "crop.h"
#include "draw.h"
#include "exif.h"
#include "extract_document.h"
#include "flip.h"
#include "foerstner_corner.h"
#include "histogram.h"
#include "perspectivetransform.h"
#include "rgba_to_grayscale.h"
#include "rotate.h"
#include "single_to_multichannel.h"
#include "sobel_edge_detection.h"
#include "trim.h"
#include "watershed_segmentation.h"
typedef struct {
char operation[32];
double param;
double param2;
double param3;
double param4;
char param_str[64]; // For string parameters like "50%" or "200x300"
int32_t has_param;
int32_t has_param2;
int32_t has_param3;
int32_t has_param4;
int32_t has_string_param;
} PipelineOp;
typedef struct {
PipelineOp *ops;
int32_t count;
int32_t capacity;
} Pipeline;
void print32_t_usage(const char *program_name) {
printf("Usage: %s <input> <pipeline> <output>\n", program_name);
printf("Pipeline operations:\n");
printf(" grayscale - Convert image to grayscale\n");
printf(" blur <radius> - Apply gaussian blur with radius\n");
printf(" resize <50%%> - Resize image uniformly by percentage\n");
printf(" resize <50%%x80%%> - Resize with different x and y percentages\n");
printf(" resize <200x300> - Resize to absolute dimensions\n");
printf(" threshold - Apply Otsu threshold\n");
printf(" bw_smart - Smart black and white conversion\n");
printf(
" bw_smooth - Smooth (anti-aliased) black and white conversion\n"
);
printf(" detect_corners - Detect corners and output as JSON\n");
printf(
" draw_corners - Detect corners and draw circles at each corner\n"
);
printf(" sobel - Apply Sobel edge detection\n");
printf(
" circle <hex_color> <radius> <x>x<y> - Draw a colored circle at position "
"(x,y)\n"
);
printf(
" disk <hex_color> <radius> <x>x<y> - Draw a filled colored disk at "
"position "
"(x,y)\n"
);
printf(
" watershed '<x1>x<y1> <x2>x<y2> ...' - Watershed segmentation with "
"markers at "
"specified coordinates\n"
);
printf(" crop <widthxheight+x+y> - Crop the image\n");
printf(
" extract_document - Extract document using corner detection and "
"perspective transform (auto-size)\n"
);
printf(
" extract_document_to <output_width>x<output_height> - Extract document "
"to specific dimensions\n"
);
printf(
" flip_x - Flip image horizontally (mirror along vertical axis)\n"
);
printf(
" flip_y - Flip image vertically (mirror along horizontal axis)\n"
);
printf(
" rotate <angle> - Rotate image by angle (must be multiple of 90)\n"
);
printf(
" trim [<threshold>%%] - Remove border pixels with same color (optional "
"threshold for JPEG artifacts/vignette)\n"
);
printf(" histogram - Generate brightness histogram visualization\n");
printf(
" border <hex_color> <border_width> - Add colored border around image\n"
);
printf(" erode <radius> - Binary erosion with disk structuring element\n");
printf(" dilate <radius> - Binary dilation with disk structuring element\n");
printf(" close <radius> - Binary closing (dilation then erosion)\n");
printf(" open <radius> - Binary opening (erosion then dilation)\n");
printf("\nPipeline syntax:\n");
printf(" Operations are applied in sequence\n");
printf(" Use parentheses for operations with parameters: (blur 3.0)\n");
printf("\nExamples:\n");
printf(" %s input.jpg grayscale output.jpg\n", program_name);
printf(" %s input.jpg resize 50%% output.jpg\n", program_name);
printf(" %s input.jpg resize '50%%x200%%' output.jpg\n", program_name);
printf(" %s input.jpg resize 800x600 output.jpg\n", program_name);
printf(
" %s input.jpg \"grayscale, resize 50%%, blur 2\" output.jpg\n",
program_name
);
printf(
" %s input.jpg \"circle FF0000 50 200x150\" output.jpg\n",
program_name
);
printf(
" %s input.jpg \"disk 00FF00 30 100x200\" output.jpg\n",
program_name
);
printf(
" %s input.jpg \"watershed '100x50 200x150 300x100'\" output.jpg\n",
program_name
);
printf(" %s input.jpg \"extract_document\" output.jpg\n", program_name);
printf(
" %s input.jpg \"extract_document_to 800x600\" output.jpg\n",
program_name
);
printf(" %s input.jpg \"border FF0000 10\" output.jpg\n", program_name);
}
Pipeline *create_pipeline(void) {
Pipeline *p = malloc(sizeof(Pipeline));
p->capacity = 8;
p->count = 0;
p->ops = malloc(sizeof(PipelineOp) * p->capacity);
return p;
}
void free_pipeline(Pipeline *p) {
if (p) {
free(p->ops);
free(p);
}
}
int32_t pipeline_has_binarization(const Pipeline *pipeline) {
for (int32_t i = 0; i < pipeline->count; i++) {
const char *op = pipeline->ops[i].operation;
if (strcmp(op, "threshold") == 0 || strcmp(op, "bw_smart") == 0 ||
strcmp(op, "bw_smooth") == 0) {
return 1;
}
}
return 0;
}
void add_operation(
Pipeline *p,
const char *op,
double param,
int32_t has_param,
double param2,
int32_t has_param2,
double param3,
int32_t has_param3,
double param4,
int32_t has_param4,
const char *param_str,
int32_t has_string_param
) {
if (p->count >= p->capacity) {
p->capacity *= 2;
p->ops = realloc(p->ops, sizeof(PipelineOp) * p->capacity);
}
strncpy(
p->ops[p->count].operation,
op,
sizeof(p->ops[p->count].operation) - 1
);
p->ops[p->count].operation[sizeof(p->ops[p->count].operation) - 1] = '\0';
p->ops[p->count].param = param;
p->ops[p->count].has_param = has_param;
p->ops[p->count].param2 = param2;
p->ops[p->count].has_param2 = has_param2;
p->ops[p->count].param3 = param3;
p->ops[p->count].has_param3 = has_param3;
p->ops[p->count].param4 = param4;
p->ops[p->count].has_param4 = has_param4;
if (has_string_param && param_str) {
strncpy(
p->ops[p->count].param_str,
param_str,
sizeof(p->ops[p->count].param_str) - 1
);
p->ops[p->count].param_str[sizeof(p->ops[p->count].param_str) - 1] = '\0';
}
else {
p->ops[p->count].param_str[0] = '\0';
}
p->ops[p->count].has_string_param = has_string_param;
p->count++;
}
/* Remove leading and trailing white-space, returns pointer to first
non-blank char (string is modified in place). */
static char *trim_whitespace(char *s) {
while (*s && isspace((uint8_t)*s)) {
s++; // left-trim
}
if (*s == '\0') {
return s;
}
char *end = s + strlen(s) - 1;
while (end > s && isspace((uint8_t)*end)) {
end--;
}
*(end + 1) = '\0'; // right-trim
return s;
}
char *skip_whitespace(char *str) {
while (*str && isspace(*str)) {
str++;
}
return str;
}
/**
* Parse geometry string (e.g., "50x50+10+20")
* Returns 1 on success, 0 on failure
*/
int32_t parse_geometry(
const char *geometry,
uint32_t *width,
uint32_t *height,
int32_t *x_offset,
int32_t *y_offset
) {
if (!geometry || !width || !height || !x_offset || !y_offset) {
return 0;
}
// Parse width x height
char *end_ptr;
unsigned long w = strtoul(geometry, &end_ptr, 10);
if (*end_ptr != 'x' || w == 0 || w > UINT_MAX) {
return 0;
}
const char *height_start = end_ptr + 1;
unsigned long h = strtoul(height_start, &end_ptr, 10);
if (h == 0 || h > UINT_MAX) {
return 0;
}
*width = (uint32_t)w;
*height = (uint32_t)h;
// Parse offsets (can be + or -)
if (*end_ptr == '\0') {
// No offsets specified, default to 0,0
*x_offset = 0;
*y_offset = 0;
return 1;
}
// Parse x offset
long x = strtol(end_ptr, &end_ptr, 10);
if (x < INT_MIN || x > INT_MAX) {
return 0;
}
*x_offset = (int32_t)x;
// Parse y offset
if (*end_ptr == '\0') {
// Only x offset specified, y offset defaults to 0
*y_offset = 0;
return 1;
}
long y = strtol(end_ptr, &end_ptr, 10);
if (y < INT_MIN || y > INT_MAX || *end_ptr != '\0') {
return 0;
}
*y_offset = (int32_t)y;
return 1;
}
int32_t parse_pipeline(
int32_t argc,
char *argv[],
int32_t start_idx,
int32_t end_idx,
Pipeline *pipeline
) {
(void)argc; // Unused parameter
/* 1. build one big string from all arguments that form the pipeline */
size_t total_len = 0;
for (int32_t i = start_idx; i < end_idx; ++i) {
total_len += strlen(argv[i]) + 2; // space OR ", "
}
char *combined = malloc(total_len + 1);
if (!combined) {
fprintf(stderr, "Error: out of memory\n");
return 0;
}
combined[0] = '\0';
for (int32_t i = start_idx; i < end_idx; ++i) {
const char *tok = argv[i];
/* add space before every token except the first one */
if (i > start_idx) {
strcat(combined, " ");
}
strcat(combined, tok);
}
/* 2. split the combined string by commas – each chunk is one operation */
char *saveptr;
for (char *tok = strtok_r(combined, ",", &saveptr); tok;
tok = strtok_r(NULL, ",", &saveptr)) {
char *piece = trim_whitespace(tok);
if (*piece == '\0') {
continue; // empty fragment, skip
}
/* optional surrounding parentheses */
size_t len = strlen(piece);
if (piece[0] == '(' && piece[len - 1] == ')') {
piece[len - 1] = '\0';
piece = trim_whitespace(piece + 1);
}
if (*piece == '\0') {
continue;
}
/* split into operation name and (optional) parameters */
char *space = strchr(piece, ' ');
if (space) {
*space = '\0';
char *params_str = trim_whitespace(space + 1);
/* Check if this is a resize operation with special formats */
if (strcmp(piece, "resize") == 0) {
/* Check for percentage format (50% or 50%x80%) or absolute format
* (200x300) */
if (strchr(params_str, '%') || strchr(params_str, 'x')) {
add_operation(
pipeline,
piece,
0.0,
0,
0.0,
0,
0.0,
0,
0.0,
0,
params_str,
1
);
}
else {
/* Fall back to numeric parsing for backward compatibility */
char *space2 = strchr(params_str, ' ');
if (space2) {
*space2 = '\0';
double param1 = atof(trim_whitespace(params_str));
double param2 = atof(trim_whitespace(space2 + 1));
add_operation(
pipeline,
piece,
param1,
1,
param2,
1,
0.0,
0,
0.0,
0,
NULL,
0
);
}
else {
double param = atof(params_str);
add_operation(
pipeline,
piece,
param,
1,
0.0,
0,
0.0,
0,
0.0,
0,
NULL,
0
);
}
}
}
else if (strcmp(piece, "circle") == 0) {
/* Circle operation requires: hex_color radius xXy */
char *space2 = strchr(params_str, ' ');
if (space2) {
*space2 = '\0';
char *color = trim_whitespace(params_str);
char *remaining = trim_whitespace(space2 + 1);
char *space3 = strchr(remaining, ' ');
if (space3) {
*space3 = '\0';
double radius = atof(trim_whitespace(remaining));
char *position_str = trim_whitespace(space3 + 1);
// Parse position in format "x×y" or "xxy"
char *pos_copy = strdup(position_str);
char *x_pos = strchr(pos_copy, 'x');
if (x_pos) {
*x_pos = '\0';
double center_x = atof(trim_whitespace(pos_copy));
double center_y = atof(trim_whitespace(x_pos + 1));
// Store color in param_str, radius in param, x,y in param2,param3
char combined_params[128];
snprintf(
combined_params,
sizeof(combined_params),
"%s %.2f %.2f",
color,
center_x,
center_y
);
add_operation(
pipeline,
piece,
radius,
1,
center_x,
1,
center_y,
1,
0.0,
0,
combined_params,
1
);
free(pos_copy);
}
else {
fprintf(
stderr,
"Error: circle position must be in format 'xXy' (e.g., "
"'200x150')\n"
);
free(pos_copy);
free(combined);
return 0;
}
}
else {
fprintf(
stderr,
"Error: circle operation requires: hex_color radius xXy\n"
);
free(combined);
return 0;
}
}
else {
fprintf(
stderr,
"Error: circle operation requires: hex_color radius xXy\n"
);
free(combined);
return 0;
}
}
else if (strcmp(piece, "disk") == 0) {
/* Disk operation requires: hex_color radius xXy */
char *space2 = strchr(params_str, ' ');
if (space2) {
*space2 = '\0';
char *color = trim_whitespace(params_str);
char *remaining = trim_whitespace(space2 + 1);
char *space3 = strchr(remaining, ' ');
if (space3) {
*space3 = '\0';
double radius = atof(trim_whitespace(remaining));
char *position_str = trim_whitespace(space3 + 1);
// Parse position in format "x×y" or "xxy"
char *pos_copy = strdup(position_str);
char *x_pos = strchr(pos_copy, 'x');
if (x_pos) {
*x_pos = '\0';
double center_x = atof(trim_whitespace(pos_copy));
double center_y = atof(trim_whitespace(x_pos + 1));
// Store color in param_str, radius in param, x,y in param2,param3
char combined_params[128];
snprintf(
combined_params,
sizeof(combined_params),
"%s %.2f %.2f",
color,
center_x,
center_y
);
add_operation(
pipeline,
piece,
radius,
1,
center_x,
1,
center_y,
1,
0.0,
0,
combined_params,
1
);
free(pos_copy);
}
else {
fprintf(
stderr,
"Error: disk position must be in format 'xXy' (e.g., "
"'200x150')\n"
);
free(pos_copy);
free(combined);
return 0;
}
}
else {
fprintf(
stderr,
"Error: disk operation requires: hex_color radius xXy\n"
);
free(combined);
return 0;
}
}
else {
fprintf(
stderr,
"Error: disk operation requires: hex_color radius xXy\n"
);
free(combined);
return 0;
}
}
else if (strcmp(piece, "watershed") == 0) {
/* Watershed operation requires: x1×y1,x2×y2,... */
add_operation(
pipeline,
piece,
0.0,
0,
0.0,
0,
0.0,
0,
0.0,
0,
params_str,
1
);
}
else if (strcmp(piece, "crop") == 0) {
uint32_t crop_width, crop_height;
int32_t x_offset, y_offset;
if (parse_geometry(
params_str,
&crop_width,
&crop_height,
&x_offset,
&y_offset
)) {
add_operation(
pipeline,
piece,
(double)x_offset,
1,
(double)y_offset,
1,
(double)crop_width,
1,
(double)crop_height,
1,
NULL,
0
);
}
else {
fprintf(
stderr,
"Error: crop operation requires geometry format (e.g., "
"50x50+10+20)\n"
);
free(combined);
return 0;
}
}
else if (strcmp(piece, "extract_document_to") == 0) {
// Parse output dimensions in format "widthxheight"
char *params_copy = strdup(params_str);
char *x_pos = strchr(params_copy, 'x');
if (x_pos) {
*x_pos = '\0';
uint32_t out_width = (uint32_t)atoi(trim_whitespace(params_copy));
uint32_t out_height = (uint32_t)atoi(trim_whitespace(x_pos + 1));
if (out_width > 0 && out_height > 0) {
add_operation(
pipeline,
piece,
(double)out_width,
1,
(double)out_height,
1,
0.0,
0,
0.0,
0,
NULL,
0
);
}
else {
fprintf(
stderr,
"Error: extract_document_to requires positive dimensions\n"
);
free(params_copy);
free(combined);
return 0;
}
free(params_copy);
}
else {
fprintf(
stderr,
"Error: extract_document_to operation requires format "
"'widthxheight' (e.g., 800x600)\n"
);
free(params_copy);
free(combined);
return 0;
}
}
else if (strcmp(piece, "extract_document") == 0) {
// Auto-size version - no parameters needed
add_operation(pipeline, piece, 0.0, 0, 0.0, 0, 0.0, 0, 0.0, 0, NULL, 0);
}
else if (strcmp(piece, "border") == 0) {
/* Border operation requires: hex_color border_width */
char *space2 = strchr(params_str, ' ');
if (space2) {
*space2 = '\0';
char *color = trim_whitespace(params_str);
double border_width = atof(trim_whitespace(space2 + 1));
if (border_width > 0) {
add_operation(
pipeline,
piece,
border_width,
1,
0.0,
0,
0.0,
0,
0.0,
0,
color,
1
);
}
else {
fprintf(stderr, "Error: border width must be positive\n");
free(combined);
return 0;
}
}
else {
fprintf(
stderr,
"Error: border operation requires: hex_color border_width\n"
);
free(combined);
return 0;
}
}
else if (strcmp(piece, "trim") == 0) {
/* Trim operation with optional threshold percentage */
if (strchr(params_str, '%')) {
/* Percentage format: 2% */
add_operation(
pipeline,
piece,
0.0,
0,
0.0,
0,
0.0,
0,
0.0,
0,
params_str,
1
);
}
else {
/* Numeric threshold (interpreted as percentage) */
double param = atof(params_str);
add_operation(
pipeline,
piece,
param,
1,
0.0,
0,
0.0,
0,
0.0,
0,
NULL,
0
);
}
}
else {
/* Regular numeric parameter parsing for other operations */
char *space2 = strchr(params_str, ' ');
if (space2) {
*space2 = '\0';
double param1 = atof(trim_whitespace(params_str));
double param2 = atof(trim_whitespace(space2 + 1));
add_operation(
pipeline,
piece,
param1,
1,
param2,
1,
0.0,
0,
0.0,
0,
NULL,
0
);
}
else {
double param = atof(params_str);
add_operation(
pipeline,
piece,
param,
1,
0.0,
0,
0.0,
0,
0.0,
0,
NULL,
0
);
}
}
}
else {
add_operation(pipeline, piece, 0.0, 0, 0.0, 0, 0.0, 0, 0.0, 0, NULL, 0);
}
}
free(combined);
return 1;
}
uint8_t *apply_operation(
int32_t *width,
int32_t *height,
const char *operation,
double param,
int32_t has_param,
double param2,
int32_t has_param2,
double param3,
int32_t has_param3,
double param4,
int32_t has_param4,
const char *param_str,
int32_t has_string_param,
uint8_t *input_data
) {
if (strcmp(operation, "grayscale") == 0) {
return (uint8_t *)fcv_grayscale(*width, *height, input_data);
}
else if (strcmp(operation, "blur") == 0) {
if (!has_param) {
fprintf(stderr, "Error: blur operation requires radius parameter\n");
return NULL;
}
return (
uint8_t *
)fcv_apply_gaussian_blur(*width, *height, param, input_data);
}
else if (strcmp(operation, "resize") == 0) {
double resize_x, resize_y;
if (has_string_param) {
// Parse new resize formats: 50%, 50%x80%, 200x300
char *param_copy = strdup(param_str);
char *x_pos = strchr(param_copy, 'x');
if (x_pos) {
// Format: 50%x80% or 200x300
*x_pos = '\0';
char *first_part = trim_whitespace(param_copy);
char *second_part = trim_whitespace(x_pos + 1);
if (strchr(first_part, '%')) {
// Percentage format: 50%x80%
resize_x = atof(first_part) / 100.0;
resize_y = strchr(second_part, '%') ? atof(second_part) / 100.0
: atof(second_part) / 100.0;
}
else {
// Absolute size format: 200x300
resize_x = atof(first_part) / *width;
resize_y = atof(second_part) / *height;
}
}
else if (strchr(param_copy, '%')) {
// Uniform percentage format: 50%
resize_x = resize_y = atof(param_copy) / 100.0;
}
else {
fprintf(stderr, "Error: Invalid resize format '%s'\n", param_str);
free(param_copy);
return NULL;
}
free(param_copy);
}
else if (has_param) {
// Backward compatibility: numeric parameters
resize_x = param;
resize_y = has_param2 ? param2 : param;
}
else {
fprintf(stderr, "Error: resize operation requires resize parameter\n");
return NULL;
}
uint32_t out_width, out_height;
uint8_t *result = (uint8_t *)fcv_resize(
*width,
*height,
resize_x,
resize_y,
&out_width,
&out_height,
input_data
);
if (result) {
*width = out_width;
*height = out_height;
}
return result;
}
else if (strcmp(operation, "threshold") == 0) {
return (
uint8_t *
)fcv_otsu_threshold_rgba(*width, *height, false, input_data);
}
else if (strcmp(operation, "bw_smart") == 0) {
return (uint8_t *)fcv_bw_smart(*width, *height, false, input_data);
}
else if (strcmp(operation, "bw_smooth") == 0) {
return (uint8_t *)fcv_bw_smart(*width, *height, true, input_data);
}
else if (strcmp(operation, "detect_corners") == 0) {
Corners corners = fcv_detect_corners(input_data, *width, *height);
printf(" {\n");
printf(" \"corners\": {\n");
printf(" \"top_left\": [%.0f, %.0f],\n", corners.tl_x, corners.tl_y);
printf(" \"top_right\": [%.0f, %.0f],\n", corners.tr_x, corners.tr_y);
printf(
" \"bottom_right\": [%.0f, %.0f],\n",
corners.br_x,
corners.br_y
);
printf(" \"bottom_left\": [%.0f, %.0f]\n", corners.bl_x, corners.bl_y);
printf(" }\n");
printf(" }\n");
// Return a copy of the input data without modification
uint8_t *result = malloc(*width * *height * 4);
if (result) {
memcpy(result, input_data, *width * *height * 4);
}
return result;
}
else if (strcmp(operation, "draw_corners") == 0) {
Corners cs = fcv_detect_corners(input_data, *width, *height);
printf(" Detected corners:\n");
printf(" Top-left: (%.0f, %.0f)\n", cs.tl_x, cs.tl_y);
printf(" Top-right: (%.0f, %.0f)\n", cs.tr_x, cs.tr_y);
printf(" Bottom-right: (%.0f, %.0f)\n", cs.br_x, cs.br_y);
printf(" Bottom-left: (%.0f, %.0f)\n", cs.bl_x, cs.bl_y);
// Create a copy of the input data and draw disks at detected corners
uint32_t img_length_byte = (*width) * (*height) * 4;
uint8_t *result = malloc(img_length_byte);
if (!result) {
return NULL;
}
memcpy(result, input_data, img_length_byte);
// Draw disks using red color and `radius`
const double radius = fmin(*width, *height) * 0.02;
const char *red = "FF0000";
fcv_draw_disk(*width, *height, 4, red, radius, cs.tl_x, cs.tl_y, result);
fcv_draw_disk(*width, *height, 4, red, radius, cs.tr_x, cs.tr_y, result);
fcv_draw_disk(*width, *height, 4, red, radius, cs.br_x, cs.br_y, result);
fcv_draw_disk(*width, *height, 4, red, radius, cs.bl_x, cs.bl_y, result);
return result;
}
else if (strcmp(operation, "sobel") == 0) {
uint8_t *grayscale_sobel =
fcv_sobel_edge_detection(*width, *height, 4, input_data);
if (!grayscale_sobel) {
return NULL;
}
// Convert single-channel grayscale to RGBA format
uint8_t *rgba_result =
fcv_single_to_multichannel(*width, *height, grayscale_sobel);
free(grayscale_sobel);
return (uint8_t *)rgba_result;
}
else if (strcmp(operation, "circle") == 0) {
if (!has_string_param || !has_param || !has_param2 || !has_param3) {
fprintf(
stderr,
"Error: circle operation requires: hex_color radius xXy\n"
);
return NULL;
}
// Parse color and coordinates from param_str
char *param_copy = strdup(param_str);
char *color = strtok(param_copy, " ");
double center_x = param2;
double center_y = param3;
double radius = param;
uint32_t img_length_byte = (*width) * (*height) * 4;
uint8_t *result = malloc(img_length_byte);
if (!result) {
free(param_copy);
return NULL;
}
memcpy(result, input_data, img_length_byte);