lib/PDF/API2/XS/ImagePNG.xs

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 145 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/PDF/API2/XS.pm ↑ Summary blib/lib/PDF/API2/XS/ImagePNG.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include <stdlib.h>
6 #include <stdint.h>
7  
8 0 int paeth_predictor (int a, int b, int c) {
9 0 int p = a + b - c;
10 0 int pa = abs(p - a);
11 0 int pb = abs(p - b);
12 0 int pc = abs(p - c);
13 0 ▶ if ((pa <= pb ) && (pa <= pc)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((pa <= pb ) && (pa <= pc)) { 0 0 0
if ((pa <= pb ) && (pa <= pc)) { 0 0 0
14 0 return a;
15 }
16 0 ▶ else if (pb <= pc) {
Branch: 1 branch
BranchTrueFalseTotal
else if (pb <= pc) { 0 0 0
17 0 return b;
18 }
19 else {
20 0 return c;
21 }
22 }
23  
24 MODULE = PDF::API2::XS::ImagePNG PACKAGE = PDF::API2::XS::ImagePNG
25 PROTOTYPES: DISABLE
26  
27 AV*
28 unfilter (AV * line, AV * prev, int filter, int bpp)
29 CODE:
30 0 int line_length = av_len(line);
31 0 uint8_t * in_array = (uint8_t *)malloc((line_length) * sizeof(uint8_t));
32 0 uint8_t * prev_array = (uint8_t *)malloc((line_length) * sizeof(uint8_t));
33 0 uint8_t * out_array = (uint8_t *)malloc((line_length) * sizeof(uint8_t));
34 0 ▶ if (in_array == NULL || out_array == NULL || prev_array == NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (in_array == NULL || out_array == NULL || prev_array == NULL) { 0 0 0
if (in_array == NULL || out_array == NULL || prev_array == NULL) { 0 0 0
if (in_array == NULL || out_array == NULL || prev_array == NULL) { 0 0 0
35 0 croak("Null pointer from memory allocation in ImagePNG.xs");
36 }
37  
38 0 ▶ for (int i = 0; i < line_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < line_length; i++) { 0 0 0
39 0 SV** elem = av_fetch(line, i, 0);
40 0 char * ptr = SvPV_nolen(*elem);
41 0 uint8_t byte = (uint8_t) *ptr;
42 0 *(in_array + i) = byte;
43 }
44  
45 0 ▶ for (int i = 0; i < line_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < line_length; i++) { 0 0 0
46 uint8_t byte;
47 0 SV** elem = av_fetch(prev, i, 0);
48 0 ▶ if (elem != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (elem != NULL) { 0 0 0
49 0 char * ptr = SvPV_nolen(*elem);
50 0 byte = (uint8_t) *ptr;
51 }
52 else {
53 0 byte = 0;
54 }
55 0 *(prev_array + i) = byte;
56 }
57  
58 0 switch (filter) {
59 0 case 0 :
60 0 ▶ for (int i = 0; i < line_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < line_length; i++) { 0 0 0
61 0 *(out_array + i) = *(in_array + i);
62 }
63 0 break;
64  
65 0 case 1 :
66 0 ▶ for (int i = 0; i < line_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < line_length; i++) { 0 0 0
67 uint8_t sub;
68 0 ▶ if (i < bpp) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < bpp) { 0 0 0
69 0 sub = *(in_array + i);
70 }
71 else {
72 0 sub = *(in_array + i) + *(out_array + i - bpp);
73 }
74 0 *(out_array + i) = sub;
75 }
76 0 break;
77  
78 0 case 2 :
79 0 ▶ for (int i = 0; i < line_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < line_length; i++) { 0 0 0
80 0 *(out_array + i) = *(in_array + i) + *(prev_array + i);
81 }
82 0 break;
83  
84 0 case 3 :
85 0 ▶ for (int i = 0; i < line_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < line_length; i++) { 0 0 0
86 uint8_t sub;
87 0 ▶ if (i < bpp) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < bpp) { 0 0 0
88 0 sub = *(in_array + i) + (*(prev_array + i) / 2);
89 }
90 else {
91 0 sub = *(in_array + i) + ((*(out_array + i - bpp) + *(prev_array + i)) / 2);
92 }
93 0 *(out_array + i) = sub;
94 }
95 0 break;
96  
97 0 case 4 :
98 0 ▶ for (int i = 0; i < line_length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < line_length; i++) { 0 0 0
99 uint8_t a, b, c;
100 0 b = *(prev_array + i);
101 0 ▶ if (i < bpp) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < bpp) { 0 0 0
102 0 a = 0;
103 0 c = 0;
104 }
105 else {
106 0 a = *(out_array + i - bpp);
107 0 c = *(prev_array + i - bpp);
108 }
109 0 *(out_array + i) = *(in_array + i) + paeth_predictor(a, b, c);
110 }
111 0 break;
112 }
113  
114 // Put the results back into a new Perl AV.
115 0 AV * clearstream_av = newAV();
116 0 ▶ for (int i = 0; i < (line_length); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < (line_length); i++) { 0 0 0
117 0 SV * this_sv = newSVuv(*(out_array + i));
118 0 av_push(clearstream_av, this_sv);
119 }
120  
121 0 free(in_array);
122 0 free(out_array);
123 0 free(prev_array);
124  
125 0 RETVAL = clearstream_av;
126 OUTPUT:
127 RETVAL
128  
129 AV*
130 split_channels (AV * stream, int w, int h)
131 CODE:
132 //
133 // The image is passed as a Perl AV (Array Variable).
134 //
135 // It cannot be passed in as a regular C char string
136 // or converted to a regular C char string because
137 // it gets truncated at the first zero byte.
138 //
139 // First we need to turn it into a C array of bytes.
140 // av_len, av_fetch and SvPV_nolen are XS macros.
141 // See documentation here: https://perldoc.perl.org/perlguts.html
142 //
143 0 uint8_t * in_array = (uint8_t *)malloc((w * h * 4) * sizeof(uint8_t));
144 0 uint8_t * out_array = (uint8_t *)malloc((w * h * 4) * sizeof(uint8_t));
145 0 uint8_t * dict_array = (uint8_t *)malloc((w * h) * sizeof(uint8_t));
146  
147 0 ▶ if (in_array == NULL || out_array == NULL || dict_array == NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (in_array == NULL || out_array == NULL || dict_array == NULL) { 0 0 0
if (in_array == NULL || out_array == NULL || dict_array == NULL) { 0 0 0
if (in_array == NULL || out_array == NULL || dict_array == NULL) { 0 0 0
148 0 croak("Null pointer from memory allocation in ImagePNG.xs");
149 }
150  
151 0 ▶ for (int i = 0; i < av_len(stream); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < av_len(stream); i++) { 0 0 0
152 0 SV** elem = av_fetch(stream, i, 0);
153 0 char * ptr = SvPV_nolen(*elem);
154 0 uint8_t byte = (uint8_t) *ptr;
155 0 *(in_array + i) = byte;
156 }
157  
158 // Transform the image into a new C array of bytes.
159 0 ▶ for (int i = 0; i < w * h; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < w * h; i++) { 0 0 0
160 0 *(out_array + (i * 3) + 0) = *(in_array + (i * 4) + 0);
161 0 *(out_array + (i * 3) + 1) = *(in_array + (i * 4) + 1);
162 0 *(out_array + (i * 3) + 2) = *(in_array + (i * 4) + 2);
163  
164 0 *(dict_array + i) = *(in_array + (i * 4) + 3);
165 }
166  
167 // Put the results back into a new Perl AV.
168 0 AV * outstream_av = newAV();
169 0 ▶ for (int i = 0; i < (w * h * 3); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < (w * h * 3); i++) { 0 0 0
170 0 SV * this_sv = newSVuv(*(out_array + i));
171 0 av_push(outstream_av, this_sv);
172 }
173  
174 0 ▶ for (int i = 0; i < (w * h * 3); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < (w * h * 3); i++) { 0 0 0
175 0 SV * this_sv = newSVuv(*(dict_array + i));
176 0 av_push(outstream_av, this_sv);
177 }
178  
179 0 free(in_array);
180 0 free(out_array);
181 0 free(dict_array);
182  
183 // Send the transformed image back to Perl in the new AV.
184 0 RETVAL = outstream_av;
185 OUTPUT:
186 RETVAL
« blib/lib/PDF/API2/XS.pm ↑ Summary blib/lib/PDF/API2/XS/ImagePNG.pm »