SDL3_gfx 1.0.1 Graphics primitives and surface functions for SDL3

SDL3_imageFilter.c
Go to the documentation of this file.
1/*
2
3 SDL3_imageFilter.c: byte-image "filter" routines
4
5 Copyright (C) 2012-2014 Andreas Schiffler
6 Copyright (C) 2013 Sylvain Beucler
7
8 This software is provided 'as-is', without any express or implied
9 warranty. In no event will the authors be held liable for any damages
10 arising from the use of this software.
11
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it
14 freely, subject to the following restrictions:
15
16 1. The origin of this software must not be misrepresented; you must not
17 claim that you wrote the original software. If you use this software
18 in a product, an acknowledgment in the product documentation would be
19 appreciated but is not required.
20
21 2. Altered source versions must be plainly marked as such, and must not be
22 misrepresented as being the original software.
23
24 3. This notice may not be removed or altered from any source
25 distribution.
26
27 Andreas Schiffler -- aschiffler at ferzkopp dot net
28
29*/
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include <SDL3/SDL.h>
36
37#include "SDL3_imageFilter.h"
38
39/* ------ Custom defines ----- */
40
44#define SWAP_32(x) (((x) >> 24) | (((x) & 0x00ff0000) >> 8) | (((x) & 0x0000ff00) << 8) | ((x) << 24))
45
46/* ------------------------------------------------------------------------------------ */
47
58 int SDL_imageFilterAdd(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
59{
60 unsigned int i, istart;
61 unsigned char *cursrc1, *cursrc2, *curdst;
62 int result;
63
64 /* Validate input parameters */
65 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
66 return(-1);
67 if (length == 0)
68 return(0);
69
70 /* Setup to process whole image */
71 istart = 0;
72 cursrc1 = Src1;
73 cursrc2 = Src2;
74 curdst = Dest;
75
76 /* Routine to process image */
77 for (i = istart; i < length; i++) {
78 result = (int) *cursrc1 + (int) *cursrc2;
79 if (result > 255)
80 result = 255;
81 *curdst = (unsigned char) result;
82 /* Advance pointers */
83 cursrc1++;
84 cursrc2++;
85 curdst++;
86 }
87
88 return (0);
89}
90
101 int SDL_imageFilterMean(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
102{
103 static unsigned char Mask[8] = { 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };
104 unsigned int i, istart;
105 unsigned char *cursrc1, *cursrc2, *curdst;
106 int result;
107
108 /* Validate input parameters */
109 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
110 return(-1);
111 if (length == 0)
112 return(0);
113
114 /* Setup to process whole image */
115 istart = 0;
116 cursrc1 = Src1;
117 cursrc2 = Src2;
118 curdst = Dest;
119
120 /* Routine to process image */
121 for (i = istart; i < length; i++) {
122 result = (int) *cursrc1 / 2 + (int) *cursrc2 / 2;
123 *curdst = (unsigned char) result;
124 /* Advance pointers */
125 cursrc1++;
126 cursrc2++;
127 curdst++;
128 }
129
130 return (0);
131}
132
143 int SDL_imageFilterSub(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
144{
145 unsigned int i, istart;
146 unsigned char *cursrc1, *cursrc2, *curdst;
147 int result;
148
149 /* Validate input parameters */
150 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
151 return(-1);
152 if (length == 0)
153 return(0);
154
155 /* Setup to process whole image */
156 istart = 0;
157 cursrc1 = Src1;
158 cursrc2 = Src2;
159 curdst = Dest;
160
161 /* Routine to process image */
162 for (i = istart; i < length; i++) {
163 result = (int) cursrc1[i] - (int) cursrc2[i];
164 if (result < 0)
165 result = 0;
166 curdst[i] = (unsigned char) result;
167 }
168 return (0);
169}
170
181 int SDL_imageFilterAbsDiff(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
182{
183 unsigned int i, istart;
184 unsigned char *cursrc1, *cursrc2, *curdst;
185 int result;
186
187 /* Validate input parameters */
188 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
189 return(-1);
190 if (length == 0)
191 return(0);
192
193 /* Setup to process whole image */
194 istart = 0;
195 cursrc1 = Src1;
196 cursrc2 = Src2;
197 curdst = Dest;
198
199 /* Routine to process image */
200 for (i = istart; i < length; i++) {
201 result = abs((int) *cursrc1 - (int) *cursrc2);
202 *curdst = (unsigned char) result;
203 /* Advance pointers */
204 cursrc1++;
205 cursrc2++;
206 curdst++;
207 }
208
209 return (0);
210}
211
222 int SDL_imageFilterMult(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
223{
224 unsigned int i, istart;
225 unsigned char *cursrc1, *cursrc2, *curdst;
226 int result;
227
228 /* Validate input parameters */
229 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
230 return(-1);
231 if (length == 0)
232 return(0);
233
234 /* Setup to process whole image */
235 istart = 0;
236 cursrc1 = Src1;
237 cursrc2 = Src2;
238 curdst = Dest;
239
240 /* Routine to process image */
241 for (i = istart; i < length; i++) {
242 result = (int) *cursrc1 * (int) *cursrc2;
243 if (result > 255)
244 result = 255;
245 *curdst = (unsigned char) result;
246 /* Advance pointers */
247 cursrc1++;
248 cursrc2++;
249 curdst++;
250 }
251
252 return (0);
253}
254
265 int SDL_imageFilterMultNor(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
266{
267 unsigned int i, istart;
268 unsigned char *cursrc1, *cursrc2, *curdst;
269
270 /* Validate input parameters */
271 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
272 return(-1);
273 if (length == 0)
274 return(0);
275
276 /* Setup to process whole image */
277 istart = 0;
278 cursrc1 = Src1;
279 cursrc2 = Src2;
280 curdst = Dest;
281
282 /* Routine to process image */
283 for (i = istart; i < length; i++) {
284 *curdst = (int)*cursrc1 * (int)*cursrc2; // (int) for efficiency
285 /* Advance pointers */
286 cursrc1++;
287 cursrc2++;
288 curdst++;
289 }
290
291 return (0);
292}
293
304 int SDL_imageFilterMultDivby2(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
305{
306 unsigned int i, istart;
307 unsigned char *cursrc1, *cursrc2, *curdst;
308 int result;
309
310 /* Validate input parameters */
311 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
312 return(-1);
313 if (length == 0)
314 return(0);
315
316 /* Setup to process whole image */
317 istart = 0;
318 cursrc1 = Src1;
319 cursrc2 = Src2;
320 curdst = Dest;
321
322 /* Routine to process image */
323 for (i = istart; i < length; i++) {
324 result = ((int) *cursrc1 / 2) * (int) *cursrc2;
325 if (result > 255)
326 result = 255;
327 *curdst = (unsigned char) result;
328 /* Advance pointers */
329 cursrc1++;
330 cursrc2++;
331 curdst++;
332 }
333
334 return (0);
335}
336
347 int SDL_imageFilterMultDivby4(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
348{
349 unsigned int i, istart;
350 unsigned char *cursrc1, *cursrc2, *curdst;
351 int result;
352
353 /* Validate input parameters */
354 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
355 return(-1);
356 if (length == 0)
357 return(0);
358
359 /* Setup to process whole image */
360 istart = 0;
361 cursrc1 = Src1;
362 cursrc2 = Src2;
363 curdst = Dest;
364
365 /* C routine to process image */
366 for (i = istart; i < length; i++) {
367 result = ((int) *cursrc1 / 2) * ((int) *cursrc2 / 2);
368 if (result > 255)
369 result = 255;
370 *curdst = (unsigned char) result;
371 /* Advance pointers */
372 cursrc1++;
373 cursrc2++;
374 curdst++;
375 }
376
377 return (0);
378}
379
390 int SDL_imageFilterBitAnd(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
391{
392 unsigned int i, istart;
393 unsigned char *cursrc1, *cursrc2, *curdst;
394
395 /* Validate input parameters */
396 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
397 return(-1);
398 if (length == 0)
399 return(0);
400
401 /* Setup to process whole image */
402 istart = 0;
403 cursrc1 = Src1;
404 cursrc2 = Src2;
405 curdst = Dest;
406
407 /* Routine to process image */
408 for (i = istart; i < length; i++) {
409 *curdst = (*cursrc1) & (*cursrc2);
410 /* Advance pointers */
411 cursrc1++;
412 cursrc2++;
413 curdst++;
414 }
415
416 return (0);
417}
418
429 int SDL_imageFilterBitOr(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
430{
431 unsigned int i, istart;
432 unsigned char *cursrc1, *cursrc2, *curdst;
433
434 /* Validate input parameters */
435 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
436 return(-1);
437 if (length == 0)
438 return(0);
439
440 /* Setup to process whole image */
441 istart = 0;
442 cursrc1 = Src1;
443 cursrc2 = Src2;
444 curdst = Dest;
445
446 /* Routine to process image */
447 for (i = istart; i < length; i++) {
448 *curdst = *cursrc1 | *cursrc2;
449 /* Advance pointers */
450 cursrc1++;
451 cursrc2++;
452 curdst++;
453 }
454
455 return (0);
456}
457
468 int SDL_imageFilterDiv(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
469{
470 unsigned int i, istart;
471 unsigned char *cursrc1, *cursrc2, *curdst;
472
473 /* Validate input parameters */
474 if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
475 return(-1);
476 if (length == 0)
477 return(0);
478
479 /* Setup to process whole image */
480 istart = 0;
481 cursrc1 = Src1;
482 cursrc2 = Src2;
483 curdst = Dest;
484
485 for (i = istart; i < length; i++) {
486 if (*cursrc2 == 0) {
487 *curdst = 255;
488 } else {
489 *curdst = (int)*cursrc1 / (int)*cursrc2; // (int) for efficiency
490 }
491 /* Advance pointers */
492 cursrc1++;
493 cursrc2++;
494 curdst++;
495 }
496
497 return (0);
498}
499
500/* ------------------------------------------------------------------------------------ */
501
511 int SDL_imageFilterBitNegation(unsigned char *Src1, unsigned char *Dest, unsigned int length)
512{
513 unsigned int i, istart;
514 unsigned char *cursrc1, *curdst;
515
516 /* Validate input parameters */
517 if ((Src1 == NULL) || (Dest == NULL))
518 return(-1);
519 if (length == 0)
520 return(0);
521
522 /* Setup to process whole image */
523 istart = 0;
524 cursrc1 = Src1;
525 curdst = Dest;
526
527 /* Routine to process image */
528 for (i = istart; i < length; i++) {
529 *curdst = ~(*cursrc1);
530 /* Advance pointers */
531 cursrc1++;
532 curdst++;
533 }
534
535 return (0);
536}
537
548 int SDL_imageFilterAddByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
549{
550 unsigned int i, istart;
551 int iC;
552 unsigned char *cursrc1, *curdest;
553 int result;
554
555 /* Validate input parameters */
556 if ((Src1 == NULL) || (Dest == NULL))
557 return(-1);
558 if (length == 0)
559 return(0);
560
561 /* Special case: C==0 */
562 if (C == 0) {
563 memcpy(Src1, Dest, length);
564 return (0);
565 }
566
567 /* Setup to process whole image */
568 istart = 0;
569 cursrc1 = Src1;
570 curdest = Dest;
571
572 /* C routine to process image */
573 iC = (int) C;
574 for (i = istart; i < length; i++) {
575 result = (int) *cursrc1 + iC;
576 if (result > 255)
577 result = 255;
578 *curdest = (unsigned char) result;
579 /* Advance pointers */
580 cursrc1++;
581 curdest++;
582 }
583 return (0);
584}
585
597 int SDL_imageFilterAddUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned int bpp, unsigned int C)
598{
599 unsigned int i, j, istart, D;
600 int iC[bpp + 1], k;
601 unsigned char *cursrc1;
602 unsigned char *curdest;
603 int result;
604
605 /* Validate input parameters */
606 if ((Src1 == NULL) || (Dest == NULL))
607 return(-1);
608 if (length == 0)
609 return(0);
610
611 /* Special case: C==0 */
612 if (C == 0) {
613 memcpy(Src1, Dest, length);
614 return (0);
615 }
616
617 /* Setup to process whole image */
618 istart = 0;
619 cursrc1 = Src1;
620 curdest = Dest;
621
622 /* Routine to process uint */
623 for (k = bpp; k >= 0; k--) {
624 iC[k] = (int) ((C >> (8*(bpp - k))) & 0xff);
625 }
626
627 /* Routine to process bytes */
628 for (i = istart; i < length; i += bpp) {
629 for (j = 0; j < bpp; j++) {
630 if ((i+j) < length) {
631 result = cursrc1[i+j] + iC[j];
632 if (result > 255){
633 result = 255;
634 }
635 curdest[i+j] = (unsigned char) result;
636 }
637 }
638 }
639 return (0);
640}
641
652 int SDL_imageFilterAddByteToHalf(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
653{
654 static unsigned char Mask[8] = { 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };
655 unsigned int i, istart;
656 int iC;
657 unsigned char *cursrc1;
658 unsigned char *curdest;
659 int result;
660
661 /* Validate input parameters */
662 if ((Src1 == NULL) || (Dest == NULL))
663 return(-1);
664 if (length == 0)
665 return(0);
666
667 /* Setup to process whole image */
668 istart = 0;
669 cursrc1 = Src1;
670 curdest = Dest;
671
672 /* Routine to process image */
673 iC = (int) C;
674 for (i = istart; i < length; i++) {
675 result = (int) (*cursrc1 / 2) + iC;
676 if (result > 255)
677 result = 255;
678 *curdest = (unsigned char) result;
679 /* Advance pointers */
680 cursrc1++;
681 curdest++;
682 }
683
684 return (0);
685}
686
697 int SDL_imageFilterSubByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
698{
699 unsigned int i, istart;
700 int iC;
701 unsigned char *cursrc1;
702 unsigned char *curdest;
703 int result;
704
705 /* Validate input parameters */
706 if ((Src1 == NULL) || (Dest == NULL))
707 return(-1);
708 if (length == 0)
709 return(0);
710
711 /* Special case: C==0 */
712 if (C == 0) {
713 memcpy(Src1, Dest, length);
714 return (0);
715 }
716
717 /* Setup to process whole image */
718 istart = 0;
719 cursrc1 = Src1;
720 curdest = Dest;
721
722 /* Routine to process image */
723 iC = (int) C;
724 for (i = istart; i < length; i++) {
725 result = (int) *cursrc1 - iC;
726 if (result < 0)
727 result = 0;
728 *curdest = (unsigned char) result;
729 /* Advance pointers */
730 cursrc1++;
731 curdest++;
732 }
733 return (0);
734}
735
747 int SDL_imageFilterSubUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned int bpp, unsigned int C)
748{
749 unsigned int i, j, istart, D;
750 int iC[bpp], k;
751 unsigned char *cursrc1;
752 unsigned char *curdest;
753 int result;
754
755 /* Validate input parameters */
756 if ((Src1 == NULL) || (Dest == NULL))
757 return(-1);
758 if (length == 0)
759 return(0);
760
761 /* Special case: C==0 */
762 if (C == 0) {
763 memcpy(Src1, Dest, length);
764 return (0);
765 }
766
767 /* Setup to process whole image */
768 istart = 0;
769 cursrc1 = Src1;
770 curdest = Dest;
771
772 /* Routine to process uint */
773 for (k = bpp; k >= 0; k--) {
774 iC[k] = (int) ((C >> (8*(bpp - k))) & 0xff);
775 }
776
777 /* Routine to process bytes */
778 for (i = istart; i < length; i += bpp) {
779 for (j = 0; j < bpp; j++) {
780 if ((i+j) < length) {
781 result = cursrc1[i+j] - iC[j];
782 if (result < 0){
783 result = 0;
784 }
785 curdest[i+j] = (unsigned char) result;
786 }
787 }
788 }
789
790 return (0);
791}
792
803 int SDL_imageFilterShiftRight(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
804{
805 static unsigned char Mask[8] = { 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };
806 unsigned int i, istart;
807 unsigned char *cursrc1;
808 unsigned char *curdest;
809
810 /* Validate input parameters */
811 if ((Src1 == NULL) || (Dest == NULL))
812 return(-1);
813 if (length == 0)
814 return(0);
815
816 /* Check shift */
817 if (N > 8) {
818 return (-1);
819 }
820
821 /* Special case: N==0 */
822 if (N == 0) {
823 memcpy(Src1, Dest, length);
824 return (0);
825 }
826
827 /* Setup to process whole image */
828 istart = 0;
829 cursrc1 = Src1;
830 curdest = Dest;
831
832 /* Routine to process image */
833 for (i = istart; i < length; i++) {
834 *curdest = (unsigned char) *cursrc1 >> N;
835 /* Advance pointers */
836 cursrc1++;
837 curdest++;
838 }
839
840 return (0);
841}
842
853 int SDL_imageFilterShiftRightUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
854{
855 unsigned int i, istart;
856 unsigned char *cursrc1, *curdest;
857 unsigned int *icursrc1, *icurdest;
858 unsigned int result;
859
860 /* Validate input parameters */
861 if ((Src1 == NULL) || (Dest == NULL))
862 return(-1);
863 if (length == 0)
864 return(0);
865
866 if (N > 32) {
867 return (-1);
868 }
869
870 /* Special case: N==0 */
871 if (N == 0) {
872 memcpy(Src1, Dest, length);
873 return (0);
874 }
875
876 /* Setup to process whole image */
877 istart = 0;
878 cursrc1 = Src1;
879 curdest = Dest;
880
881 /* Routine to process image */
882 icursrc1=(unsigned int *)cursrc1;
883 icurdest=(unsigned int *)curdest;
884 for (i = istart; i < length; i += 4) {
885 if ((i+4)<length) {
886 result = ((unsigned int)*icursrc1 >> N);
887 *icurdest = result;
888 }
889 /* Advance pointers */
890 icursrc1++;
891 icurdest++;
892 }
893
894 return (0);
895}
896
907 int SDL_imageFilterMultByByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
908{
909 unsigned int i, istart;
910 int iC;
911 unsigned char *cursrc1;
912 unsigned char *curdest;
913 int result;
914
915 /* Validate input parameters */
916 if ((Src1 == NULL) || (Dest == NULL))
917 return(-1);
918 if (length == 0)
919 return(0);
920
921 /* Special case: C==1 */
922 if (C == 1) {
923 memcpy(Src1, Dest, length);
924 return (0);
925 }
926
927 /* Setup to process whole image */
928 istart = 0;
929 cursrc1 = Src1;
930 curdest = Dest;
931
932 /* Routine to process image */
933 iC = (int) C;
934 for (i = istart; i < length; i++) {
935 result = (int) *cursrc1 * iC;
936 if (result > 255)
937 result = 255;
938 *curdest = (unsigned char) result;
939 /* Advance pointers */
940 cursrc1++;
941 curdest++;
942 }
943
944 return (0);
945}
946
958 int SDL_imageFilterShiftRightAndMultByByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N,
959 unsigned char C)
960{
961 unsigned int i, istart;
962 int iC;
963 unsigned char *cursrc1;
964 unsigned char *curdest;
965 int result;
966
967 /* Validate input parameters */
968 if ((Src1 == NULL) || (Dest == NULL))
969 return(-1);
970 if (length == 0)
971 return(0);
972
973 /* Check shift */
974 if (N > 8) {
975 return (-1);
976 }
977
978 /* Special case: N==0 && C==1 */
979 if ((N == 0) && (C == 1)) {
980 memcpy(Src1, Dest, length);
981 return (0);
982 }
983
984 /* Setup to process whole image */
985 istart = 0;
986 cursrc1 = Src1;
987 curdest = Dest;
988
989 /* Routine to process image */
990 iC = (int) C;
991 for (i = istart; i < length; i++) {
992 result = (int) (*cursrc1 >> N) * iC;
993 if (result > 255)
994 result = 255;
995 *curdest = (unsigned char) result;
996 /* Advance pointers */
997 cursrc1++;
998 curdest++;
999 }
1000
1001 return (0);
1002}
1003
1014 int SDL_imageFilterShiftLeftByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
1015{
1016 static unsigned char Mask[8] = { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE };
1017 unsigned int i, istart;
1018 unsigned char *cursrc1, *curdest;
1019 int result;
1020
1021 /* Validate input parameters */
1022 if ((Src1 == NULL) || (Dest == NULL))
1023 return(-1);
1024 if (length == 0)
1025 return(0);
1026
1027 if (N > 8) {
1028 return (-1);
1029 }
1030
1031 /* Special case: N==0 */
1032 if (N == 0) {
1033 memcpy(Src1, Dest, length);
1034 return (0);
1035 }
1036
1037 /* Setup to process whole image */
1038 istart = 0;
1039 cursrc1 = Src1;
1040 curdest = Dest;
1041
1042 /* Routine to process image */
1043 for (i = istart; i < length; i++) {
1044 result = ((int) *cursrc1 << N) & 0xff;
1045 *curdest = (unsigned char) result;
1046 /* Advance pointers */
1047 cursrc1++;
1048 curdest++;
1049 }
1050
1051 return (0);
1052}
1053
1064 int SDL_imageFilterShiftLeftUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
1065{
1066 unsigned int i, istart;
1067 unsigned char *cursrc1, *curdest;
1068 unsigned int *icursrc1, *icurdest;
1069 unsigned int result;
1070
1071 /* Validate input parameters */
1072 if ((Src1 == NULL) || (Dest == NULL))
1073 return(-1);
1074 if (length == 0)
1075 return(0);
1076
1077 if (N > 32) {
1078 return (-1);
1079 }
1080
1081 /* Special case: N==0 */
1082 if (N == 0) {
1083 memcpy(Src1, Dest, length);
1084 return (0);
1085 }
1086
1087 /* Setup to process whole image */
1088 istart = 0;
1089 cursrc1 = Src1;
1090 curdest = Dest;
1091
1092 /* Routine to process image */
1093 icursrc1=(unsigned int *)cursrc1;
1094 icurdest=(unsigned int *)curdest;
1095 for (i = istart; i < length; i += 4) {
1096 if ((i+4)<length) {
1097 result = ((unsigned int)*icursrc1 << N);
1098 *icurdest = result;
1099 }
1100 /* Advance pointers */
1101 icursrc1++;
1102 icurdest++;
1103 }
1104
1105 return (0);
1106}
1107
1118 int SDL_imageFilterShiftLeft(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
1119{
1120 unsigned int i, istart;
1121 unsigned char *cursrc1, *curdest;
1122 int result;
1123
1124 /* Validate input parameters */
1125 if ((Src1 == NULL) || (Dest == NULL))
1126 return(-1);
1127 if (length == 0)
1128 return(0);
1129
1130 if (N > 8) {
1131 return (-1);
1132 }
1133
1134 /* Special case: N==0 */
1135 if (N == 0) {
1136 memcpy(Src1, Dest, length);
1137 return (0);
1138 }
1139
1140 /* Setup to process whole image */
1141 istart = 0;
1142 cursrc1 = Src1;
1143 curdest = Dest;
1144
1145 /* Routine to process image */
1146 for (i = istart; i < length; i++) {
1147 result = (int) *cursrc1 << N;
1148 if (result > 255)
1149 result = 255;
1150 *curdest = (unsigned char) result;
1151 /* Advance pointers */
1152 cursrc1++;
1153 curdest++;
1154 }
1155
1156 return (0);
1157}
1158
1169 int SDL_imageFilterBinarizeUsingThreshold(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char T)
1170{
1171 unsigned int i, istart;
1172 unsigned char *cursrc1;
1173 unsigned char *curdest;
1174
1175 /* Validate input parameters */
1176 if ((Src1 == NULL) || (Dest == NULL))
1177 return(-1);
1178 if (length == 0)
1179 return(0);
1180
1181 /* Special case: T==0 */
1182 if (T == 0) {
1183 memset(Dest, 255, length);
1184 return (0);
1185 }
1186
1187 /* Setup to process whole image */
1188 istart = 0;
1189 cursrc1 = Src1;
1190 curdest = Dest;
1191
1192 /* C routine to process image */
1193 for (i = istart; i < length; i++) {
1194 *curdest = (unsigned char)(((unsigned char)*cursrc1 >= T) ? 255 : 0);
1195 /* Advance pointers */
1196 cursrc1++;
1197 curdest++;
1198 }
1199
1200 return (0);
1201}
1202
1214 int SDL_imageFilterClipToRange(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char Tmin,
1215 unsigned char Tmax)
1216{
1217 unsigned int i, istart;
1218 unsigned char *cursrc1;
1219 unsigned char *curdest;
1220
1221 /* Validate input parameters */
1222 if ((Src1 == NULL) || (Dest == NULL))
1223 return(-1);
1224 if (length == 0)
1225 return(0);
1226
1227 /* Special case: Tmin==0 && Tmax = 255 */
1228 if ((Tmin == 0) && (Tmax == 25)) {
1229 memcpy(Src1, Dest, length);
1230 return (0);
1231 }
1232
1233 /* Setup to process whole image */
1234 istart = 0;
1235 cursrc1 = Src1;
1236 curdest = Dest;
1237
1238 /* Routine to process image */
1239 for (i = istart; i < length; i++) {
1240 if (*cursrc1 < Tmin) {
1241 *curdest = Tmin;
1242 } else if (*cursrc1 > Tmax) {
1243 *curdest = Tmax;
1244 } else {
1245 *curdest = *cursrc1;
1246 }
1247 /* Advance pointers */
1248 cursrc1++;
1249 curdest++;
1250 }
1251
1252 return (0);
1253}
1254
1268 int SDL_imageFilterNormalizeLinear(unsigned char *Src, unsigned char *Dest, unsigned int length, int Cmin, int Cmax, int Nmin,
1269 int Nmax)
1270{
1271 unsigned int i, istart;
1272 unsigned char *cursrc;
1273 unsigned char *curdest;
1274 int dN, dC, factor;
1275 int result;
1276
1277 /* Validate input parameters */
1278 if ((Src == NULL) || (Dest == NULL))
1279 return(-1);
1280 if (length == 0)
1281 return(0);
1282
1283 /* Setup to process whole image */
1284 istart = 0;
1285 cursrc = Src;
1286 curdest = Dest;
1287
1288 /* Routine to process image */
1289 dC = Cmax - Cmin;
1290 if (dC == 0)
1291 return (0);
1292 dN = Nmax - Nmin;
1293 factor = dN / dC;
1294 for (i = istart; i < length; i++) {
1295 result = factor * ((int) (*cursrc) - Cmin) + Nmin;
1296 if (result > 255)
1297 result = 255;
1298 *curdest = (unsigned char) result;
1299 /* Advance pointers */
1300 cursrc++;
1301 curdest++;
1302 }
1303
1304 return (0);
1305}
1306
1307/* ------------------------------------------------------------------------------------ */
1308
1323 int SDL_imageFilterConvolveKernel3x3Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1324 signed short *Kernel, unsigned char Divisor)
1325{
1326 /* Validate input parameters */
1327 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1328 return(-1);
1329
1330 if ((columns < 3) || (rows < 3) || (Divisor == 0))
1331 return (-1);
1332
1333 /* No implementation yet */
1334 return (-1);
1335}
1336
1351 int SDL_imageFilterConvolveKernel5x5Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1352 signed short *Kernel, unsigned char Divisor)
1353{
1354 /* Validate input parameters */
1355 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1356 return(-1);
1357
1358 if ((columns < 5) || (rows < 5) || (Divisor == 0))
1359 return (-1);
1360
1361 /* No implementation yet */
1362 return (-1);
1363}
1364
1379 int SDL_imageFilterConvolveKernel7x7Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1380 signed short *Kernel, unsigned char Divisor)
1381{
1382 /* Validate input parameters */
1383 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1384 return(-1);
1385
1386 if ((columns < 7) || (rows < 7) || (Divisor == 0))
1387 return (-1);
1388
1389 /* No implementation yet */
1390 return (-1);
1391}
1392
1407 int SDL_imageFilterConvolveKernel9x9Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1408 signed short *Kernel, unsigned char Divisor)
1409{
1410 /* Validate input parameters */
1411 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1412 return(-1);
1413
1414 if ((columns < 9) || (rows < 9) || (Divisor == 0))
1415 return (-1);
1416
1417 /* No implementation yet */
1418 return (-1);
1419}
1420
1435 int SDL_imageFilterConvolveKernel3x3ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1436 signed short *Kernel, unsigned char NRightShift)
1437{
1438 /* Validate input parameters */
1439 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1440 return(-1);
1441
1442 if ((columns < 3) || (rows < 3) || (NRightShift > 7))
1443 return (-1);
1444
1445 /* No implementation yet */
1446 return (-1);
1447}
1448
1463 int SDL_imageFilterConvolveKernel5x5ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1464 signed short *Kernel, unsigned char NRightShift)
1465{
1466 /* Validate input parameters */
1467 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1468 return(-1);
1469
1470 if ((columns < 5) || (rows < 5) || (NRightShift > 7))
1471 return (-1);
1472
1473 /* No implementation yet */
1474 return (-1);
1475}
1476
1491 int SDL_imageFilterConvolveKernel7x7ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1492 signed short *Kernel, unsigned char NRightShift)
1493{
1494 /* Validate input parameters */
1495 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1496 return(-1);
1497
1498 if ((columns < 7) || (rows < 7) || (NRightShift > 7))
1499 return (-1);
1500
1501 /* No implementation yet */
1502 return (-1);
1503}
1504
1519 int SDL_imageFilterConvolveKernel9x9ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1520 signed short *Kernel, unsigned char NRightShift)
1521{
1522 /* Validate input parameters */
1523 if ((Src == NULL) || (Dest == NULL) || (Kernel == NULL))
1524 return(-1);
1525
1526 if ((columns < 9) || (rows < 9) || (NRightShift > 7))
1527 return (-1);
1528
1529 /* No implementation yet */
1530 return (-1);
1531}
1532
1533/* ------------------------------------------------------------------------------------ */
1534
1547 int SDL_imageFilterSobelX(unsigned char *Src, unsigned char *Dest, int rows, int columns)
1548{
1549 /* Validate input parameters */
1550 if ((Src == NULL) || (Dest == NULL))
1551 return(-1);
1552
1553 if ((columns < 8) || (rows < 3))
1554 return (-1);
1555
1556 /* No implementation yet */
1557 return (-1);
1558}
1559
1573 int SDL_imageFilterSobelXShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns,
1574 unsigned char NRightShift)
1575{
1576 /* Validate input parameters */
1577 if ((Src == NULL) || (Dest == NULL))
1578 return(-1);
1579 if ((columns < 8) || (rows < 3) || (NRightShift > 7))
1580 return (-1);
1581
1582 /* No implementation yet */
1583 return (-1);
1584}
SDL_imageFilterSobelX
int SDL_imageFilterSobelX(unsigned char *Src, unsigned char *Dest, int rows, int columns)
Filter using SobelX: Dij = saturation255( ... )
Definition SDL3_imageFilter.c:1547
SDL_imageFilterMultByByte
int SDL_imageFilterMultByByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
Filter using MultByByte: D = saturation255(S * C)
Definition SDL3_imageFilter.c:907
SDL_imageFilterShiftRightAndMultByByte
int SDL_imageFilterShiftRightAndMultByByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N, unsigned char C)
Filter using ShiftRightAndMultByByte: D = saturation255((S >> N) * C)
Definition SDL3_imageFilter.c:958
SDL_imageFilterSobelXShiftRight
int SDL_imageFilterSobelXShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns, unsigned char NRightShift)
Filter using SobelXShiftRight: Dij = saturation255( ... )
Definition SDL3_imageFilter.c:1573
SDL_imageFilterDiv
int SDL_imageFilterDiv(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using Div: D = S1 / S2.
Definition SDL3_imageFilter.c:468
SDL_imageFilterShiftLeftUint
int SDL_imageFilterShiftLeftUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
Filter using ShiftLeftUint: D = ((uint)S << N)
Definition SDL3_imageFilter.c:1064
SDL_imageFilterMultDivby4
int SDL_imageFilterMultDivby4(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using MultDivby4: D = saturation255(S1/2 * S2/2)
Definition SDL3_imageFilter.c:347
SDL_imageFilterConvolveKernel7x7Divide
int SDL_imageFilterConvolveKernel7x7Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char Divisor)
Filter using ConvolveKernel7x7Divide: Dij = saturation0and255( ... )
Definition SDL3_imageFilter.c:1379
SDL_imageFilterSubByte
int SDL_imageFilterSubByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
Filter using SubByte: D = saturation0(S - C)
Definition SDL3_imageFilter.c:697
SDL_imageFilterSub
int SDL_imageFilterSub(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using Sub: D = saturation0(S1 - S2)
Definition SDL3_imageFilter.c:143
SDL_imageFilterShiftLeftByte
int SDL_imageFilterShiftLeftByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
Filter using ShiftLeftByte: D = (S << N)
Definition SDL3_imageFilter.c:1014
SDL_imageFilterAbsDiff
int SDL_imageFilterAbsDiff(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using AbsDiff: D = | S1 - S2 |.
Definition SDL3_imageFilter.c:181
SDL_imageFilterConvolveKernel7x7ShiftRight
int SDL_imageFilterConvolveKernel7x7ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char NRightShift)
Filter using ConvolveKernel7x7ShiftRight: Dij = saturation0and255( ... )
Definition SDL3_imageFilter.c:1491
SDL_imageFilterConvolveKernel5x5ShiftRight
int SDL_imageFilterConvolveKernel5x5ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char NRightShift)
Filter using ConvolveKernel5x5ShiftRight: Dij = saturation0and255( ... )
Definition SDL3_imageFilter.c:1463
SDL_imageFilterShiftRightUint
int SDL_imageFilterShiftRightUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
Filter using ShiftRightUint: D = saturation0((uint)S[i] >> N)
Definition SDL3_imageFilter.c:853
SDL_imageFilterSubUint
int SDL_imageFilterSubUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned int bpp, unsigned int C)
Filter using SubUint: D = saturation0(S[i] - Cs[i % BPP])
Definition SDL3_imageFilter.c:747
SDL_imageFilterBitOr
int SDL_imageFilterBitOr(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using BitOr: D = S1 | S2.
Definition SDL3_imageFilter.c:429
SDL_imageFilterMultNor
int SDL_imageFilterMultNor(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using MultNor: D = S1 * S2.
Definition SDL3_imageFilter.c:265
SDL_imageFilterShiftRight
int SDL_imageFilterShiftRight(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
Filter using ShiftRight: D = saturation0(S >> N)
Definition SDL3_imageFilter.c:803
SDL_imageFilterConvolveKernel9x9ShiftRight
int SDL_imageFilterConvolveKernel9x9ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char NRightShift)
Filter using ConvolveKernel9x9ShiftRight: Dij = saturation255( ... )
Definition SDL3_imageFilter.c:1519
SDL_imageFilterMultDivby2
int SDL_imageFilterMultDivby2(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using MultDivby2: D = saturation255(S1/2 * S2)
Definition SDL3_imageFilter.c:304
SDL_imageFilterAddByte
int SDL_imageFilterAddByte(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
Filter using AddByte: D = saturation255(S + C)
Definition SDL3_imageFilter.c:548
SDL_imageFilterBitAnd
int SDL_imageFilterBitAnd(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using BitAnd: D = S1 & S2.
Definition SDL3_imageFilter.c:390
SDL_imageFilterConvolveKernel3x3Divide
int SDL_imageFilterConvolveKernel3x3Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char Divisor)
Filter using ConvolveKernel3x3Divide: Dij = saturation0and255( ... )
Definition SDL3_imageFilter.c:1323
SDL_imageFilterBinarizeUsingThreshold
int SDL_imageFilterBinarizeUsingThreshold(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char T)
Filter using BinarizeUsingThreshold: D = (S >= T) ? 255:0.
Definition SDL3_imageFilter.c:1169
SDL_imageFilterAddUint
int SDL_imageFilterAddUint(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned int bpp, unsigned int C)
Filter using AddUint: D = saturation255((S[i] + Cs[i % BPP])
Definition SDL3_imageFilter.c:597
SDL_imageFilterShiftLeft
int SDL_imageFilterShiftLeft(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char N)
Filter ShiftLeft: D = saturation255(S << N)
Definition SDL3_imageFilter.c:1118
SDL_imageFilterAdd
int SDL_imageFilterAdd(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using Add: D = saturation255(S1 + S2)
Definition SDL3_imageFilter.c:58
SDL_imageFilterNormalizeLinear
int SDL_imageFilterNormalizeLinear(unsigned char *Src, unsigned char *Dest, unsigned int length, int Cmin, int Cmax, int Nmin, int Nmax)
Filter using NormalizeLinear: D = saturation255((Nmax - Nmin)/(Cmax - Cmin)*(S - Cmin) + Nmin)
Definition SDL3_imageFilter.c:1268
SDL_imageFilterClipToRange
int SDL_imageFilterClipToRange(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char Tmin, unsigned char Tmax)
Filter using ClipToRange: D = (S >= Tmin) & (S <= Tmax) S:Tmin | Tmax.
Definition SDL3_imageFilter.c:1214
SDL_imageFilterAddByteToHalf
int SDL_imageFilterAddByteToHalf(unsigned char *Src1, unsigned char *Dest, unsigned int length, unsigned char C)
Filter using AddByteToHalf: D = saturation255(S/2 + C)
Definition SDL3_imageFilter.c:652
SDL_imageFilterConvolveKernel3x3ShiftRight
int SDL_imageFilterConvolveKernel3x3ShiftRight(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char NRightShift)
Filter using ConvolveKernel3x3ShiftRight: Dij = saturation0and255( ... )
Definition SDL3_imageFilter.c:1435
SDL_imageFilterBitNegation
int SDL_imageFilterBitNegation(unsigned char *Src1, unsigned char *Dest, unsigned int length)
Filter using BitNegation: D = !S.
Definition SDL3_imageFilter.c:511
SDL_imageFilterConvolveKernel5x5Divide
int SDL_imageFilterConvolveKernel5x5Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char Divisor)
Filter using ConvolveKernel5x5Divide: Dij = saturation0and255( ... )
Definition SDL3_imageFilter.c:1351
SDL_imageFilterMean
int SDL_imageFilterMean(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using Mean: D = S1/2 + S2/2.
Definition SDL3_imageFilter.c:101
SDL_imageFilterConvolveKernel9x9Divide
int SDL_imageFilterConvolveKernel9x9Divide(unsigned char *Src, unsigned char *Dest, int rows, int columns, signed short *Kernel, unsigned char Divisor)
Filter using ConvolveKernel9x9Divide: Dij = saturation0and255( ... )
Definition SDL3_imageFilter.c:1407
SDL_imageFilterMult
int SDL_imageFilterMult(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
Filter using Mult: D = saturation255(S1 * S2)
Definition SDL3_imageFilter.c:222
SDL3_imageFilter.h