Class: Rszr::Image

Inherits:
Object
  • Object
show all
Extended by:
Identification
Includes:
Buffered, Transformations, Orientation
Defined in:
lib/rszr/image.rb,
ext/rszr/image.c

Defined Under Namespace

Modules: Transformations

Constant Summary collapse

GRAVITIES =
[true, :center, :n, :nw, :w, :sw, :s, :se, :e, :ne].freeze
BLENDING_MODES =
%i[copy add subtract reshade].freeze

Constants included from Orientation

Orientation::ROTATIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transformations

#blend, #blend!, #blur, #blur!, #brighten, #brighten!, #contrast, #contrast!, #crop, #crop!, #fill, #fill!, #filter, #flip, #flop, #gamma, #gamma!, #rectangle, #rectangle!, #resize, #resize!, #rotate, #rotate!, #sharpen, #sharpen!, #turn, #turn!

Methods included from Orientation

included

Methods included from Buffered

included

Constructor Details

#initialize(width, height, alpha: false, background: nil) ⇒ Image

Returns a new instance of Image.

Raises:

  • (ArgumentError)


183
184
185
186
187
188
189
190
# File 'lib/rszr/image.rb', line 183

def initialize(width, height, alpha: false, background: nil)
  raise ArgumentError, 'illegal image dimensions' if width < 1 || width > 32766 || height < 1 || height > 32766
  raise ArgumentError, 'background must respond to to_fill' if background && !(background.respond_to?(:to_fill))
  _initialize(width, height).tap do |image|
    image.alpha = alpha
    image.fill!(background) if background
  end
end

Class Method Details

.load(path, autorotate: Rszr.autorotate, **opts) ⇒ Object Also known as: open

Raises:



12
13
14
15
16
17
18
# File 'lib/rszr/image.rb', line 12

def load(path, autorotate: Rszr.autorotate, **opts)
  path = path.to_s
  raise FileNotFound unless File.exist?(path)
  image = _load(path, opts[:immediately])
  autorotate(image, path) if autorotate
  image
end

.load_data(data, autorotate: Rszr.autorotate, **opts) ⇒ Object

Raises:



21
22
23
24
25
26
# File 'lib/rszr/image.rb', line 21

def load_data(data, autorotate: Rszr.autorotate, **opts)
  raise LoadError, 'Unknown format' unless format = identify(data)
  with_tempfile(format, data) do |file|
    load(file.path, autorotate: autorotate, **opts.merge(immediately: true))
  end
end

Instance Method Details

#[](x, y) ⇒ Object



48
49
50
51
52
# File 'lib/rszr/image.rb', line 48

def [](x, y)
  if x >= 0 && x <= width - 1 && y >= 0 && y <= height - 1
    Color::RGBA.new(*_pixel(x, y))
  end
end

#alphaObject



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/rszr/image.c', line 141

static VALUE rszr_image_alpha_get(VALUE self)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);

  imlib_context_set_image(handle->image);
  if (imlib_image_has_alpha()) {
    return Qtrue;
  }

  return Qfalse;
}

#alpha=(rb_alpha) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'ext/rszr/image.c', line 155

static VALUE rszr_image_alpha_set(VALUE self, VALUE rb_alpha)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);

  imlib_context_set_image(handle->image);
  imlib_image_set_has_alpha(RTEST(rb_alpha) ? 1 : 0);

  return Qnil;
}

#alpha?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rszr/image.rb', line 44

def alpha?
  !!alpha
end

#dimensionsObject



30
31
32
# File 'lib/rszr/image.rb', line 30

def dimensions
  [width, height]
end

#dupObject

static VALUE rszr_image_get_quality(VALUE self) {

rszr_image_handle * handle;
int quality;

Data_Get_Struct(self, rszr_image_handle, handle);

imlib_context_set_image(handle->image);
quality = imlib_image_get_attached_value("quality");

if (quality) {
  return INT2NUM(quality);
} else {
  return Qnil;
}

}

static VALUE rszr_image_set_quality(VALUE self, VALUE rb_quality) {

rszr_image_handle * handle;
int quality;

Check_Type(rb_quality, T_FIXNUM);
quality = FIX2INT(rb_quality);
if (quality <= 0) {
  rb_raise(rb_eArgError, "quality must be >= 0");
  return Qnil;
}

Data_Get_Struct(self, rszr_image_handle, handle);

imlib_context_set_image(handle->image);
imlib_image_attach_data_value("quality", NULL, quality, NULL);

return INT2NUM(quality);

}



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'ext/rszr/image.c', line 260

static VALUE rszr_image_dup(VALUE self)
{
  rszr_image_handle * handle;
  rszr_image_handle * cloned_handle;
  Imlib_Image cloned_image;
  VALUE oClonedImage;
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  cloned_image = imlib_clone_image();
  
  if (!cloned_image) {
    rb_raise(eRszrTransformationError, "error cloning image");
    return Qnil;
  }
  
  oClonedImage = rszr_image_s_allocate(cImage);
  Data_Get_Struct(oClonedImage, rszr_image_handle, cloned_handle);
  cloned_handle->image = cloned_image;

  return oClonedImage;
}

#filter!(rb_filter_expr) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'ext/rszr/image.c', line 360

static VALUE rszr_image_filter_bang(VALUE self, VALUE rb_filter_expr)
{
  rszr_image_handle * handle;
  char * filter_expr;
  
  filter_expr = StringValueCStr(rb_filter_expr);
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  imlib_apply_filter(filter_expr);
  
  return self;
}

#flip!Object



311
312
313
314
315
316
317
318
319
320
321
# File 'ext/rszr/image.c', line 311

static VALUE rszr_image_flip_bang(VALUE self)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  imlib_image_flip_vertical();

  return self;
}

#flop!Object



298
299
300
301
302
303
304
305
306
307
308
# File 'ext/rszr/image.c', line 298

static VALUE rszr_image_flop_bang(VALUE self)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  imlib_image_flip_horizontal();

  return self;
}

#formatObject



34
35
36
37
# File 'lib/rszr/image.rb', line 34

def format
  fmt = _format
  fmt == 'jpg' ? 'jpeg' : fmt
end

#format=(fmt) ⇒ Object



39
40
41
42
# File 'lib/rszr/image.rb', line 39

def format=(fmt)
  fmt = fmt.to_s if fmt.is_a?(Symbol)
  self._format = fmt
end

#heightObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/rszr/image.c', line 182

static VALUE rszr_image_height(VALUE self)
{
  rszr_image_handle * handle;
  int height;
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  height = imlib_image_get_height();
  
  return INT2NUM(height);
}

#inspectObject



54
55
56
57
58
# File 'lib/rszr/image.rb', line 54

def inspect
  fmt = format
  fmt = " #{fmt.upcase}" if fmt
  "#<#{self.class.name}:0x#{object_id.to_s(16)} #{width}x#{height}x#{alpha? ? 32 : 24}#{fmt}>"
end

#save(path, format: nil, quality: nil, interlace: false) ⇒ Object

Raises:

  • (ArgumentError)


192
193
194
195
196
197
# File 'lib/rszr/image.rb', line 192

def save(path, format: nil, quality: nil, interlace: false)
  format ||= format_from_filename(path) || self.format || 'jpg'
  raise ArgumentError, "invalid quality #{quality.inspect}" if quality && !(0..100).cover?(quality)
  ensure_path_is_writable(path)
  _save(path.to_s, format.to_s, quality, interlace)
end

#save_data(format: nil, quality: nil) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/rszr/image.rb', line 199

def save_data(format: nil, quality: nil)
  format ||= self.format || 'jpg'
  with_tempfile(format) do |file|
    save(file.path, format: format, quality: quality)
    file.rewind
    file.read
  end
end

#widthObject

Instance methods



168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/rszr/image.c', line 168

static VALUE rszr_image_width(VALUE self)
{
  rszr_image_handle * handle;
  int width;
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  width = imlib_image_get_width();
  
  return INT2NUM(width);
}