Class: PDFium::Image
- Inherits:
-
Object
- Object
- PDFium::Image
- Defined in:
- ext/pdfium_ext/image.cc,
ext/pdfium_ext/image.cc
Overview
A Image can represent either a Page that has been rendered to a Image via Page#as_image
Or an embedded image on a Page, obtained via Page#images
Instance Attribute Summary collapse
-
#bounds ⇒ Object
readonly
Returns the bouding box of the image as a BoundingBox.
-
#height ⇒ Object
Height of the image in pixels (Fixnum).
-
#index ⇒ Object
readonly
Returns the index of the image on the page.
-
#width ⇒ Object
Width of the image in pixels (Fixnum).
Instance Method Summary collapse
-
#as_science ⇒ ImageScience instance
Converts to an ImageScience bitmap and returns it.
-
#data(: format) ⇒ Object
Used in conjuction with Document.from_memory this can render be used to render a PDF’s pages completely in memory.
-
#save(file) ⇒ Boolean
Save image to a file.
Instance Attribute Details
#bounds ⇒ Object (readonly)
Returns the bouding box of the image as a BoundingBox
#height ⇒ Object
Height of the image in pixels (Fixnum)
#index ⇒ Object (readonly)
Returns the index of the image on the page. Note: The index method is only provided as a convience method. It has no relation to the position of images on the page. Do not depend on the top-left image being index 0, even if it often is.
#width ⇒ Object
Width of the image in pixels (Fixnum)
Instance Method Details
#as_science ⇒ ImageScience instance
Converts to an ImageScience bitmap and returns it. The ImageScience (github.com/seattlerb/image_science) library must be installed and required before calling this method or a NameError: uninitialized constant ImageScience exception will be raised.
Example
pdf = PDFium::Document.new( "test.pdf" )
page = pdf.pages.first
page.images.each do | image |
image.as_science.cropped_thumbnail 100 do |thumb|
thumb.save "image-#{image.index}-cropped.png"
end
end
207 208 209 210 211 212 213 214 215 |
# File 'ext/pdfium_ext/image.cc', line 207 VALUE image_as_science(VALUE self){ VALUE RBImageScience = rb_const_get(rb_cObject, rb_intern("ImageScience")); FIBITMAP *image = render_to_bitmap(self, FIF_BMP); VALUE instance = Data_Wrap_Struct(RBImageScience, NULL, NULL, image); rb_iv_set(instance, "@file_type", INT2FIX(FIF_BMP)); return instance; } |
#data(: format) ⇒ Object
Used in conjuction with Document.from_memory this can render be used to render a PDF’s pages completely in memory.
Example rendering a PDF to AWS without hitting disk
# Assuming AWS::S3 is already authorized elsewhere
bucket = AWS::S3.new.buckets['my-pdfs']
pdf = PDFium::Document.from_memory bucket.objects['secrets.pdf'].read
pdf.pages.each do | page |
path = "secrets/page-#{page.number}.jpg"
bucket.objects[path].write page.as_image(height: 1000).data(:jpg)
page.images.each do | image |
path = "secrets/page-#{page.number}-image-#{image.index}.png"
bucket.objects[path].write image.data(:png)
end
end
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 305 306 307 |
# File 'ext/pdfium_ext/image.cc', line 271
static VALUE
image_data(VALUE self, VALUE rb_format)
{
VALUE path = RB::to_s(rb_format);
const char* type = StringValuePtr(path);
FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(type);
if((format == FIF_UNKNOWN) || !FreeImage_FIFSupportsWriting(format)) {
rb_raise(rb_eArgError, "Unable to write to a image of that type");
}
FIBITMAP *image = render_to_bitmap(self, format);
FIMEMORY *mem = FreeImage_OpenMemory(); //mem_buffer, buf.st_size);
bool success = FreeImage_SaveToMemory(format, image, mem, 0);
if (!success){
FreeImage_Unload(image);
rb_raise(rb_eArgError, "Unable to save image to memory buffer");
}
long size = FreeImage_TellMemory(mem);
char *buffer = ALLOC_N(char, size);
FreeImage_SeekMemory(mem, 0L, SEEK_SET);
FreeImage_ReadMemory(buffer, size, 1, mem);
FreeImage_Unload(image);
FreeImage_CloseMemory(mem);
VALUE ret = rb_str_new(buffer, size);
xfree(buffer);
return ret;
}
|
#save(file) ⇒ Boolean
Save image to a file
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'ext/pdfium_ext/image.cc', line 227
VALUE
image_save(VALUE self, VALUE rb_file){
// figure out the desired format from the file extension
const char* file = StringValuePtr(rb_file);
FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(file);
if((format == FIF_UNKNOWN) || !FreeImage_FIFSupportsWriting(format)) {
rb_raise(rb_eArgError, "Unable to write to a image of that type");
}
FIBITMAP *image = render_to_bitmap(self, format);
bool success = FreeImage_Save(format, image, file, 0);
// unload the image
FreeImage_Unload(image);
return success ? Qtrue : Qfalse;
}
|