Class: Bitmap

Inherits:
Object
  • Object
show all
Defined in:
lib/rgss3/bitmap.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Bitmap

Returns a new instance of Bitmap.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rgss3/bitmap.rb', line 7

def initialize(*args)
  case args.size
  when 1
    basename, = args
    filename = RGSS3::RTP.find!(basename, ['', '.png', 'jpg'])
    initialize_with_gosu_image Gosu::Image.new(filename)
  when 2
    initialize_with_rmagick_image Magick::Image.new(*args) { self.background_color = 'none' }
  else
    raise ArgumentError
  end
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



5
6
7
# File 'lib/rgss3/bitmap.rb', line 5

def font
  @font
end

#rectObject (readonly)

Returns the value of attribute rect.



4
5
6
# File 'lib/rgss3/bitmap.rb', line 4

def rect
  @rect
end

Class Method Details

.from_gosu(img) ⇒ Object



223
224
225
226
227
# File 'lib/rgss3/bitmap.rb', line 223

def self.from_gosu(img)
  bitmap = allocate
  bitmap.initialize_with_gosu_image(img)
  bitmap
end

.from_rmagick(img) ⇒ Object



229
230
231
232
233
# File 'lib/rgss3/bitmap.rb', line 229

def self.from_rmagick(img)
  bitmap = allocate
  bitmap.initialize_with_rmagick_image(img)
  bitmap
end

.gosu_to_rmagick(image) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/rgss3/bitmap.rb', line 235

def self.gosu_to_rmagick(image)
  Magick::Image.from_blob(image.to_blob) {
    self.format = "RGBA"
    self.size = "#{image.width}x#{image.height}"
    self.depth = 8
  }.first
end

.pixel_map!(rmagick_image, &block) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/rgss3/bitmap.rb', line 243

def self.pixel_map!(rmagick_image, &block)
  return to_enum(__method__, rmagick_image) unless block
  width = rmagick_image.columns
  height = rmagick_image.rows
  pixels = rmagick_image.get_pixels(0, 0, width, height)
  pixels.map!(&block)
  rmagick_image.store_pixels(0, 0, width, height, pixels)
end

Instance Method Details

#blt(x, y, src_bitmap, src_rect, opacity = 255) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rgss3/bitmap.rb', line 54

def blt(x, y, src_bitmap, src_rect, opacity = 255)
  return if opacity == 0
  src = src_bitmap.gosu_image.subimage(*src_rect)
  if opacity != 255
    src = Bitmap.gosu_to_rmagick(src)
    ratio = opacity / 255.0
    Bitmap.pixel_map!(src) do |pixel|
      pixel.opacity *= ratio
      pixel
    end
  end
  gosu_image.insert(src, x, y)
  set_dirty
end

#blurObject



156
157
# File 'lib/rgss3/bitmap.rb', line 156

def blur
end

#clearObject



130
131
132
# File 'lib/rgss3/bitmap.rb', line 130

def clear
  self.rmagick_image = Magick::Image.new(width, height) { self.background_color = 'none' }
end

#clear_rect(*args) ⇒ Object



134
135
136
# File 'lib/rgss3/bitmap.rb', line 134

def clear_rect(*args)
  fill_rect(*args, Color.new)
end

#disposeObject



36
37
38
39
40
# File 'lib/rgss3/bitmap.rb', line 36

def dispose
  @gosu_image = nil
  @rmagick_image = nil
  @disposed = true
end

#disposed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rgss3/bitmap.rb', line 42

def disposed?
  @disposed
end

#draw_text(*args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
# File 'lib/rgss3/bitmap.rb', line 163

def draw_text(*args)
  case args.size
  when 2, 3
    rect, string, align = args
    x, y, width, height = *rect
  when 5, 6
    x, y, width, height, string, align = args
  else
    raise ArgumentError
  end

  string = string.to_s
  string.gsub!('<', '&lt;')
  string.gsub!('>', '&gt;')
  if @font.bold
    string.prepend("<b>") << "</b>"
  end
  if @font.italic
    string.prepend("<i>") << "</i>"
  end
  text_image = Gosu::Image.from_text(string, @font.size, font: @font.first_existant_name)
  x += (width - text_image.width) * (align || 0) / 2
  y += (height - text_image.height) / 2
  text_image = Bitmap.gosu_to_rmagick(text_image)
  image = text_image.dup
  font_pixel = @font.color.to_pixel
  Bitmap.pixel_map!(image) do |pixel|
    result = font_pixel.dup
    result.opacity = pixel.opacity
    result
  end
  if @font.outline
    font_pixel = @font.out_color.to_pixel
    Bitmap.pixel_map!(text_image) do |pixel|
      result = font_pixel.dup
      result.opacity = pixel.opacity
      result
    end
    image.composite!(text_image, 1, 1, Magick::DstOverCompositeOp)
  end
  # no shadow support for now
  # if @font.shadow
  #   shadow = bigger_image
  #   font_pixel = Magick::Pixel.from_color('rgba(0,0,0,128)')
  #   Bitmap.pixel_map!(shadow) do |pixel|
  #     result = font_pixel.dup
  #     result.opacity = pixel.opacity
  #     result
  #   end
  #   image.composite!(shadow, 0, 0, Magick::DstOverCompositeOp)
  # end
  # @gosu_image.insert(image, x, y)
  self.rmagick_image = rmagick_image.composite!(image, x, y, Magick::OverCompositeOp)
end

#fill_rect(*args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rgss3/bitmap.rb', line 85

def fill_rect(*args)
  case args.size
  when 2, 5
    if args[0].is_a?(Rect)
      rect, color = args
      x, y, width, height = *rect
    else
      x, y, width, height, color = *args
    end
  else
    raise ArgumentError
  end
  img = Magick::Image.new(width, height) { self.background_color = color.to_rmagick_color }
  gosu_image.insert(img, x, y)
  set_dirty
end

#get_pixel(x, y) ⇒ Object



138
139
140
# File 'lib/rgss3/bitmap.rb', line 138

def get_pixel(x, y)
  Color.from_pixel(rmagick_image.pixel_color(x, y))
end

#gosu_imageObject



271
272
273
274
# File 'lib/rgss3/bitmap.rb', line 271

def gosu_image
  check_disposed
  @gosu_image
end

#gradient_fill_rect(*args) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rgss3/bitmap.rb', line 102

def gradient_fill_rect(*args)
  case args.size
  when 3, 4
    rect, start_color, end_color, vertical = args
    x, y, width, height = *rect
  when 6, 7
    x, y, width, height, start_color, end_color, vertical = args
  else
    raise ArgumentError
  end
  start_color = start_color.to_rmagick_color
  end_color = end_color.to_rmagick_color

  if vertical
    x2 = width
    y2 = 0
  else
    x2 = 0
    y2 = height
  end

  fill = Magick::GradientFill.new(0, 0, x2, y2, start_color, end_color)

  img = Magick::Image.new(width, height, fill)
  gosu_image.insert(img, x, y)
  set_dirty
end

#heightObject



50
51
52
# File 'lib/rgss3/bitmap.rb', line 50

def height
  gosu_image.height
end

#hue_change(hue) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/rgss3/bitmap.rb', line 146

def hue_change(hue)
  image = rmagick_image
  Bitmap.pixel_map!(rmagick_image) do |pixel|
    h, *sla = pixel.to_hsla
    h = (h + hue) % 360
    Pixel.from_hsla(h, *sla)
  end
  self.rmagick_image = image
end

#init_other_attrObject



31
32
33
34
# File 'lib/rgss3/bitmap.rb', line 31

def init_other_attr
  @rect = Rect.new(0, 0, gosu_image.width, gosu_image.height)
  @font = Font.new
end

#initialize_with_gosu_image(gosu_image) ⇒ Object



20
21
22
23
24
# File 'lib/rgss3/bitmap.rb', line 20

def initialize_with_gosu_image(gosu_image)
  @gosu_image = gosu_image
  init_other_attr
  set_dirty
end

#initialize_with_rmagick_image(rmagick_image) ⇒ Object



26
27
28
29
# File 'lib/rgss3/bitmap.rb', line 26

def initialize_with_rmagick_image(rmagick_image)
  self.rmagick_image = rmagick_image
  init_other_attr
end

#radial_blur(angle, division) ⇒ Object



159
160
161
# File 'lib/rgss3/bitmap.rb', line 159

def radial_blur(angle, division)
  blur
end

#rmagick_imageObject

If bitmap.rmagick_image is changed, the behaviour is undefined. If you want to change it, call set_dirty or bitmap.rmagick_image = image after the change.



255
256
257
258
259
260
261
262
263
# File 'lib/rgss3/bitmap.rb', line 255

def rmagick_image
  check_disposed
  if @dirty
    @dirty = false
    @rmagick_image = Bitmap.gosu_to_rmagick(@gosu_image)
  else
    @rmagick_image
  end
end

#rmagick_image=(image) ⇒ Object



265
266
267
268
269
# File 'lib/rgss3/bitmap.rb', line 265

def rmagick_image=(image)
  check_disposed
  @rmagick_image = image
  @gosu_image = Gosu::Image.new(@rmagick_image)
end

#set_dirtyObject



276
277
278
# File 'lib/rgss3/bitmap.rb', line 276

def set_dirty
  @dirty = true
end

#set_pixel(x, y, color) ⇒ Object



142
143
144
# File 'lib/rgss3/bitmap.rb', line 142

def set_pixel(x, y, color)
  fill_rect(x, y, 1, 1, color)
end

#stretch_blt(dest_rect, src_bitmap, src_rect, opacity = 255) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rgss3/bitmap.rb', line 69

def stretch_blt(dest_rect, src_bitmap, src_rect, opacity = 255)
  return if opacity == 0
  src = src_bitmap.gosu_image.subimage(*src_rect)
  src = Bitmap.gosu_to_rmagick(gosu_to_rmagick)
  src.resize!(dest_rect.width, dest_rect.height)
  if opacity != 255
    ratio = opacity / 255.0
    Bitmap.pixel_map!(src) do |pixel|
      pixel.opacity *= ratio
      pixel
    end
  end
  gosu_image.insert(src, dest_rect.x, dest_rect.y)
  set_dirty
end

#text_size(string) ⇒ Object



218
219
220
221
# File 'lib/rgss3/bitmap.rb', line 218

def text_size(string)
  f = Gosu::Font.new(@font.size, name: @font.first_existant_name)
  Rect.new(0, 0, f.text_width(string.to_s), f.height)
end

#widthObject



46
47
48
# File 'lib/rgss3/bitmap.rb', line 46

def width
  gosu_image.width
end