SIXEL.xs

filter: Statementstmt 88.3% 38 / 43 Branchbran 61.9% 26 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.2% 64 / 85 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Imager/File/SIXEL.pm ↑ Summary sixel_read.c »
1 /*
2 * Imager::File::SIXEL - XS glue.
3 *
4 * Copyright (C) 2026 davenonymous.
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the same terms as Perl itself.
8 */
9 #define PERL_NO_GET_CONTEXT
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 #include "EXTERN.h"
14 #include "perl.h"
15 #include "XSUB.h"
16 #include "imext.h"
17 #include "imperl.h"
18 #include "imsixel.h"
19 #ifdef __cplusplus
20 }
21 #endif
22  
23 DEFINE_IMAGER_CALLBACKS;
24  
25 /* Copies an array reference of Imager::Color objects, or undef, into a
26 * colour array freed with the current Perl scope; *size receives the
27 * number of colours. The Perl layer validates the palette, so misuse
28 * croaks.
29 */
30 static i_color *
31 63 palette_from_sv(pTHX_ SV *sv, int *size) {
32 AV *av;
33 i_color *palette;
34 SSize_t count, i;
35  
36 63 *size = 0;
37 63 ▶ if (!SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) 59 4 63
38 59 return NULL;
39 4 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) 4 0 4
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) 0 4 4
40 0 croak("palette must be an array reference");
41  
42 4 av = (AV *)SvRV(sv);
43 4 count = av_len(av) + 1;
44 4 ▶ if (count < 1 || count > 256)
Branch: 2 branches
BranchTrueFalseTotal
if (count < 1 || count > 256) 4 0 4
if (count < 1 || count > 256) 0 4 4
45 0 croak("palette must hold from 1 to 256 colors");
46  
47 4 ▶ Newx(palette, count, i_color);
Branch: 1 branch
BranchTrueFalseTotal
Newx(palette, count, i_color); 0 4 4
48 4 SAVEFREEPV(palette);
49 12 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 8 4 12
50 8 SV **entry = av_fetch(av, i, 0);
51 8 ▶ if (!entry || !SvROK(*entry) || !sv_derived_from(*entry, "Imager::Color"))
Branch: 3 branches
BranchTrueFalseTotal
if (!entry || !SvROK(*entry) || !sv_derived_from(*entry, "Imager::Color")) 8 0 8
if (!entry || !SvROK(*entry) || !sv_derived_from(*entry, "Imager::Color")) 8 0 8
if (!entry || !SvROK(*entry) || !sv_derived_from(*entry, "Imager::Color")) 0 8 8
52 0 croak("palette entry %d is not an Imager::Color", (int)i);
53 8 palette[i] = *INT2PTR(i_color *, SvIV(SvRV(*entry)));
54 }
55 4 *size = (int)count;
56 4 return palette;
57 }
58  
59 static i_img *
60 5 image_from_sv(pTHX_ SV *sv) {
61 5 ▶ if (!SvROK(sv) || !sv_derived_from(sv, "Imager::ImgRaw"))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || !sv_derived_from(sv, "Imager::ImgRaw")) 5 0 5
if (!SvROK(sv) || !sv_derived_from(sv, "Imager::ImgRaw")) 0 5 5
62 0 croak("only images can be written");
63 5 return INT2PTR(i_img *, SvIV(SvRV(sv)));
64 }
65  
66 MODULE = Imager::File::SIXEL PACKAGE = Imager::File::SIXEL
67  
68 PROTOTYPES: DISABLE
69  
70 Imager::ImgRaw
71 i_readsixel(ig, page, allow_incomplete)
72 Imager::IO ig
73 int page
74 int allow_incomplete
75  
76 void
77 i_readsixel_multi(ig, allow_incomplete)
78 Imager::IO ig
79 int allow_incomplete
80 PREINIT:
81 i_img **imgs;
82 int count;
83 int i;
84 PPCODE:
85 7 imgs = i_readsixel_multi(ig, &count, allow_incomplete);
86 7 ▶ if (imgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (imgs) { 5 2 7
87 5 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 5 0 5
EXTEND(SP, count); 0 5 5
88 16 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 11 5 16
89 11 SV *sv = sv_newmortal();
90 11 sv_setref_pv(sv, "Imager::ImgRaw", (void *)imgs[i]);
91 11 PUSHs(sv);
92 }
93 5 myfree(imgs);
94 }
95  
96 undef_int
97 i_writesixel(ig, palette, im)
98 Imager::IO ig
99 SV *palette
100 Imager::ImgRaw im
101 PREINIT:
102 i_color *colors;
103 int color_count;
104 CODE:
105 61 colors = palette_from_sv(aTHX_ palette, &color_count);
106 61 RETVAL = i_writesixel(ig, im, colors, color_count);
107 OUTPUT:
108 RETVAL
109  
110 undef_int
111 i_writesixel_multi(ig, palette, ...)
112 Imager::IO ig
113 SV *palette
114 PREINIT:
115 i_color *colors;
116 int color_count;
117 i_img **imgs;
118 int img_count;
119 int i;
120 CODE:
121 2 img_count = items - 2;
122 2 ▶ if (img_count < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (img_count < 1) 0 2 2
123 0 croak("Usage: i_writesixel_multi(ig, palette, images...)");
124 2 Newx(imgs, img_count, i_img *);
125 2 SAVEFREEPV(imgs);
126 7 ▶ for (i = 0; i < img_count; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < img_count; ++i) 5 2 7
127 5 imgs[i] = image_from_sv(aTHX_ ST(2 + i));
128 2 colors = palette_from_sv(aTHX_ palette, &color_count);
129 2 RETVAL = i_writesixel_multi(ig, imgs, img_count, colors, color_count);
130 OUTPUT:
131 RETVAL
132  
133 BOOT:
134 6 ▶ PERL_INITIALIZE_IMAGER_CALLBACKS;
Branch: 3 branches
BranchTrueFalseTotal
PERL_INITIALIZE_IMAGER_CALLBACKS; 0 6 6
PERL_INITIALIZE_IMAGER_CALLBACKS; 0 6 6
PERL_INITIALIZE_IMAGER_CALLBACKS; 0 6 6
« blib/lib/Imager/File/SIXEL.pm ↑ Summary sixel_read.c »