Module: SafeImage::Native
- Defined in:
- ext/safe_image_native/safe_image_native.c
Class Method Summary collapse
- .crop_north(input_val, output_val, width_val, height_val, format_val, quality_val, max_pixels_val) ⇒ Object
- .probe(path_val) ⇒ Object
- .resize(input_val, output_val, scale_val, format_val, quality_val, max_pixels_val) ⇒ Object
- .thumbnail(input_val, output_val, width_val, height_val, format_val, quality_val, max_pixels_val) ⇒ Object
Class Method Details
.crop_north(input_val, output_val, width_val, height_val, format_val, quality_val, max_pixels_val) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'ext/safe_image_native/safe_image_native.c', line 306
static VALUE rb_crop_north(VALUE self, VALUE input_val, VALUE output_val, VALUE width_val, VALUE height_val, VALUE format_val, VALUE quality_val, VALUE max_pixels_val) {
Check_Type(input_val, T_STRING);
Check_Type(output_val, T_STRING);
Check_Type(format_val, T_STRING);
int width = NUM2INT(width_val);
int height = NUM2INT(height_val);
int quality = NUM2INT(quality_val);
validate_dimensions_or_raise(width, height);
validate_quality_or_raise(quality);
const char *out_fmt = normalized_format(StringValueCStr(format_val));
if (!out_fmt || strcmp(out_fmt, "heic") == 0) rb_raise(eUnsupported, "unsupported output format");
double start = now_ms();
const char *input_fmt = NULL;
VipsImage *in = load_explicit(StringValueCStr(input_val), &input_fmt);
long long pixels = 0, max_pixels = 0;
if (pixels_exceed_limit(in, max_pixels_val, &pixels, &max_pixels)) {
g_object_unref(in);
raise_pixels_limit(pixels, max_pixels);
}
VipsImage *rot = NULL;
if (vips_autorot(in, &rot, NULL) != 0) {
g_object_unref(in);
raise_vips();
}
double sx = (double)width / (double)rot->Xsize;
double sy = (double)height / (double)rot->Ysize;
double scale = sx > sy ? sx : sy;
scale *= 1.0000001;
VipsImage *resized = NULL;
if (vips_resize(rot, &resized, scale, NULL) != 0) {
g_object_unref(rot);
g_object_unref(in);
raise_vips();
}
int left = (resized->Xsize - width) / 2;
if (left < 0) left = 0;
VipsImage *crop = NULL;
if (vips_extract_area(resized, &crop, left, 0, width, height, NULL) != 0) {
g_object_unref(resized);
g_object_unref(rot);
g_object_unref(in);
raise_vips();
}
if (save_explicit(crop, StringValueCStr(output_val), out_fmt, quality) != 0) {
g_object_unref(crop);
g_object_unref(resized);
g_object_unref(rot);
g_object_unref(in);
raise_vips();
}
int out_width = crop->Xsize;
int out_height = crop->Ysize;
double duration_ms = now_ms() - start;
g_object_unref(crop);
g_object_unref(resized);
g_object_unref(rot);
g_object_unref(in);
VALUE hash = rb_hash_new();
rb_hash_aset(hash, ID2SYM(rb_intern("input_format")), rb_str_new_cstr(input_fmt));
rb_hash_aset(hash, ID2SYM(rb_intern("output_format")), rb_str_new_cstr(out_fmt));
rb_hash_aset(hash, ID2SYM(rb_intern("width")), INT2NUM(out_width));
rb_hash_aset(hash, ID2SYM(rb_intern("height")), INT2NUM(out_height));
rb_hash_aset(hash, ID2SYM(rb_intern("duration_ms")), DBL2NUM(duration_ms));
return hash;
}
|
.probe(path_val) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'ext/safe_image_native/safe_image_native.c', line 171
static VALUE rb_probe(VALUE self, VALUE path_val) {
Check_Type(path_val, T_STRING);
double start = now_ms();
const char *fmt = NULL;
VipsImage *image = load_explicit(StringValueCStr(path_val), &fmt);
int width = image->Xsize;
int height = image->Ysize;
double duration_ms = now_ms() - start;
g_object_unref(image);
VALUE hash = rb_hash_new();
rb_hash_aset(hash, ID2SYM(rb_intern("format")), rb_str_new_cstr(fmt));
rb_hash_aset(hash, ID2SYM(rb_intern("width")), INT2NUM(width));
rb_hash_aset(hash, ID2SYM(rb_intern("height")), INT2NUM(height));
rb_hash_aset(hash, ID2SYM(rb_intern("duration_ms")), DBL2NUM(duration_ms));
return hash;
}
|
.resize(input_val, output_val, scale_val, format_val, quality_val, max_pixels_val) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'ext/safe_image_native/safe_image_native.c', line 250
static VALUE rb_resize(VALUE self, VALUE input_val, VALUE output_val, VALUE scale_val, VALUE format_val, VALUE quality_val, VALUE max_pixels_val) {
Check_Type(input_val, T_STRING);
Check_Type(output_val, T_STRING);
Check_Type(format_val, T_STRING);
double scale = NUM2DBL(scale_val);
int quality = NUM2INT(quality_val);
validate_scale_or_raise(scale);
validate_quality_or_raise(quality);
const char *out_fmt = normalized_format(StringValueCStr(format_val));
if (!out_fmt || strcmp(out_fmt, "heic") == 0) rb_raise(eUnsupported, "unsupported output format");
double start = now_ms();
const char *input_fmt = NULL;
VipsImage *in = load_explicit(StringValueCStr(input_val), &input_fmt);
long long pixels = 0, max_pixels = 0;
if (pixels_exceed_limit(in, max_pixels_val, &pixels, &max_pixels)) {
g_object_unref(in);
raise_pixels_limit(pixels, max_pixels);
}
VipsImage *rot = NULL;
if (vips_autorot(in, &rot, NULL) != 0) {
g_object_unref(in);
raise_vips();
}
VipsImage *out = NULL;
if (vips_resize(rot, &out, scale, NULL) != 0) {
g_object_unref(rot);
g_object_unref(in);
raise_vips();
}
if (save_explicit(out, StringValueCStr(output_val), out_fmt, quality) != 0) {
g_object_unref(out);
g_object_unref(rot);
g_object_unref(in);
raise_vips();
}
int out_width = out->Xsize;
int out_height = out->Ysize;
double duration_ms = now_ms() - start;
g_object_unref(out);
g_object_unref(rot);
g_object_unref(in);
VALUE hash = rb_hash_new();
rb_hash_aset(hash, ID2SYM(rb_intern("input_format")), rb_str_new_cstr(input_fmt));
rb_hash_aset(hash, ID2SYM(rb_intern("output_format")), rb_str_new_cstr(out_fmt));
rb_hash_aset(hash, ID2SYM(rb_intern("width")), INT2NUM(out_width));
rb_hash_aset(hash, ID2SYM(rb_intern("height")), INT2NUM(out_height));
rb_hash_aset(hash, ID2SYM(rb_intern("duration_ms")), DBL2NUM(duration_ms));
return hash;
}
|
.thumbnail(input_val, output_val, width_val, height_val, format_val, quality_val, max_pixels_val) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'ext/safe_image_native/safe_image_native.c', line 189
static VALUE rb_thumbnail(VALUE self, VALUE input_val, VALUE output_val, VALUE width_val, VALUE height_val, VALUE format_val, VALUE quality_val, VALUE max_pixels_val) {
Check_Type(input_val, T_STRING);
Check_Type(output_val, T_STRING);
Check_Type(format_val, T_STRING);
int width = NUM2INT(width_val);
int height = NUM2INT(height_val);
int quality = NUM2INT(quality_val);
validate_dimensions_or_raise(width, height);
validate_quality_or_raise(quality);
const char *out_fmt = normalized_format(StringValueCStr(format_val));
if (!out_fmt || strcmp(out_fmt, "heic") == 0) rb_raise(eUnsupported, "unsupported output format");
const char *input_path = StringValueCStr(input_val);
double start = now_ms();
/* Read the header through an explicit allowlisted loader. This validates the
* input format (the loader fails on mismatched bytes) and lets us enforce the
* pixel-count limit before any full decode happens. */
const char *input_fmt = NULL;
VipsImage *header = load_explicit(input_path, &input_fmt);
long long pixels = 0, max_pixels = 0;
if (pixels_exceed_limit(header, max_pixels_val, &pixels, &max_pixels)) {
g_object_unref(header);
raise_pixels_limit(pixels, max_pixels);
}
g_object_unref(header);
/* Thumbnail straight from the file so libvips can shrink on load (e.g.
* libjpeg DCT downscaling) instead of decoding the source at full
* resolution. vips_thumbnail auto-rotates from the orientation tag by
* default. ImageMagick loader classes are blocked globally in
* init_vips_once, so this still cannot reach an ImageMagick delegate. */
VipsImage *thumb = NULL;
if (vips_thumbnail(input_path, &thumb, width,
"height", height,
"size", VIPS_SIZE_BOTH,
"crop", VIPS_INTERESTING_CENTRE,
"fail_on", VIPS_FAIL_ON_ERROR,
NULL) != 0) {
raise_vips();
}
if (save_explicit(thumb, StringValueCStr(output_val), out_fmt, quality) != 0) {
g_object_unref(thumb);
raise_vips();
}
int out_width = thumb->Xsize;
int out_height = thumb->Ysize;
double duration_ms = now_ms() - start;
g_object_unref(thumb);
VALUE hash = rb_hash_new();
rb_hash_aset(hash, ID2SYM(rb_intern("input_format")), rb_str_new_cstr(input_fmt));
rb_hash_aset(hash, ID2SYM(rb_intern("output_format")), rb_str_new_cstr(out_fmt));
rb_hash_aset(hash, ID2SYM(rb_intern("width")), INT2NUM(out_width));
rb_hash_aset(hash, ID2SYM(rb_intern("height")), INT2NUM(out_height));
rb_hash_aset(hash, ID2SYM(rb_intern("duration_ms")), DBL2NUM(duration_ms));
return hash;
}
|