| 1 |
root |
1.1 |
#include "EXTERN.h" |
| 2 |
|
|
#include "perl.h" |
| 3 |
|
|
#include "XSUB.h" |
| 4 |
|
|
|
| 5 |
root |
1.18 |
#include <string.h> |
| 6 |
root |
1.17 |
#include <setjmp.h> |
| 7 |
root |
1.33 |
#include <math.h> |
| 8 |
root |
1.17 |
|
| 9 |
root |
1.31 |
#include <magic.h> |
| 10 |
|
|
|
| 11 |
root |
1.17 |
#include <jpeglib.h> |
| 12 |
root |
1.53 |
#include <jerror.h> |
| 13 |
|
|
|
| 14 |
root |
1.23 |
#include <glib.h> |
| 15 |
|
|
#include <gtk/gtk.h> |
| 16 |
root |
1.50 |
#include <gdk/gdkx.h> |
| 17 |
root |
1.2 |
#include <gdk-pixbuf/gdk-pixbuf.h> |
| 18 |
|
|
|
| 19 |
|
|
#include <gperl.h> |
| 20 |
|
|
#include <gtk2perl.h> |
| 21 |
|
|
|
| 22 |
root |
1.43 |
#include <assert.h> |
| 23 |
|
|
|
| 24 |
root |
1.71 |
#if JXL |
| 25 |
|
|
#include <jxl/decode.h> |
| 26 |
|
|
#include "jxl/thread_parallel_runner.h" |
| 27 |
|
|
#endif |
| 28 |
|
|
|
| 29 |
root |
1.50 |
#if WEBP |
| 30 |
root |
1.54 |
#include <webp/demux.h> |
| 31 |
root |
1.50 |
#include <webp/decode.h> |
| 32 |
|
|
#endif |
| 33 |
|
|
|
| 34 |
root |
1.71 |
#if AVIF |
| 35 |
|
|
#include <avif/avif.h> |
| 36 |
root |
1.58 |
#endif |
| 37 |
|
|
|
| 38 |
root |
1.46 |
#include "perlmulticore.h" |
| 39 |
|
|
|
| 40 |
root |
1.33 |
#define IW 80 /* MUST match Schnauzer.pm! */ |
| 41 |
|
|
#define IH 60 /* MUST match Schnauzer.pm! */ |
| 42 |
root |
1.5 |
|
| 43 |
root |
1.7 |
#define LINELENGTH 240 |
| 44 |
|
|
|
| 45 |
root |
1.6 |
#define ELLIPSIS "\xe2\x80\xa6" |
| 46 |
|
|
|
| 47 |
root |
1.42 |
typedef char *octet_string; |
| 48 |
|
|
|
| 49 |
root |
1.51 |
static magic_t magic_cookie[2]; /* !mime, mime */ |
| 50 |
|
|
|
| 51 |
root |
1.17 |
struct jpg_err_mgr |
| 52 |
|
|
{ |
| 53 |
|
|
struct jpeg_error_mgr err; |
| 54 |
|
|
jmp_buf setjmp_buffer; |
| 55 |
|
|
}; |
| 56 |
|
|
|
| 57 |
|
|
static void |
| 58 |
|
|
cv_error_exit (j_common_ptr cinfo) |
| 59 |
|
|
{ |
| 60 |
root |
1.48 |
cinfo->err->output_message (cinfo); |
| 61 |
root |
1.17 |
longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
static void |
| 65 |
|
|
cv_error_output (j_common_ptr cinfo) |
| 66 |
|
|
{ |
| 67 |
root |
1.48 |
char msg[JMSG_LENGTH_MAX]; |
| 68 |
|
|
|
| 69 |
|
|
cinfo->err->format_message (cinfo, msg); |
| 70 |
|
|
|
| 71 |
|
|
fprintf (stderr, "JPEG decoding error: %s\n", msg); |
| 72 |
root |
1.17 |
return; |
| 73 |
|
|
} |
| 74 |
|
|
|
| 75 |
|
|
static void |
| 76 |
|
|
rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
| 77 |
|
|
unsigned int *h, unsigned int *s, unsigned int *v) |
| 78 |
|
|
{ |
| 79 |
|
|
unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b; |
| 80 |
|
|
unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b; |
| 81 |
|
|
unsigned int delta = mx - mn; |
| 82 |
|
|
|
| 83 |
|
|
*v = mx; |
| 84 |
|
|
|
| 85 |
|
|
*s = mx ? delta * 255 / mx : 0; |
| 86 |
|
|
|
| 87 |
|
|
if (delta == 0) |
| 88 |
|
|
*h = 0; |
| 89 |
|
|
else |
| 90 |
|
|
{ |
| 91 |
|
|
if (r == mx) |
| 92 |
|
|
*h = ((int)g - (int)b) * 255 / (int)(delta * 3); |
| 93 |
|
|
else if (g == mx) |
| 94 |
|
|
*h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52; |
| 95 |
|
|
else if (b == mx) |
| 96 |
|
|
*h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103; |
| 97 |
|
|
|
| 98 |
|
|
*h &= 255; |
| 99 |
|
|
} |
| 100 |
|
|
} |
| 101 |
|
|
|
| 102 |
root |
1.36 |
struct feature { |
| 103 |
|
|
float v1, v2, v3; // mean, square, cube |
| 104 |
|
|
int n; |
| 105 |
|
|
}; |
| 106 |
|
|
|
| 107 |
|
|
static void |
| 108 |
|
|
feature_init (struct feature *f) |
| 109 |
|
|
{ |
| 110 |
|
|
f->v1 = 0.; |
| 111 |
|
|
f->v2 = 0.; |
| 112 |
|
|
f->v3 = 0.; |
| 113 |
|
|
f->n = 0; |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
// didn't find an algorithm to neatly do mean, variance and skew in one pass. |
| 117 |
|
|
// elmex ist schuld. |
| 118 |
|
|
static void |
| 119 |
|
|
feature_update_pass_1 (struct feature *f, unsigned int v) |
| 120 |
|
|
{ |
| 121 |
|
|
f->v1 += v; |
| 122 |
|
|
f->n += 1; |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
static void |
| 126 |
|
|
feature_finish_pass_1 (struct feature *f) |
| 127 |
|
|
{ |
| 128 |
|
|
if (f->n < 1) |
| 129 |
|
|
return; |
| 130 |
|
|
|
| 131 |
|
|
f->v1 /= f->n; |
| 132 |
|
|
} |
| 133 |
|
|
|
| 134 |
|
|
static void |
| 135 |
|
|
feature_update_pass_2 (struct feature *f, unsigned int v) |
| 136 |
|
|
{ |
| 137 |
|
|
float d = v - f->v1; |
| 138 |
|
|
|
| 139 |
|
|
f->v2 += d * d; |
| 140 |
|
|
f->v3 += d * d * d; |
| 141 |
|
|
} |
| 142 |
|
|
|
| 143 |
|
|
static void |
| 144 |
|
|
feature_finish_pass_2 (struct feature *f) |
| 145 |
|
|
{ |
| 146 |
|
|
if (f->n < 1) |
| 147 |
|
|
return; |
| 148 |
|
|
|
| 149 |
|
|
f->v2 /= f->n; |
| 150 |
|
|
f->v3 /= f->n; |
| 151 |
|
|
|
| 152 |
|
|
f->v1 /= 255.; |
| 153 |
root |
1.37 |
f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2); |
| 154 |
|
|
f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.); |
| 155 |
root |
1.36 |
} |
| 156 |
|
|
|
| 157 |
root |
1.4 |
static guint32 a85_val; |
| 158 |
|
|
static guint a85_cnt; |
| 159 |
root |
1.7 |
static guchar a85_buf[LINELENGTH], *a85_ptr; |
| 160 |
root |
1.4 |
|
| 161 |
|
|
static void |
| 162 |
|
|
a85_init (void) |
| 163 |
|
|
{ |
| 164 |
|
|
a85_cnt = 4; |
| 165 |
|
|
a85_ptr = a85_buf; |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
|
|
static void |
| 169 |
|
|
a85_push (PerlIO *fp, guchar c) |
| 170 |
|
|
{ |
| 171 |
|
|
a85_val = a85_val << 8 | c; |
| 172 |
|
|
|
| 173 |
|
|
if (!--a85_cnt) |
| 174 |
|
|
{ |
| 175 |
|
|
a85_cnt = 4; |
| 176 |
|
|
if (a85_val) |
| 177 |
|
|
{ |
| 178 |
root |
1.49 |
a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
| 179 |
|
|
a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
| 180 |
|
|
a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
| 181 |
root |
1.4 |
a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
| 182 |
|
|
a85_ptr[0] = (a85_val ) + 33; |
| 183 |
|
|
|
| 184 |
|
|
a85_ptr += 5; |
| 185 |
|
|
} |
| 186 |
|
|
else |
| 187 |
|
|
*a85_ptr++ = 'z'; |
| 188 |
|
|
|
| 189 |
|
|
if (a85_ptr >= a85_buf + sizeof (a85_buf) - 7) |
| 190 |
|
|
{ |
| 191 |
|
|
*a85_ptr++ = '\n'; |
| 192 |
|
|
PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
| 193 |
|
|
a85_ptr = a85_buf; |
| 194 |
|
|
} |
| 195 |
|
|
} |
| 196 |
|
|
|
| 197 |
|
|
} |
| 198 |
|
|
|
| 199 |
root |
1.8 |
static void |
| 200 |
root |
1.4 |
a85_finish (PerlIO *fp) |
| 201 |
|
|
{ |
| 202 |
|
|
while (a85_cnt != 4) |
| 203 |
|
|
a85_push (fp, 0); |
| 204 |
|
|
|
| 205 |
|
|
*a85_ptr++ = '~'; // probably buggy end-marker |
| 206 |
|
|
*a85_ptr++ = '>'; // probably buggy end-marker |
| 207 |
|
|
*a85_ptr++ = '\n'; |
| 208 |
|
|
|
| 209 |
|
|
PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
| 210 |
|
|
} |
| 211 |
|
|
|
| 212 |
root |
1.6 |
///////////////////////////////////////////////////////////////////////////// |
| 213 |
root |
1.56 |
// memory source for libjpeg |
| 214 |
root |
1.6 |
|
| 215 |
root |
1.53 |
static void cv_ms_init (j_decompress_ptr cinfo) |
| 216 |
|
|
{ |
| 217 |
|
|
} |
| 218 |
|
|
|
| 219 |
|
|
static void cv_ms_term (j_decompress_ptr cinfo) |
| 220 |
|
|
{ |
| 221 |
|
|
} |
| 222 |
|
|
|
| 223 |
|
|
static boolean cv_ms_fill (j_decompress_ptr cinfo) |
| 224 |
|
|
{ |
| 225 |
root |
1.57 |
// unexpected EOF, warn and generate fake EOI marker |
| 226 |
|
|
|
| 227 |
|
|
WARNMS (cinfo, JWRN_JPEG_EOF); |
| 228 |
|
|
|
| 229 |
|
|
struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src; |
| 230 |
|
|
|
| 231 |
|
|
static const JOCTET eoi[] = { 0xFF, JPEG_EOI }; |
| 232 |
|
|
|
| 233 |
|
|
src->next_input_byte = eoi; |
| 234 |
|
|
src->bytes_in_buffer = sizeof (eoi); |
| 235 |
root |
1.53 |
|
| 236 |
|
|
return TRUE; |
| 237 |
|
|
} |
| 238 |
|
|
|
| 239 |
|
|
static void cv_ms_skip (j_decompress_ptr cinfo, long num_bytes) |
| 240 |
|
|
{ |
| 241 |
root |
1.57 |
struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src; |
| 242 |
|
|
|
| 243 |
|
|
src->next_input_byte += num_bytes; |
| 244 |
|
|
src->bytes_in_buffer -= num_bytes; |
| 245 |
root |
1.53 |
} |
| 246 |
|
|
|
| 247 |
|
|
static void cv_jpeg_mem_src (j_decompress_ptr cinfo, void *buf, size_t buflen) |
| 248 |
|
|
{ |
| 249 |
|
|
struct jpeg_source_mgr *src; |
| 250 |
|
|
|
| 251 |
|
|
if (!cinfo->src) |
| 252 |
|
|
cinfo->src = (struct jpeg_source_mgr *) |
| 253 |
|
|
(*cinfo->mem->alloc_small) ( |
| 254 |
|
|
(j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr) |
| 255 |
|
|
); |
| 256 |
|
|
|
| 257 |
|
|
src = (struct jpeg_source_mgr *)cinfo->src; |
| 258 |
|
|
src->init_source = cv_ms_init; |
| 259 |
|
|
src->fill_input_buffer = cv_ms_fill; |
| 260 |
|
|
src->skip_input_data = cv_ms_skip; |
| 261 |
|
|
src->resync_to_restart = jpeg_resync_to_restart; |
| 262 |
|
|
src->term_source = cv_ms_term; |
| 263 |
|
|
src->next_input_byte = (JOCTET *)buf; |
| 264 |
|
|
src->bytes_in_buffer = buflen; |
| 265 |
|
|
} |
| 266 |
|
|
|
| 267 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 268 |
|
|
|
| 269 |
root |
1.58 |
/* great, the jpeg-xl reference implementaton requires us to parse bmff files */ |
| 270 |
|
|
|
| 271 |
|
|
struct bmff_box |
| 272 |
|
|
{ |
| 273 |
|
|
char type[4]; |
| 274 |
|
|
const uint8_t *ptr; |
| 275 |
|
|
size_t size; |
| 276 |
|
|
}; |
| 277 |
|
|
|
| 278 |
|
|
static int |
| 279 |
|
|
bmff_parse_box (struct bmff_box *box, const uint8_t **next_in, size_t *avail_in) |
| 280 |
|
|
{ |
| 281 |
|
|
if (*avail_in < 8) |
| 282 |
|
|
return 0; |
| 283 |
|
|
|
| 284 |
|
|
box->size = ((*next_in)[0] << 24) |
| 285 |
|
|
| ((*next_in)[1] << 16) |
| 286 |
|
|
| ((*next_in)[2] << 8) |
| 287 |
|
|
| ((*next_in)[3] ); |
| 288 |
|
|
|
| 289 |
|
|
if (box->size < 8) |
| 290 |
|
|
return 0; |
| 291 |
|
|
|
| 292 |
|
|
if (*avail_in < box->size) |
| 293 |
|
|
return 0; |
| 294 |
|
|
|
| 295 |
|
|
memcpy (box->type, *next_in + 4, 4); |
| 296 |
|
|
box->ptr = *next_in + 8; |
| 297 |
|
|
|
| 298 |
|
|
*next_in += box->size; |
| 299 |
|
|
*avail_in -= box->size; |
| 300 |
|
|
|
| 301 |
|
|
box->size -= 8; |
| 302 |
|
|
|
| 303 |
|
|
return 1; |
| 304 |
|
|
} |
| 305 |
|
|
|
| 306 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 307 |
root |
1.71 |
// libavif api is just annoying on every minor detail |
| 308 |
|
|
|
| 309 |
|
|
static int |
| 310 |
|
|
is_avif (U8 *data, STRLEN data_len) |
| 311 |
|
|
{ |
| 312 |
|
|
#if AVIF |
| 313 |
|
|
avifROData buf; |
| 314 |
|
|
buf.data = data; |
| 315 |
|
|
buf.size = data_len; |
| 316 |
|
|
return avifPeekCompatibleFileType (&buf); |
| 317 |
|
|
#else |
| 318 |
|
|
return data_len >= 12 |
| 319 |
|
|
&& data[ 4] == 'f' |
| 320 |
|
|
&& data[ 5] == 't' |
| 321 |
|
|
&& data[ 6] == 'y' |
| 322 |
|
|
&& data[ 7] == 'p' |
| 323 |
|
|
&& data[ 8] == 'a' |
| 324 |
|
|
&& data[ 9] == 'v' |
| 325 |
|
|
&& data[10] == 'i' |
| 326 |
|
|
&& data[11] == 'f' |
| 327 |
|
|
; |
| 328 |
|
|
#endif |
| 329 |
|
|
} |
| 330 |
|
|
|
| 331 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 332 |
root |
1.58 |
|
| 333 |
root |
1.9 |
MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
| 334 |
root |
1.1 |
|
| 335 |
|
|
PROTOTYPES: ENABLE |
| 336 |
root |
1.2 |
|
| 337 |
root |
1.66 |
void |
| 338 |
|
|
_exit (int code) |
| 339 |
|
|
|
| 340 |
root |
1.56 |
# calculate the common prefix length of two strings |
| 341 |
root |
1.29 |
# missing function in perl. really :) |
| 342 |
|
|
int |
| 343 |
|
|
common_prefix_length (a, b) |
| 344 |
|
|
unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg); |
| 345 |
|
|
unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg); |
| 346 |
|
|
CODE: |
| 347 |
|
|
RETVAL = 0; |
| 348 |
|
|
|
| 349 |
|
|
while (*a == *b && *a) |
| 350 |
|
|
{ |
| 351 |
|
|
RETVAL += (*a & 0xc0) != 0x80; |
| 352 |
|
|
a++, b++; |
| 353 |
|
|
} |
| 354 |
|
|
|
| 355 |
|
|
OUTPUT: |
| 356 |
|
|
RETVAL |
| 357 |
|
|
|
| 358 |
root |
1.70 |
int |
| 359 |
|
|
common_prefix_length_byte (a, b) |
| 360 |
|
|
unsigned char *a = (unsigned char *)SvPVbyte_nolen ($arg); |
| 361 |
|
|
unsigned char *b = (unsigned char *)SvPVbyte_nolen ($arg); |
| 362 |
|
|
CODE: |
| 363 |
|
|
RETVAL = 0; |
| 364 |
|
|
while (*a == *b && *a) |
| 365 |
|
|
a++, b++, RETVAL++; |
| 366 |
|
|
OUTPUT: |
| 367 |
|
|
RETVAL |
| 368 |
|
|
|
| 369 |
root |
1.31 |
const char * |
| 370 |
root |
1.51 |
magic (SV *path_or_data) |
| 371 |
|
|
ALIAS: |
| 372 |
|
|
magic = 0 |
| 373 |
|
|
magic_mime = 1 |
| 374 |
|
|
magic_buffer = 2 |
| 375 |
|
|
magic_buffer_mime = 3 |
| 376 |
root |
1.31 |
CODE: |
| 377 |
|
|
{ |
| 378 |
root |
1.51 |
STRLEN len; |
| 379 |
|
|
char *data = SvPVbyte (path_or_data, len); |
| 380 |
root |
1.31 |
|
| 381 |
root |
1.51 |
if (!magic_cookie[0]) |
| 382 |
|
|
{ |
| 383 |
|
|
magic_cookie[0] = magic_open (MAGIC_SYMLINK); |
| 384 |
root |
1.52 |
magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE); |
| 385 |
|
|
magic_load (magic_cookie[0], 0); |
| 386 |
|
|
magic_load (magic_cookie[1], 0); |
| 387 |
root |
1.32 |
} |
| 388 |
|
|
|
| 389 |
root |
1.52 |
perlinterp_release (); |
| 390 |
|
|
|
| 391 |
root |
1.51 |
RETVAL = ix & 2 |
| 392 |
root |
1.52 |
? magic_buffer (magic_cookie[ix & 1], data, len) |
| 393 |
|
|
: magic_file (magic_cookie[ix & 1], data); |
| 394 |
root |
1.32 |
|
| 395 |
root |
1.46 |
perlinterp_acquire (); |
| 396 |
root |
1.31 |
} |
| 397 |
|
|
OUTPUT: |
| 398 |
|
|
RETVAL |
| 399 |
|
|
|
| 400 |
root |
1.40 |
# missing/broken in Gtk2 perl module |
| 401 |
|
|
|
| 402 |
|
|
void |
| 403 |
|
|
gdk_window_clear_hints (GdkWindow *window) |
| 404 |
|
|
CODE: |
| 405 |
|
|
gdk_window_set_geometry_hints (window, 0, 0); |
| 406 |
root |
1.22 |
|
| 407 |
|
|
gboolean |
| 408 |
|
|
gdk_net_wm_supports (GdkAtom property) |
| 409 |
|
|
CODE: |
| 410 |
|
|
#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) |
| 411 |
|
|
RETVAL = gdk_net_wm_supports (property); |
| 412 |
|
|
#else |
| 413 |
|
|
RETVAL = 0; |
| 414 |
|
|
#endif |
| 415 |
|
|
OUTPUT: |
| 416 |
|
|
RETVAL |
| 417 |
|
|
|
| 418 |
root |
1.3 |
GdkPixbuf_noinc * |
| 419 |
root |
1.26 |
dealpha_expose (GdkPixbuf *pb) |
| 420 |
|
|
CODE: |
| 421 |
root |
1.46 |
perlinterp_release (); |
| 422 |
root |
1.26 |
{ |
| 423 |
|
|
int w = gdk_pixbuf_get_width (pb); |
| 424 |
|
|
int h = gdk_pixbuf_get_height (pb); |
| 425 |
|
|
int bpp = gdk_pixbuf_get_n_channels (pb); |
| 426 |
|
|
int x, y, i; |
| 427 |
|
|
guchar *src = gdk_pixbuf_get_pixels (pb), *dst; |
| 428 |
|
|
int sstr = gdk_pixbuf_get_rowstride (pb), dstr; |
| 429 |
|
|
|
| 430 |
|
|
RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); |
| 431 |
|
|
|
| 432 |
|
|
dst = gdk_pixbuf_get_pixels (RETVAL); |
| 433 |
|
|
dstr = gdk_pixbuf_get_rowstride (RETVAL); |
| 434 |
|
|
|
| 435 |
|
|
for (x = 0; x < w; x++) |
| 436 |
|
|
for (y = 0; y < h; y++) |
| 437 |
|
|
for (i = 0; i < 3; i++) |
| 438 |
|
|
dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; |
| 439 |
|
|
} |
| 440 |
root |
1.46 |
perlinterp_acquire (); |
| 441 |
root |
1.26 |
OUTPUT: |
| 442 |
|
|
RETVAL |
| 443 |
|
|
|
| 444 |
|
|
GdkPixbuf_noinc * |
| 445 |
root |
1.28 |
rotate (GdkPixbuf *pb, int angle) |
| 446 |
root |
1.2 |
CODE: |
| 447 |
root |
1.46 |
perlinterp_release (); |
| 448 |
root |
1.44 |
if (angle < 0) |
| 449 |
|
|
angle += 360; |
| 450 |
root |
1.28 |
RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE |
| 451 |
|
|
: angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
| 452 |
|
|
: angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
| 453 |
|
|
: angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
| 454 |
|
|
: angle); |
| 455 |
root |
1.46 |
perlinterp_acquire (); |
| 456 |
root |
1.2 |
OUTPUT: |
| 457 |
|
|
RETVAL |
| 458 |
|
|
|
| 459 |
root |
1.53 |
const char * |
| 460 |
|
|
filetype (SV *image_data) |
| 461 |
|
|
CODE: |
| 462 |
|
|
{ |
| 463 |
|
|
STRLEN data_len; |
| 464 |
|
|
U8 *data = SvPVbyte (image_data, data_len); |
| 465 |
root |
1.58 |
static const unsigned char jxl_header[] = { |
| 466 |
root |
1.63 |
0, 0, 0, 0x0c, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0xa, 0x87, 0x0a |
| 467 |
root |
1.58 |
}; |
| 468 |
root |
1.53 |
|
| 469 |
|
|
if (data_len >= 20 |
| 470 |
|
|
&& data[0] == 0xff |
| 471 |
|
|
&& data[1] == 0xd8 |
| 472 |
|
|
&& data[2] == 0xff) |
| 473 |
root |
1.58 |
RETVAL = "image/jpeg"; |
| 474 |
root |
1.53 |
else if (data_len >= 12 |
| 475 |
|
|
&& data[ 0] == (U8)'R' |
| 476 |
|
|
&& data[ 1] == (U8)'I' |
| 477 |
|
|
&& data[ 2] == (U8)'F' |
| 478 |
|
|
&& data[ 3] == (U8)'F' |
| 479 |
|
|
&& data[ 8] == (U8)'W' |
| 480 |
|
|
&& data[ 9] == (U8)'E' |
| 481 |
|
|
&& data[10] == (U8)'B' |
| 482 |
|
|
&& data[11] == (U8)'P') |
| 483 |
root |
1.58 |
RETVAL = "image/webp"; |
| 484 |
root |
1.53 |
else if (data_len >= 16 |
| 485 |
|
|
&& data[ 0] == 0x89 |
| 486 |
|
|
&& data[ 1] == (U8)'P' |
| 487 |
|
|
&& data[ 2] == (U8)'N' |
| 488 |
|
|
&& data[ 3] == (U8)'G' |
| 489 |
|
|
&& data[ 4] == 0x0d |
| 490 |
|
|
&& data[ 5] == 0x0a |
| 491 |
|
|
&& data[ 6] == 0x1a |
| 492 |
|
|
&& data[ 7] == 0x0a) |
| 493 |
root |
1.58 |
RETVAL = "image/png"; |
| 494 |
root |
1.71 |
else if (is_avif (data, data_len)) |
| 495 |
|
|
RETVAL = "image/avif"; |
| 496 |
root |
1.58 |
else if (data_len >= sizeof (jxl_header) && memcmp (data, jxl_header, sizeof (jxl_header)) == 0) |
| 497 |
root |
1.63 |
RETVAL = "image/jxl"; // todo: might want to use JxlSignatureCheck |
| 498 |
|
|
else if (data_len >= 2 |
| 499 |
|
|
&& data[0] == 0xff |
| 500 |
|
|
&& data[1] == 0x0a) |
| 501 |
root |
1.58 |
RETVAL = "image/jxl"; |
| 502 |
root |
1.61 |
else if (data_len >= 13 |
| 503 |
|
|
&& data[0] == 'G' |
| 504 |
|
|
&& data[1] == 'I' |
| 505 |
|
|
&& data[2] == 'F' |
| 506 |
|
|
&& data[3] == '8' |
| 507 |
|
|
//&& (data[4] == '7' || data[4] == '9') |
| 508 |
|
|
&& data[5] == 'a') |
| 509 |
|
|
{ |
| 510 |
|
|
RETVAL = "image/gif"; |
| 511 |
|
|
|
| 512 |
|
|
// now see if its animated - we require the netscape application header for this |
| 513 |
|
|
int ofs = 13; |
| 514 |
|
|
|
| 515 |
|
|
if (data[10] & 0x80) |
| 516 |
|
|
ofs += (1 << ((data[10] & 7) + 1)) * 3; |
| 517 |
|
|
|
| 518 |
|
|
if (data_len >= ofs + 2 + 1 + 11) |
| 519 |
|
|
{ |
| 520 |
root |
1.62 |
|
| 521 |
|
|
// skip a graphic control extension block. we assume |
| 522 |
|
|
// there is at most one such block - while the NAB |
| 523 |
|
|
// has to come firstz, some files do not obey this |
| 524 |
root |
1.61 |
if (data[ofs] == 0x21 && data[ofs + 1] == 0xf9) |
| 525 |
|
|
ofs += 3 + data[ofs + 2] + 1; |
| 526 |
|
|
|
| 527 |
|
|
if (data_len >= ofs + 2 + 1 + 11) |
| 528 |
|
|
if (!memcmp (data + ofs, "\x21\xff\x0bNETSCAPE2.0", sizeof ("\x21\xff\x0bNETSCAPE2.0") - 1)) |
| 529 |
|
|
RETVAL = "video/gif"; |
| 530 |
|
|
} |
| 531 |
|
|
} |
| 532 |
root |
1.67 |
else if (data_len >= 0x8000 + 6 |
| 533 |
|
|
&& data[0x8000+1] == (U8)'B' |
| 534 |
|
|
&& data[0x8000+2] == (U8)'E' |
| 535 |
|
|
&& data[0x8000+3] == (U8)'A' |
| 536 |
|
|
&& data[0x8000+4] == (U8)'0' |
| 537 |
|
|
&& data[0x8000+5] == (U8)'1') |
| 538 |
|
|
RETVAL = "video/iso-bluray"; |
| 539 |
root |
1.68 |
else if (data_len >= 0x8000 + 6 |
| 540 |
|
|
&& data[0x8000+1] == (U8)'C' |
| 541 |
|
|
&& data[0x8000+2] == (U8)'D' |
| 542 |
|
|
&& data[0x8000+3] == (U8)'0' |
| 543 |
|
|
&& data[0x8000+4] == (U8)'0' |
| 544 |
|
|
&& data[0x8000+5] == (U8)'1') |
| 545 |
|
|
RETVAL = "video/iso9660"; |
| 546 |
root |
1.61 |
|
| 547 |
root |
1.53 |
else |
| 548 |
root |
1.58 |
XSRETURN_UNDEF; |
| 549 |
root |
1.53 |
} |
| 550 |
|
|
OUTPUT: |
| 551 |
|
|
RETVAL |
| 552 |
|
|
|
| 553 |
root |
1.17 |
GdkPixbuf_noinc * |
| 554 |
root |
1.71 |
decode_jpeg (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
| 555 |
root |
1.50 |
CODE: |
| 556 |
|
|
{ |
| 557 |
root |
1.71 |
struct jpeg_decompress_struct cinfo; |
| 558 |
|
|
struct jpg_err_mgr jerr; |
| 559 |
|
|
int rs; |
| 560 |
|
|
volatile GdkPixbuf *pb = 0; |
| 561 |
|
|
STRLEN data_len; |
| 562 |
|
|
guchar *data = SvPVbyte (image_data, data_len); |
| 563 |
root |
1.51 |
|
| 564 |
root |
1.71 |
RETVAL = 0; |
| 565 |
root |
1.50 |
|
| 566 |
|
|
perlinterp_release (); |
| 567 |
|
|
|
| 568 |
root |
1.71 |
cinfo.err = jpeg_std_error (&jerr.err); |
| 569 |
|
|
|
| 570 |
|
|
jerr.err.error_exit = cv_error_exit; |
| 571 |
|
|
jerr.err.output_message = cv_error_output; |
| 572 |
|
|
|
| 573 |
|
|
if ((rs = setjmp (jerr.setjmp_buffer))) |
| 574 |
|
|
{ |
| 575 |
|
|
jpeg_destroy_decompress (&cinfo); |
| 576 |
|
|
|
| 577 |
|
|
if (pb) |
| 578 |
|
|
g_object_unref ((gpointer)pb); |
| 579 |
root |
1.50 |
|
| 580 |
root |
1.71 |
perlinterp_acquire (); |
| 581 |
|
|
XSRETURN_UNDEF; |
| 582 |
|
|
} |
| 583 |
root |
1.54 |
|
| 584 |
root |
1.71 |
if (!data_len) |
| 585 |
|
|
longjmp (jerr.setjmp_buffer, 4); |
| 586 |
root |
1.54 |
|
| 587 |
root |
1.71 |
jpeg_create_decompress (&cinfo); |
| 588 |
|
|
cv_jpeg_mem_src (&cinfo, data, data_len); |
| 589 |
root |
1.50 |
|
| 590 |
root |
1.71 |
jpeg_read_header (&cinfo, TRUE); |
| 591 |
root |
1.50 |
|
| 592 |
root |
1.71 |
cinfo.dct_method = JDCT_DEFAULT; |
| 593 |
|
|
cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ |
| 594 |
|
|
cinfo.do_block_smoothing = FALSE; |
| 595 |
|
|
cinfo.out_color_space = JCS_RGB; |
| 596 |
|
|
cinfo.quantize_colors = FALSE; |
| 597 |
root |
1.51 |
|
| 598 |
root |
1.71 |
cinfo.scale_num = 1; |
| 599 |
|
|
cinfo.scale_denom = 1; |
| 600 |
root |
1.51 |
|
| 601 |
root |
1.71 |
jpeg_calc_output_dimensions (&cinfo); |
| 602 |
root |
1.51 |
|
| 603 |
root |
1.71 |
if (thumbnail) |
| 604 |
|
|
{ |
| 605 |
|
|
cinfo.dct_method = JDCT_FASTEST; |
| 606 |
|
|
cinfo.do_fancy_upsampling = FALSE; |
| 607 |
root |
1.50 |
|
| 608 |
root |
1.71 |
while (cinfo.scale_denom < 8 |
| 609 |
|
|
&& cinfo.output_width >= iw*4 |
| 610 |
|
|
&& cinfo.output_height >= ih*4) |
| 611 |
|
|
{ |
| 612 |
|
|
cinfo.scale_denom <<= 1; |
| 613 |
|
|
jpeg_calc_output_dimensions (&cinfo); |
| 614 |
|
|
} |
| 615 |
root |
1.50 |
} |
| 616 |
root |
1.71 |
|
| 617 |
|
|
if (cinfo.output_components != 3) |
| 618 |
|
|
longjmp (jerr.setjmp_buffer, 3); |
| 619 |
|
|
|
| 620 |
|
|
if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK) |
| 621 |
root |
1.50 |
{ |
| 622 |
root |
1.71 |
cinfo.out_color_space = JCS_CMYK; |
| 623 |
|
|
cinfo.output_components = 4; |
| 624 |
root |
1.50 |
} |
| 625 |
|
|
|
| 626 |
root |
1.71 |
pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height); |
| 627 |
|
|
if (!RETVAL) |
| 628 |
|
|
longjmp (jerr.setjmp_buffer, 2); |
| 629 |
root |
1.50 |
|
| 630 |
root |
1.71 |
data = gdk_pixbuf_get_pixels (RETVAL); |
| 631 |
|
|
rs = gdk_pixbuf_get_rowstride (RETVAL); |
| 632 |
root |
1.50 |
|
| 633 |
root |
1.71 |
jpeg_start_decompress (&cinfo); |
| 634 |
root |
1.50 |
|
| 635 |
root |
1.71 |
while (cinfo.output_scanline < cinfo.output_height) |
| 636 |
root |
1.50 |
{ |
| 637 |
root |
1.71 |
int remaining = cinfo.output_height - cinfo.output_scanline; |
| 638 |
|
|
JSAMPROW rp[4]; |
| 639 |
|
|
|
| 640 |
|
|
rp [0] = data + cinfo.output_scanline * rs; |
| 641 |
|
|
rp [1] = (guchar *)rp [0] + rs; |
| 642 |
|
|
rp [2] = (guchar *)rp [1] + rs; |
| 643 |
|
|
rp [3] = (guchar *)rp [2] + rs; |
| 644 |
|
|
|
| 645 |
|
|
jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
| 646 |
root |
1.50 |
} |
| 647 |
|
|
|
| 648 |
root |
1.71 |
if (cinfo.out_color_space == JCS_CMYK) |
| 649 |
|
|
{ |
| 650 |
|
|
guchar *end = data + cinfo.output_height * rs; |
| 651 |
|
|
|
| 652 |
|
|
while (data < end) |
| 653 |
|
|
{ |
| 654 |
|
|
U32 c = data [0]; |
| 655 |
|
|
U32 m = data [1]; |
| 656 |
|
|
U32 y = data [2]; |
| 657 |
|
|
U32 k = data [3]; |
| 658 |
|
|
|
| 659 |
|
|
if (0) |
| 660 |
|
|
if (cinfo.Adobe_transform == 2) |
| 661 |
|
|
{ |
| 662 |
|
|
c ^= 0xff; |
| 663 |
|
|
m ^= 0xff; |
| 664 |
|
|
y ^= 0xff; |
| 665 |
|
|
k ^= 0xff; |
| 666 |
|
|
} |
| 667 |
|
|
|
| 668 |
|
|
data [0] = (c * k + 0x80) / 0xff; |
| 669 |
|
|
data [1] = (m * k + 0x80) / 0xff; |
| 670 |
|
|
data [2] = (y * k + 0x80) / 0xff; |
| 671 |
|
|
data [3] = 0xff; |
| 672 |
|
|
|
| 673 |
|
|
data += 4; |
| 674 |
|
|
} |
| 675 |
|
|
} |
| 676 |
root |
1.54 |
|
| 677 |
root |
1.71 |
jpeg_finish_decompress (&cinfo); |
| 678 |
|
|
jpeg_destroy_decompress (&cinfo); |
| 679 |
root |
1.50 |
perlinterp_acquire (); |
| 680 |
|
|
} |
| 681 |
|
|
OUTPUT: |
| 682 |
|
|
RETVAL |
| 683 |
|
|
|
| 684 |
|
|
GdkPixbuf_noinc * |
| 685 |
root |
1.58 |
decode_jxl (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
| 686 |
|
|
CODE: |
| 687 |
|
|
{ |
| 688 |
|
|
#if JXL |
| 689 |
root |
1.63 |
JxlDecoder *dec; |
| 690 |
root |
1.58 |
JxlBasicInfo info; |
| 691 |
|
|
const uint8_t *next_in = (uint8_t *)SvPVbyte_nolen (image_data); |
| 692 |
|
|
size_t avail_in = SvCUR (image_data); |
| 693 |
|
|
const char *error = 0; |
| 694 |
root |
1.63 |
JxlDecoderStatus status; |
| 695 |
root |
1.65 |
static void *runner_cache; |
| 696 |
root |
1.58 |
void *runner = 0; |
| 697 |
|
|
|
| 698 |
root |
1.63 |
RETVAL = 0; |
| 699 |
|
|
|
| 700 |
root |
1.65 |
if (runner_cache) |
| 701 |
|
|
runner = runner_cache; |
| 702 |
|
|
else |
| 703 |
|
|
runner = JxlThreadParallelRunnerCreate (0, JxlThreadParallelRunnerDefaultNumWorkerThreads ()); |
| 704 |
|
|
|
| 705 |
|
|
runner_cache = 0; |
| 706 |
|
|
|
| 707 |
root |
1.58 |
perlinterp_release (); |
| 708 |
|
|
|
| 709 |
root |
1.63 |
dec = JxlDecoderCreate (0); |
| 710 |
root |
1.58 |
|
| 711 |
|
|
error = "JxlDecoderCreate failed"; |
| 712 |
|
|
if (!dec) |
| 713 |
|
|
goto done; |
| 714 |
|
|
|
| 715 |
root |
1.63 |
status = JxlDecoderSetParallelRunner (dec, JxlThreadParallelRunner, runner); |
| 716 |
root |
1.58 |
error = "JxlDecoderSetParallelRunner failed"; |
| 717 |
root |
1.63 |
if (status != JXL_DEC_SUCCESS) |
| 718 |
root |
1.58 |
goto done; |
| 719 |
|
|
|
| 720 |
|
|
error = "JxlDecoderSubscribeEvents failed"; |
| 721 |
root |
1.63 |
status = JxlDecoderSubscribeEvents (dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE); |
| 722 |
|
|
if (status != JXL_DEC_SUCCESS) |
| 723 |
root |
1.59 |
goto done; |
| 724 |
|
|
|
| 725 |
root |
1.63 |
status = JxlDecoderSetInput (dec, next_in, avail_in); |
| 726 |
root |
1.59 |
error = "JxlDecoderSetInput failed"; |
| 727 |
root |
1.63 |
if (status != JXL_DEC_SUCCESS) |
| 728 |
root |
1.58 |
goto done; |
| 729 |
|
|
|
| 730 |
|
|
for (;;) |
| 731 |
|
|
{ |
| 732 |
root |
1.63 |
status = JxlDecoderProcessInput (dec); |
| 733 |
root |
1.58 |
|
| 734 |
|
|
switch (status) |
| 735 |
|
|
{ |
| 736 |
root |
1.63 |
case JXL_DEC_FULL_IMAGE: |
| 737 |
|
|
error = 0; |
| 738 |
|
|
goto done; |
| 739 |
|
|
|
| 740 |
root |
1.58 |
case JXL_DEC_ERROR: |
| 741 |
|
|
error = "JxlDecoderProcessInput failed"; |
| 742 |
|
|
goto done; |
| 743 |
|
|
|
| 744 |
|
|
case JXL_DEC_NEED_MORE_INPUT: |
| 745 |
|
|
error = "incomplete file"; |
| 746 |
|
|
goto done; |
| 747 |
|
|
|
| 748 |
|
|
case JXL_DEC_SUCCESS: |
| 749 |
root |
1.63 |
error = "incomplete decode"; |
| 750 |
root |
1.58 |
goto done; |
| 751 |
|
|
|
| 752 |
root |
1.63 |
case JXL_DEC_BASIC_INFO: |
| 753 |
root |
1.58 |
{ |
| 754 |
root |
1.63 |
status = JxlDecoderGetBasicInfo (dec, &info); |
| 755 |
root |
1.58 |
error = "JxlDecoderGetBasicInfo failed"; |
| 756 |
root |
1.63 |
if (status != JXL_DEC_SUCCESS) |
| 757 |
root |
1.58 |
goto done; |
| 758 |
|
|
|
| 759 |
|
|
RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!info.alpha_bits, 8, info.xsize, info.ysize); |
| 760 |
|
|
error = "unable to allocate pixbuf"; |
| 761 |
|
|
if (!RETVAL) |
| 762 |
|
|
goto done; |
| 763 |
|
|
|
| 764 |
|
|
JxlPixelFormat format = { |
| 765 |
|
|
info.alpha_bits ? 4 : 3, |
| 766 |
|
|
JXL_TYPE_UINT8, |
| 767 |
|
|
JXL_NATIVE_ENDIAN, |
| 768 |
|
|
gdk_pixbuf_get_rowstride (RETVAL) |
| 769 |
|
|
}; |
| 770 |
|
|
|
| 771 |
root |
1.63 |
// cannot use gdk_pixbuf_get_byte_length because that does |
| 772 |
|
|
// not return the size of the buffer, but the size of the buffer without |
| 773 |
|
|
// the last padding bytes. the internal buffer is rowstride * ysize, |
| 774 |
|
|
// and this is what the jxl decoder needs. none of this is documented |
| 775 |
|
|
// in either library, of course. |
| 776 |
|
|
|
| 777 |
|
|
status = JxlDecoderSetImageOutBuffer ( |
| 778 |
|
|
dec, |
| 779 |
|
|
&format, |
| 780 |
|
|
gdk_pixbuf_get_pixels (RETVAL), |
| 781 |
|
|
gdk_pixbuf_get_rowstride (RETVAL) * info.ysize |
| 782 |
|
|
); |
| 783 |
root |
1.58 |
error = "JxlDecoderSetImageOutBuffer failed"; |
| 784 |
root |
1.63 |
if (status != JXL_DEC_SUCCESS) |
| 785 |
root |
1.58 |
goto done; |
| 786 |
|
|
} |
| 787 |
|
|
break; |
| 788 |
|
|
|
| 789 |
|
|
default: |
| 790 |
|
|
error = "unexpected event"; |
| 791 |
|
|
goto done; |
| 792 |
|
|
} |
| 793 |
|
|
} |
| 794 |
|
|
|
| 795 |
|
|
done: |
| 796 |
|
|
if (dec) |
| 797 |
|
|
JxlDecoderDestroy (dec); |
| 798 |
|
|
|
| 799 |
root |
1.65 |
perlinterp_acquire (); |
| 800 |
|
|
|
| 801 |
|
|
if (runner_cache) |
| 802 |
root |
1.58 |
JxlThreadParallelRunnerDestroy (runner); |
| 803 |
|
|
|
| 804 |
root |
1.65 |
runner_cache = runner; |
| 805 |
root |
1.58 |
|
| 806 |
|
|
if (error) |
| 807 |
|
|
{ |
| 808 |
|
|
if (RETVAL) |
| 809 |
|
|
g_object_unref (RETVAL); |
| 810 |
|
|
|
| 811 |
root |
1.63 |
croak ("load_jxl: %s (status %d)", error, status); |
| 812 |
root |
1.58 |
} |
| 813 |
|
|
#else |
| 814 |
|
|
croak ("load_jxl: jpeg-xl not enabled at compile time"); |
| 815 |
|
|
#endif |
| 816 |
|
|
} |
| 817 |
|
|
OUTPUT: |
| 818 |
|
|
RETVAL |
| 819 |
|
|
|
| 820 |
|
|
GdkPixbuf_noinc * |
| 821 |
root |
1.71 |
decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
| 822 |
root |
1.17 |
CODE: |
| 823 |
|
|
{ |
| 824 |
root |
1.71 |
#if WEBP |
| 825 |
|
|
STRLEN data_size; |
| 826 |
|
|
int alpha; |
| 827 |
|
|
WebPData data; |
| 828 |
|
|
WebPDemuxer *demux; |
| 829 |
|
|
WebPIterator iter; |
| 830 |
|
|
WebPDecoderConfig config; |
| 831 |
|
|
int inw, inh; |
| 832 |
root |
1.23 |
|
| 833 |
root |
1.71 |
data.bytes = (uint8_t *)SvPVbyte (image_data, data_size); |
| 834 |
|
|
data.size = data_size; |
| 835 |
root |
1.17 |
|
| 836 |
root |
1.46 |
perlinterp_release (); |
| 837 |
|
|
|
| 838 |
root |
1.71 |
RETVAL = 0; |
| 839 |
root |
1.17 |
|
| 840 |
root |
1.71 |
if (!(demux = WebPDemux (&data))) |
| 841 |
|
|
goto err_demux; |
| 842 |
root |
1.17 |
|
| 843 |
root |
1.71 |
if (!WebPDemuxGetFrame (demux, 1, &iter)) |
| 844 |
|
|
goto err_iter; |
| 845 |
root |
1.17 |
|
| 846 |
root |
1.71 |
if (!WebPInitDecoderConfig (&config)) |
| 847 |
|
|
goto err_iter; |
| 848 |
root |
1.17 |
|
| 849 |
root |
1.71 |
config.options.use_threads = 1; |
| 850 |
root |
1.17 |
|
| 851 |
root |
1.71 |
if (WebPGetFeatures (iter.fragment.bytes, iter.fragment.size, &config.input) != VP8_STATUS_OK) |
| 852 |
|
|
goto err_iter; |
| 853 |
root |
1.57 |
|
| 854 |
root |
1.71 |
inw = config.input.width; |
| 855 |
|
|
inh = config.input.height; |
| 856 |
root |
1.17 |
|
| 857 |
root |
1.71 |
if (thumbnail) |
| 858 |
|
|
{ |
| 859 |
|
|
if (inw * ih > inh * iw) |
| 860 |
|
|
ih = (iw * inh + inw - 1) / inw; |
| 861 |
|
|
else |
| 862 |
|
|
iw = (ih * inw + inh - 1) / inh; |
| 863 |
root |
1.17 |
|
| 864 |
root |
1.71 |
config.options.bypass_filtering = 1; |
| 865 |
|
|
config.options.no_fancy_upsampling = 1; |
| 866 |
root |
1.17 |
|
| 867 |
root |
1.71 |
config.options.use_scaling = 1; |
| 868 |
|
|
config.options.scaled_width = iw; |
| 869 |
|
|
config.options.scaled_height = ih; |
| 870 |
|
|
} |
| 871 |
|
|
else |
| 872 |
|
|
{ |
| 873 |
|
|
iw = inw; |
| 874 |
|
|
ih = inh; |
| 875 |
|
|
} |
| 876 |
root |
1.17 |
|
| 877 |
root |
1.71 |
alpha = !!config.input.has_alpha; |
| 878 |
root |
1.17 |
|
| 879 |
root |
1.71 |
RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, alpha, 8, iw, ih); |
| 880 |
|
|
if (!RETVAL) |
| 881 |
|
|
goto err_iter; |
| 882 |
root |
1.17 |
|
| 883 |
root |
1.71 |
config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB; |
| 884 |
|
|
config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL); |
| 885 |
|
|
config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL); |
| 886 |
|
|
config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL); |
| 887 |
|
|
config.output.is_external_memory = 1; |
| 888 |
root |
1.17 |
|
| 889 |
root |
1.71 |
if (WebPDecode (iter.fragment.bytes, iter.fragment.size, &config) != VP8_STATUS_OK) |
| 890 |
root |
1.48 |
{ |
| 891 |
root |
1.71 |
g_object_unref (RETVAL); |
| 892 |
|
|
RETVAL = 0; |
| 893 |
|
|
goto err_iter; |
| 894 |
root |
1.48 |
} |
| 895 |
|
|
|
| 896 |
root |
1.71 |
err_iter: |
| 897 |
|
|
WebPDemuxReleaseIterator (&iter); |
| 898 |
|
|
err_demux: |
| 899 |
|
|
WebPDemuxDelete (demux); |
| 900 |
|
|
|
| 901 |
|
|
perlinterp_acquire (); |
| 902 |
|
|
#else |
| 903 |
|
|
croak ("load_webp: webp not enabled at compile time"); |
| 904 |
|
|
#endif |
| 905 |
|
|
} |
| 906 |
|
|
OUTPUT: |
| 907 |
|
|
RETVAL |
| 908 |
root |
1.17 |
|
| 909 |
root |
1.71 |
GdkPixbuf_noinc * |
| 910 |
|
|
decode_avif (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
| 911 |
|
|
CODE: |
| 912 |
|
|
{ |
| 913 |
|
|
#if AVIF |
| 914 |
|
|
STRLEN data_size; |
| 915 |
|
|
uint8_t *data; |
| 916 |
|
|
const char *error = 0; |
| 917 |
|
|
avifDecoder *decoder = 0; |
| 918 |
|
|
avifImage *image = 0; |
| 919 |
root |
1.17 |
|
| 920 |
root |
1.71 |
data = (uint8_t *)SvPVbyte (image_data, data_size); |
| 921 |
root |
1.17 |
|
| 922 |
root |
1.71 |
perlinterp_release (); |
| 923 |
root |
1.17 |
|
| 924 |
root |
1.71 |
RETVAL = 0; |
| 925 |
|
|
decoder = avifDecoderCreate (); |
| 926 |
|
|
image = avifImageCreateEmpty (); |
| 927 |
root |
1.17 |
|
| 928 |
root |
1.71 |
if (!decoder || !image) |
| 929 |
|
|
{ |
| 930 |
|
|
error = "unable to create avif decoder or image"; |
| 931 |
|
|
goto error; |
| 932 |
root |
1.17 |
} |
| 933 |
|
|
|
| 934 |
root |
1.71 |
avifResult result = avifDecoderReadMemory (decoder, image, data, data_size); |
| 935 |
|
|
if (result != AVIF_RESULT_OK) |
| 936 |
|
|
{ |
| 937 |
|
|
error = avifResultToString (result); |
| 938 |
|
|
goto error; |
| 939 |
|
|
} |
| 940 |
|
|
|
| 941 |
|
|
RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!image->alphaPlane, 8, image->width, image->height); |
| 942 |
|
|
if (!RETVAL) |
| 943 |
root |
1.48 |
{ |
| 944 |
root |
1.71 |
error = "unable to create pixbuf for avif decoding"; |
| 945 |
|
|
goto error; |
| 946 |
|
|
} |
| 947 |
root |
1.48 |
|
| 948 |
root |
1.71 |
if (image->colorPrimaries == AVIF_COLOR_PRIMARIES_UNSPECIFIED) |
| 949 |
|
|
image->colorPrimaries = AVIF_COLOR_PRIMARIES_XYZ; // libavif seems to ignore this without icc profile |
| 950 |
|
|
if (image->transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED) |
| 951 |
|
|
image->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_BT709; // libavif seems to ignore this without icc profile |
| 952 |
|
|
if (image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED) |
| 953 |
|
|
image->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT709; |
| 954 |
|
|
|
| 955 |
|
|
avifRGBImage rgb; |
| 956 |
|
|
avifRGBImageSetDefaults (&rgb, image); |
| 957 |
|
|
|
| 958 |
|
|
rgb.format = image->alphaPlane ? AVIF_RGB_FORMAT_RGBA : AVIF_RGB_FORMAT_RGB; |
| 959 |
|
|
rgb.depth = 8; |
| 960 |
|
|
rgb.pixels = gdk_pixbuf_get_pixels (RETVAL); |
| 961 |
|
|
rgb.rowBytes = gdk_pixbuf_get_rowstride (RETVAL); |
| 962 |
|
|
|
| 963 |
|
|
avifImageYUVToRGB (image, &rgb); |
| 964 |
|
|
|
| 965 |
|
|
error: |
| 966 |
|
|
|
| 967 |
|
|
if (image) |
| 968 |
|
|
avifImageDestroy (image); |
| 969 |
|
|
if (decoder) |
| 970 |
|
|
avifDecoderDestroy (decoder); |
| 971 |
root |
1.48 |
|
| 972 |
root |
1.71 |
perlinterp_acquire (); |
| 973 |
root |
1.49 |
|
| 974 |
root |
1.71 |
if (error) |
| 975 |
|
|
{ |
| 976 |
|
|
if (RETVAL) |
| 977 |
|
|
g_object_unref (RETVAL); |
| 978 |
root |
1.48 |
|
| 979 |
root |
1.71 |
croak (error); |
| 980 |
root |
1.48 |
} |
| 981 |
root |
1.71 |
#else |
| 982 |
|
|
croak ("load_avif: avif not enabled at compile time"); |
| 983 |
|
|
#endif |
| 984 |
root |
1.17 |
} |
| 985 |
|
|
OUTPUT: |
| 986 |
|
|
RETVAL |
| 987 |
|
|
|
| 988 |
root |
1.33 |
void |
| 989 |
|
|
compare (GdkPixbuf *a, GdkPixbuf *b) |
| 990 |
|
|
PPCODE: |
| 991 |
root |
1.46 |
perlinterp_release (); |
| 992 |
root |
1.33 |
{ |
| 993 |
|
|
int w = gdk_pixbuf_get_width (a); |
| 994 |
|
|
int h = gdk_pixbuf_get_height (a); |
| 995 |
root |
1.43 |
|
| 996 |
root |
1.33 |
int sa = gdk_pixbuf_get_rowstride (a); |
| 997 |
|
|
int sb = gdk_pixbuf_get_rowstride (b); |
| 998 |
|
|
|
| 999 |
|
|
guchar *pa = gdk_pixbuf_get_pixels (a); |
| 1000 |
|
|
guchar *pb = gdk_pixbuf_get_pixels (b); |
| 1001 |
|
|
|
| 1002 |
|
|
int x, y; |
| 1003 |
|
|
|
| 1004 |
|
|
assert (w == gdk_pixbuf_get_width (b)); |
| 1005 |
|
|
assert (h == gdk_pixbuf_get_height (b)); |
| 1006 |
|
|
|
| 1007 |
|
|
assert (gdk_pixbuf_get_n_channels (a) == 3); |
| 1008 |
|
|
assert (gdk_pixbuf_get_n_channels (b) == 3); |
| 1009 |
|
|
|
| 1010 |
|
|
double diff = 0.; |
| 1011 |
|
|
int peak = 0; |
| 1012 |
|
|
|
| 1013 |
root |
1.35 |
if (w && h) |
| 1014 |
root |
1.34 |
for (y = 0; y < h; y++) |
| 1015 |
|
|
{ |
| 1016 |
|
|
guchar *pa_ = pa + y * sa; |
| 1017 |
|
|
guchar *pb_ = pb + y * sb; |
| 1018 |
root |
1.33 |
|
| 1019 |
root |
1.34 |
for (x = 0; x < w; x++) |
| 1020 |
|
|
{ |
| 1021 |
|
|
int d; |
| 1022 |
|
|
|
| 1023 |
|
|
d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
| 1024 |
|
|
d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
| 1025 |
|
|
d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
| 1026 |
|
|
} |
| 1027 |
|
|
} |
| 1028 |
root |
1.33 |
|
| 1029 |
root |
1.46 |
perlinterp_acquire (); |
| 1030 |
|
|
|
| 1031 |
root |
1.33 |
EXTEND (SP, 2); |
| 1032 |
|
|
PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
| 1033 |
|
|
PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
| 1034 |
|
|
} |
| 1035 |
|
|
|
| 1036 |
root |
1.6 |
############################################################################# |
| 1037 |
|
|
|
| 1038 |
root |
1.2 |
MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer |
| 1039 |
|
|
|
| 1040 |
root |
1.41 |
# currently only works for filenames (octet strings) |
| 1041 |
|
|
|
| 1042 |
root |
1.18 |
SV * |
| 1043 |
|
|
foldcase (SV *pathsv) |
| 1044 |
|
|
PROTOTYPE: $ |
| 1045 |
|
|
CODE: |
| 1046 |
|
|
{ |
| 1047 |
|
|
STRLEN plen; |
| 1048 |
root |
1.41 |
U8 *path = (U8 *)SvPV (pathsv, plen); |
| 1049 |
root |
1.18 |
U8 *pend = path + plen; |
| 1050 |
root |
1.60 |
U8 dst [plen * 8 * 3], *dstp = dst; |
| 1051 |
root |
1.18 |
|
| 1052 |
|
|
while (path < pend) |
| 1053 |
|
|
{ |
| 1054 |
root |
1.19 |
U8 ch = *path; |
| 1055 |
|
|
|
| 1056 |
|
|
if (ch >= 'a' && ch <= 'z') |
| 1057 |
|
|
*dstp++ = *path++; |
| 1058 |
root |
1.41 |
else if (ch >= 'A' && ch <= 'Z') |
| 1059 |
|
|
*dstp++ = *path++ + ('a' - 'A'); |
| 1060 |
root |
1.19 |
else if (ch >= '0' && ch <= '9') |
| 1061 |
root |
1.18 |
{ |
| 1062 |
root |
1.60 |
/* version sort, up to 8 digits */ |
| 1063 |
root |
1.18 |
STRLEN el, nl = 0; |
| 1064 |
root |
1.19 |
while (*path >= '0' && *path <= '9' && path < pend) |
| 1065 |
root |
1.18 |
path++, nl++; |
| 1066 |
|
|
|
| 1067 |
root |
1.60 |
for (el = nl; el < 8; el++) |
| 1068 |
root |
1.18 |
*dstp++ = '0'; |
| 1069 |
|
|
|
| 1070 |
|
|
memcpy (dstp, path - nl, nl); |
| 1071 |
|
|
dstp += nl; |
| 1072 |
|
|
} |
| 1073 |
|
|
else |
| 1074 |
root |
1.41 |
*dstp++ = *path++; |
| 1075 |
|
|
#if 0 |
| 1076 |
|
|
else |
| 1077 |
root |
1.18 |
{ |
| 1078 |
|
|
STRLEN cl; |
| 1079 |
|
|
to_utf8_fold (path, dstp, &cl); |
| 1080 |
|
|
dstp += cl; |
| 1081 |
|
|
path += is_utf8_char (path); |
| 1082 |
|
|
} |
| 1083 |
root |
1.41 |
#endif |
| 1084 |
root |
1.18 |
} |
| 1085 |
|
|
|
| 1086 |
root |
1.25 |
RETVAL = newSVpvn ((const char *)dst, dstp - dst); |
| 1087 |
root |
1.18 |
} |
| 1088 |
|
|
OUTPUT: |
| 1089 |
|
|
RETVAL |
| 1090 |
|
|
|
| 1091 |
root |
1.3 |
GdkPixbuf_noinc * |
| 1092 |
root |
1.24 |
p7_to_pb (int w, int h, SV *src_sv) |
| 1093 |
root |
1.21 |
PROTOTYPE: @ |
| 1094 |
root |
1.2 |
CODE: |
| 1095 |
|
|
{ |
| 1096 |
|
|
int x, y; |
| 1097 |
|
|
guchar *dst, *d; |
| 1098 |
|
|
int dstr; |
| 1099 |
root |
1.25 |
guchar *src = (guchar *)SvPVbyte_nolen (src_sv); |
| 1100 |
root |
1.2 |
|
| 1101 |
|
|
RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); |
| 1102 |
|
|
dst = gdk_pixbuf_get_pixels (RETVAL); |
| 1103 |
|
|
dstr = gdk_pixbuf_get_rowstride (RETVAL); |
| 1104 |
|
|
|
| 1105 |
|
|
for (y = 0; y < h; y++) |
| 1106 |
|
|
for (d = dst + y * dstr, x = 0; x < w; x++) |
| 1107 |
|
|
{ |
| 1108 |
root |
1.3 |
*d++ = (((*src >> 5) & 7) * 255 + 4) / 7; |
| 1109 |
|
|
*d++ = (((*src >> 2) & 7) * 255 + 4) / 7; |
| 1110 |
|
|
*d++ = (((*src >> 0) & 3) * 255 + 2) / 3; |
| 1111 |
root |
1.2 |
|
| 1112 |
|
|
src++; |
| 1113 |
|
|
} |
| 1114 |
root |
1.3 |
} |
| 1115 |
|
|
OUTPUT: |
| 1116 |
|
|
RETVAL |
| 1117 |
|
|
|
| 1118 |
root |
1.6 |
############################################################################# |
| 1119 |
root |
1.4 |
|
| 1120 |
|
|
MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript |
| 1121 |
|
|
|
| 1122 |
|
|
void |
| 1123 |
root |
1.7 |
dump_ascii85 (PerlIO *fp, GdkPixbuf *pb) |
| 1124 |
root |
1.4 |
CODE: |
| 1125 |
|
|
{ |
| 1126 |
|
|
int w = gdk_pixbuf_get_width (pb); |
| 1127 |
|
|
int h = gdk_pixbuf_get_height (pb); |
| 1128 |
|
|
int x, y, i; |
| 1129 |
|
|
guchar *dst; |
| 1130 |
root |
1.7 |
int bpp = gdk_pixbuf_get_n_channels (pb); |
| 1131 |
root |
1.4 |
guchar *src = gdk_pixbuf_get_pixels (pb); |
| 1132 |
|
|
int sstr = gdk_pixbuf_get_rowstride (pb); |
| 1133 |
|
|
|
| 1134 |
|
|
a85_init (); |
| 1135 |
|
|
|
| 1136 |
|
|
for (y = 0; y < h; y++) |
| 1137 |
|
|
for (x = 0; x < w; x++) |
| 1138 |
root |
1.7 |
for (i = 0; i < (bpp < 3 ? 1 : 3); i++) |
| 1139 |
root |
1.4 |
a85_push (fp, src [x * bpp + y * sstr + i]); |
| 1140 |
|
|
|
| 1141 |
|
|
a85_finish (fp); |
| 1142 |
root |
1.7 |
} |
| 1143 |
|
|
|
| 1144 |
|
|
void |
| 1145 |
|
|
dump_binary (PerlIO *fp, GdkPixbuf *pb) |
| 1146 |
|
|
CODE: |
| 1147 |
|
|
{ |
| 1148 |
|
|
int w = gdk_pixbuf_get_width (pb); |
| 1149 |
|
|
int h = gdk_pixbuf_get_height (pb); |
| 1150 |
|
|
int x, y, i; |
| 1151 |
|
|
guchar *dst; |
| 1152 |
|
|
int bpp = gdk_pixbuf_get_n_channels (pb); |
| 1153 |
|
|
guchar *src = gdk_pixbuf_get_pixels (pb); |
| 1154 |
|
|
int sstr = gdk_pixbuf_get_rowstride (pb); |
| 1155 |
|
|
|
| 1156 |
|
|
for (y = 0; y < h; y++) |
| 1157 |
|
|
for (x = 0; x < w; x++) |
| 1158 |
|
|
for (i = 0; i < (bpp < 3 ? 1 : 3); i++) |
| 1159 |
|
|
PerlIO_putc (fp, src [x * bpp + y * sstr + i]); |
| 1160 |
root |
1.4 |
} |
| 1161 |
root |
1.8 |
|
| 1162 |
|
|
############################################################################# |
| 1163 |
|
|
|
| 1164 |
|
|
MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
| 1165 |
|
|
|
| 1166 |
|
|
SV * |
| 1167 |
|
|
pb_to_hv84 (GdkPixbuf *pb) |
| 1168 |
|
|
CODE: |
| 1169 |
|
|
{ |
| 1170 |
|
|
int w = gdk_pixbuf_get_width (pb); |
| 1171 |
|
|
int h = gdk_pixbuf_get_height (pb); |
| 1172 |
|
|
int x, y; |
| 1173 |
|
|
guchar *dst; |
| 1174 |
|
|
int bpp = gdk_pixbuf_get_n_channels (pb); |
| 1175 |
|
|
guchar *src = gdk_pixbuf_get_pixels (pb); |
| 1176 |
|
|
int sstr = gdk_pixbuf_get_rowstride (pb); |
| 1177 |
|
|
|
| 1178 |
|
|
RETVAL = newSV (6 * 8 * 12 / 8); |
| 1179 |
|
|
SvPOK_only (RETVAL); |
| 1180 |
|
|
SvCUR_set (RETVAL, 6 * 8 * 12 / 8); |
| 1181 |
|
|
|
| 1182 |
root |
1.25 |
dst = (guchar *)SvPVX (RETVAL); |
| 1183 |
root |
1.8 |
|
| 1184 |
|
|
/* some primitive error distribution + random dithering */ |
| 1185 |
|
|
|
| 1186 |
|
|
for (y = 0; y < h; y++) |
| 1187 |
|
|
{ |
| 1188 |
|
|
guchar *p = src + y * sstr; |
| 1189 |
|
|
|
| 1190 |
|
|
for (x = 0; x < w; x += 2) |
| 1191 |
|
|
{ |
| 1192 |
|
|
unsigned int r, g, b, h, s, v, H, V1, V2; |
| 1193 |
|
|
|
| 1194 |
|
|
if (bpp == 3) |
| 1195 |
|
|
r = *p++, g = *p++, b = *p++; |
| 1196 |
|
|
else if (bpp == 1) |
| 1197 |
|
|
r = g = b = *p++; |
| 1198 |
|
|
else |
| 1199 |
|
|
abort (); |
| 1200 |
|
|
|
| 1201 |
|
|
rgb_to_hsv (r, g, b, &h, &s, &v); |
| 1202 |
|
|
|
| 1203 |
|
|
H = (h * 15 / 255) << 4; |
| 1204 |
|
|
V1 = v; |
| 1205 |
|
|
|
| 1206 |
|
|
if (bpp == 3) |
| 1207 |
|
|
r = *p++, g = *p++, b = *p++; |
| 1208 |
|
|
else if (bpp == 1) |
| 1209 |
|
|
r = g = b = *p++; |
| 1210 |
|
|
else |
| 1211 |
|
|
abort (); |
| 1212 |
|
|
|
| 1213 |
|
|
rgb_to_hsv (r, g, b, &h, &s, &v); |
| 1214 |
|
|
|
| 1215 |
|
|
H |= h * 15 / 255; |
| 1216 |
|
|
V2 = v; |
| 1217 |
|
|
|
| 1218 |
|
|
*dst++ = H; |
| 1219 |
|
|
*dst++ = V1; |
| 1220 |
|
|
*dst++ = V2; |
| 1221 |
|
|
} |
| 1222 |
|
|
} |
| 1223 |
|
|
} |
| 1224 |
|
|
OUTPUT: |
| 1225 |
|
|
RETVAL |
| 1226 |
root |
1.4 |
|
| 1227 |
root |
1.9 |
SV * |
| 1228 |
|
|
hv84_to_av (unsigned char *hv84) |
| 1229 |
|
|
CODE: |
| 1230 |
|
|
{ |
| 1231 |
|
|
int i = 72 / 3; |
| 1232 |
|
|
AV *av = newAV (); |
| 1233 |
|
|
|
| 1234 |
|
|
RETVAL = (SV *)newRV_noinc ((SV *)av); |
| 1235 |
|
|
while (i--) |
| 1236 |
|
|
{ |
| 1237 |
|
|
int h = *hv84++; |
| 1238 |
|
|
int v1 = *hv84++; |
| 1239 |
|
|
int v2 = *hv84++; |
| 1240 |
|
|
|
| 1241 |
|
|
av_push (av, newSViv (v1)); |
| 1242 |
|
|
av_push (av, newSViv ((h >> 4) * 255 / 15)); |
| 1243 |
|
|
av_push (av, newSViv (v2)); |
| 1244 |
|
|
av_push (av, newSViv ((h & 15) * 255 / 15)); |
| 1245 |
|
|
} |
| 1246 |
|
|
} |
| 1247 |
|
|
OUTPUT: |
| 1248 |
|
|
RETVAL |
| 1249 |
root |
1.2 |
|
| 1250 |
root |
1.20 |
############################################################################# |
| 1251 |
|
|
|
| 1252 |
|
|
MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster |
| 1253 |
|
|
|
| 1254 |
|
|
SV * |
| 1255 |
root |
1.36 |
extract_features (SV *ar) |
| 1256 |
|
|
CODE: |
| 1257 |
|
|
{ |
| 1258 |
|
|
int i; |
| 1259 |
|
|
AV *av, *result; |
| 1260 |
|
|
|
| 1261 |
|
|
if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) |
| 1262 |
root |
1.39 |
croak ("Not an array ref as first argument to extract_features"); |
| 1263 |
root |
1.36 |
|
| 1264 |
|
|
av = (AV *) SvRV (ar); |
| 1265 |
|
|
result = newAV (); |
| 1266 |
|
|
|
| 1267 |
|
|
for (i = 0; i <= av_len (av); ++i) |
| 1268 |
|
|
{ |
| 1269 |
|
|
SV *sv = *av_fetch (av, i, 1); |
| 1270 |
|
|
SV *histsv = newSV (9 * sizeof (float) + 1); |
| 1271 |
|
|
|
| 1272 |
|
|
SvPOK_on (histsv); |
| 1273 |
|
|
SvCUR_set (histsv, 9 * sizeof (float)); |
| 1274 |
|
|
float *hist = (float *)SvPVX (histsv); |
| 1275 |
|
|
|
| 1276 |
|
|
struct feature f_h, f_s, f_v; |
| 1277 |
|
|
feature_init (&f_h); |
| 1278 |
|
|
feature_init (&f_s); |
| 1279 |
|
|
feature_init (&f_v); |
| 1280 |
|
|
|
| 1281 |
|
|
{ |
| 1282 |
|
|
STRLEN len; |
| 1283 |
|
|
unsigned char *buf = (unsigned char *)SvPVbyte (sv, len); |
| 1284 |
|
|
while (len >= 3) |
| 1285 |
|
|
{ |
| 1286 |
|
|
unsigned int r, g, b, h, s, v; |
| 1287 |
|
|
r = *buf++; g = *buf++; b = *buf++; |
| 1288 |
|
|
rgb_to_hsv (r, g, b, &h, &s, &v); |
| 1289 |
|
|
|
| 1290 |
|
|
feature_update_pass_1 (&f_h, h); |
| 1291 |
|
|
feature_update_pass_1 (&f_s, s); |
| 1292 |
|
|
feature_update_pass_1 (&f_v, v); |
| 1293 |
|
|
|
| 1294 |
|
|
len -= 3; |
| 1295 |
|
|
} |
| 1296 |
|
|
|
| 1297 |
|
|
feature_finish_pass_1 (&f_h); |
| 1298 |
|
|
feature_finish_pass_1 (&f_s); |
| 1299 |
|
|
feature_finish_pass_1 (&f_v); |
| 1300 |
|
|
} |
| 1301 |
|
|
|
| 1302 |
|
|
{ |
| 1303 |
|
|
STRLEN len; |
| 1304 |
|
|
unsigned char *buf = (unsigned char *)SvPVbyte (sv, len); |
| 1305 |
|
|
while (len >= 3) |
| 1306 |
|
|
{ |
| 1307 |
|
|
unsigned int r, g, b, h, s, v; |
| 1308 |
|
|
r = *buf++; g = *buf++; b = *buf++; |
| 1309 |
|
|
rgb_to_hsv (r, g, b, &h, &s, &v); |
| 1310 |
|
|
|
| 1311 |
|
|
feature_update_pass_2 (&f_h, h); |
| 1312 |
|
|
feature_update_pass_2 (&f_s, s); |
| 1313 |
|
|
feature_update_pass_2 (&f_v, v); |
| 1314 |
|
|
|
| 1315 |
|
|
len -= 3; |
| 1316 |
|
|
} |
| 1317 |
|
|
|
| 1318 |
|
|
feature_finish_pass_2 (&f_h); |
| 1319 |
|
|
feature_finish_pass_2 (&f_s); |
| 1320 |
|
|
feature_finish_pass_2 (&f_v); |
| 1321 |
|
|
} |
| 1322 |
|
|
|
| 1323 |
|
|
hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.; |
| 1324 |
|
|
hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ; |
| 1325 |
root |
1.38 |
hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5; |
| 1326 |
root |
1.36 |
|
| 1327 |
|
|
av_push (result, histsv); |
| 1328 |
|
|
} |
| 1329 |
|
|
|
| 1330 |
|
|
RETVAL = newRV_noinc ((SV *)result); |
| 1331 |
|
|
} |
| 1332 |
|
|
OUTPUT: |
| 1333 |
|
|
RETVAL |
| 1334 |
|
|
|