File: ImagePNG.xs

package info (click to toggle)
libpdf-api2-xs-perl 1.002-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 160 kB
  • sloc: perl: 35; makefile: 3
file content (186 lines) | stat: -rw-r--r-- 5,128 bytes parent folder | download
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
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdlib.h>
#include <stdint.h>

int paeth_predictor (int a, int b, int c) {
    int p = a + b - c;
    int pa = abs(p - a);
    int pb = abs(p - b);
    int pc = abs(p - c);
    if ((pa <= pb ) && (pa <= pc)) {
        return a;
    }
    else if (pb <= pc) {
        return b;
    }
    else {
        return c;
    }
}

MODULE = PDF::API2::XS::ImagePNG  PACKAGE = PDF::API2::XS::ImagePNG
PROTOTYPES: DISABLE

AV*
unfilter (AV * line, AV * prev, int filter, int bpp)
  CODE:
    int line_length = av_len(line);
    uint8_t * in_array   = (uint8_t *)malloc((line_length) * sizeof(uint8_t));
    uint8_t * prev_array = (uint8_t *)malloc((line_length) * sizeof(uint8_t));
    uint8_t * out_array  = (uint8_t *)malloc((line_length) * sizeof(uint8_t));
    if (in_array == NULL || out_array == NULL || prev_array == NULL) { 
      croak("Null pointer from memory allocation in ImagePNG.xs");
    }

    for (int i = 0; i < line_length; i++) {
      SV** elem = av_fetch(line, i, 0);
      char * ptr = SvPV_nolen(*elem);
      uint8_t byte = (uint8_t) *ptr;
      *(in_array + i) = byte;
    }

    for (int i = 0; i < line_length; i++) {
      uint8_t byte;
      SV** elem = av_fetch(prev, i, 0);
      if (elem != NULL) {
        char * ptr = SvPV_nolen(*elem);
        byte = (uint8_t) *ptr;
      }
      else {
        byte = 0;
      }
      *(prev_array + i) = byte;
    }

    switch (filter) {
      case 0 :
        for (int i = 0; i < line_length; i++) {
          *(out_array + i) = *(in_array + i);
        }
        break;

      case 1 :
        for (int i = 0; i < line_length; i++) {
          uint8_t sub;
          if (i < bpp) {
            sub = *(in_array + i);
          }
          else {
            sub = *(in_array + i) + *(out_array + i - bpp);
          }
          *(out_array + i) = sub;
        }
        break;

      case 2 :
        for (int i = 0; i < line_length; i++) {
          *(out_array + i) = *(in_array + i) + *(prev_array + i);
        }
        break;

      case 3 :
        for (int i = 0; i < line_length; i++) {
          uint8_t sub;
          if (i < bpp) {
            sub = *(in_array + i) + (*(prev_array + i) / 2);
          }
          else {
            sub = *(in_array + i) + ((*(out_array + i - bpp) + *(prev_array + i)) / 2);
          }
          *(out_array + i) = sub;
        }
        break;

      case 4 :
        for (int i = 0; i < line_length; i++) {
          uint8_t a, b, c;
          b = *(prev_array + i);
          if (i < bpp) {
            a = 0;
            c = 0;
          }
          else {
            a = *(out_array + i - bpp);
            c = *(prev_array + i - bpp);
          }
          *(out_array + i) = *(in_array + i) + paeth_predictor(a, b, c);
        }
        break;
    }

    // Put the results back into a new Perl AV.
    AV * clearstream_av = newAV();
    for (int i = 0; i < (line_length); i++) {
      SV * this_sv = newSVuv(*(out_array + i));
      av_push(clearstream_av, this_sv);
    }

    free(in_array);
    free(out_array);
    free(prev_array);

    RETVAL = clearstream_av;
  OUTPUT:
    RETVAL

AV*
split_channels (AV * stream, int w, int h)
  CODE:
    //
    // The image is passed as a Perl AV (Array Variable).
    //
    // It cannot be passed in as a regular C char string
    // or converted to a regular C char string because
    // it gets truncated at the first zero byte.
    //
    // First we need to turn it into a C array of bytes.
    // av_len, av_fetch and SvPV_nolen are XS macros.
    // See documentation here: https://perldoc.perl.org/perlguts.html
    //
    uint8_t * in_array = (uint8_t *)malloc((w * h * 4) * sizeof(uint8_t));
    uint8_t * out_array = (uint8_t *)malloc((w * h * 4) * sizeof(uint8_t));
    uint8_t * dict_array = (uint8_t *)malloc((w * h) * sizeof(uint8_t));

    if (in_array == NULL || out_array == NULL || dict_array == NULL) {
      croak("Null pointer from memory allocation in ImagePNG.xs");
    }

    for (int i = 0; i < av_len(stream); i++) {
      SV** elem = av_fetch(stream, i, 0);
      char * ptr = SvPV_nolen(*elem);
      uint8_t byte = (uint8_t) *ptr;
      *(in_array + i) = byte;
    }

    // Transform the image into a new C array of bytes.
    for (int i = 0; i < w * h; i++) {
      *(out_array + (i * 3) + 0) = *(in_array + (i * 4) + 0);
      *(out_array + (i * 3) + 1) = *(in_array + (i * 4) + 1);
      *(out_array + (i * 3) + 2) = *(in_array + (i * 4) + 2);

      *(dict_array + i) = *(in_array + (i * 4) + 3);
    }

    // Put the results back into a new Perl AV.
    AV * outstream_av = newAV();
    for (int i = 0; i < (w * h * 3); i++) {
      SV * this_sv = newSVuv(*(out_array + i));
      av_push(outstream_av, this_sv);
    }

    for (int i = 0; i < (w * h * 3); i++) {
      SV * this_sv = newSVuv(*(dict_array + i));
      av_push(outstream_av, this_sv);
    }

    free(in_array);
    free(out_array);
    free(dict_array);

    // Send the transformed image back to Perl in the new AV.
    RETVAL = outstream_av;
  OUTPUT:
    RETVAL