Class: ZPNG::ScanLine
- Inherits:
-
Object
- Object
- ZPNG::ScanLine
- Defined in:
- lib/zpng/scan_line.rb,
lib/zpng/scan_line/mixins.rb
Defined Under Namespace
Modules: Mixins
Constant Summary collapse
- FILTER_NONE =
0
- FILTER_SUB =
1
- FILTER_UP =
2
- FILTER_AVERAGE =
3
- FILTER_PAETH =
4
- VALID_FILTERS =
FILTER_NONE..FILTER_PAETH
Instance Attribute Summary collapse
-
#bpp ⇒ Object
Returns the value of attribute bpp.
- #decoded_bytes ⇒ Object
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#idx ⇒ Object
Returns the value of attribute idx.
-
#image ⇒ Object
Returns the value of attribute image.
-
#offset ⇒ Object
Returns the value of attribute offset.
Instance Method Summary collapse
- #[](x) ⇒ Object
- #[]=(x, color) ⇒ Object
-
#bad? ⇒ Boolean
ScanLine is BAD if it has no filter.
- #crop!(x, w) ⇒ Object
- #decode! ⇒ Object
- #each_pixel ⇒ Object
- #export ⇒ Object
-
#initialize(image, idx, params = {}) ⇒ ScanLine
constructor
A new instance of ScanLine.
- #inspect ⇒ Object
- #pixels ⇒ Object
- #raw_data ⇒ Object
- #raw_data=(data) ⇒ Object
-
#raw_set(offset, value) ⇒ Object
set raw byte data at specified offset modifies @image, resets @decoded_bytes.
-
#size ⇒ Object
total scanline size in bytes, INCLUDING leading ‘filter’ byte.
- #to_ascii(*args) ⇒ Object
- #valid_filter? ⇒ Boolean
-
#width ⇒ Object
scanline width in pixels.
Constructor Details
#initialize(image, idx, params = {}) ⇒ ScanLine
Returns a new instance of ScanLine.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/zpng/scan_line.rb', line 15 def initialize image, idx, params={} @image,@idx = image,idx @bpp = image.hdr.bpp raise "[!] zero bpp" if @bpp == 0 # Bytes Per Pixel, if bpp = 8, 16, 24, 32 # NULL otherwise @BPP = (@bpp%8 == 0) && (@bpp>>3) if @image.new? @size = params[:size] @decoded_bytes = params[:decoded_bytes] || "\x00" * (size-1) @filter = FILTER_NONE @offset = params[:offset] || idx*size else @offset = if image.interlaced? image.adam7.scanline_offset(idx) else idx*size end if @filter = image.imagedata[@offset] @filter = @filter.ord elsif @image.verbose >= -1 STDERR.puts "[!] #{self.class}: ##@idx: no data at pos 0, scanline dropped".red end end end |
Instance Attribute Details
#bpp ⇒ Object
Returns the value of attribute bpp.
12 13 14 |
# File 'lib/zpng/scan_line.rb', line 12 def bpp @bpp end |
#decoded_bytes ⇒ Object
243 244 245 246 247 248 249 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 |
# File 'lib/zpng/scan_line.rb', line 243 def decoded_bytes @decoded_bytes ||= begin raw = @image.imagedata[@offset+1, size-1] if raw.size < size-1 # handle broken images when data ends in the middle of scanline raw += "\x00" * (size-1-raw.size) end # TODO: check if converting raw to array would give any speedup # number of bytes per complete pixel, rounding up to one bpp1 = (@bpp/8.0).ceil case @filter when FILTER_NONE # 0 s = raw when FILTER_SUB # 1 s = "\x00" * (size-1) s[0,bpp1] = raw[0,bpp1] # TODO: optimize bpp1.upto(size-2) do |i| s.setbyte(i, raw.getbyte(i) + s.getbyte(i-bpp1)) end when FILTER_UP # 2 s = "\x00" * (size-1) 0.upto(size-2) do |i| s.setbyte(i, raw.getbyte(i) + prev_scanline_byte(i)) end when FILTER_AVERAGE # 3 s = "\x00" * (size-1) 0.upto(bpp1-1) do |i| s.setbyte(i, raw.getbyte(i) + prev_scanline_byte(i)/2) end bpp1.upto(size-2) do |i| s.setbyte(i, raw.getbyte(i) + (s.getbyte(i-bpp1) + prev_scanline_byte(i))/2) end when FILTER_PAETH # 4 s = "\x00" * (size-1) 0.upto(bpp1-1) do |i| s.setbyte(i, raw.getbyte(i) + prev_scanline_byte(i)) end bpp1.upto(size-2) do |i| s.setbyte(i, raw.getbyte(i) + paeth_predictor(s.getbyte(i-bpp1), prev_scanline_byte(i), prev_scanline_byte(i-bpp1)) ) end else STDERR.puts "[!] #{self.class}: ##@idx: invalid filter #@filter, assuming FILTER_NONE".red s = raw end s end end |
#filter ⇒ Object
Returns the value of attribute filter.
12 13 14 |
# File 'lib/zpng/scan_line.rb', line 12 def filter @filter end |
#idx ⇒ Object
Returns the value of attribute idx.
12 13 14 |
# File 'lib/zpng/scan_line.rb', line 12 def idx @idx end |
#image ⇒ Object
Returns the value of attribute image.
12 13 14 |
# File 'lib/zpng/scan_line.rb', line 12 def image @image end |
#offset ⇒ Object
Returns the value of attribute offset.
12 13 14 |
# File 'lib/zpng/scan_line.rb', line 12 def offset @offset end |
Instance Method Details
#[](x) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 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 |
# File 'lib/zpng/scan_line.rb', line 149 def [] x raw = if @BPP # 8, 16, 24, 32, 48 bits per pixel decoded_bytes[x*@BPP, @BPP] else # 1, 2 or 4 bits per pixel decoded_bytes[x*@bpp/8] end case image.hdr.color when COLOR_INDEXED # ALLOWED_DEPTHS: 1, 2, 4, 8 mask = 2**@bpp-1 shift = 8-(x%(8/@bpp)+1)*@bpp raise "invalid shift #{shift}" if shift < 0 || shift > 7 idx = (raw.ord >> shift) & mask if image.trns # transparency from tRNS chunk # For color type 3 (indexed color), the tRNS chunk contains a series of one-byte alpha values, # corresponding to entries in the PLTE chunk: # # Alpha for palette index 0: 1 byte # Alpha for palette index 1: 1 byte # ... # color = image.palette[idx].dup if color.alpha = image.trns.data[idx] # if it's not NULL - convert it from char to int, # otherwise it means fully opaque color, as well as NULL alpha in ZPNG::Color color.alpha = color.alpha.ord end return color else # no transparency return image.palette[idx] end when COLOR_GRAYSCALE # ALLOWED_DEPTHS: 1, 2, 4, 8, 16 c = if @bpp == 16 raw.unpack('n')[0] else mask = 2**@bpp-1 shift = 8-(x%(8/@bpp)+1)*@bpp raise "invalid shift #{shift}" if shift < 0 || shift > 7 (raw.ord >> shift) & mask end color = Color.from_grayscale(c, :depth => @bpp) # only in this color mode depth == bpp color.alpha = image._alpha_color(color) return color when COLOR_RGB # ALLOWED_DEPTHS: 8, 16 color = case @bpp when 24 # RGB 8 bits per sample = 24bpp if image.trns extend Mixins::RGB24_TRNS else extend Mixins::RGB24 end return self[x] when 48 # RGB 16 bits per sample = 48bpp Color.new(*raw.unpack('n3'), :depth => 16) else raise "COLOR_RGB unexpected bpp #@bpp" end color.alpha = image._alpha_color(color) return color when COLOR_GRAY_ALPHA # ALLOWED_DEPTHS: 8, 16 case @bpp when 16 # 8-bit grayscale + 8-bit alpha return Color.from_grayscale(*raw.unpack('C2')) when 32 # 16-bit grayscale + 16-bit alpha return Color.from_grayscale(*raw.unpack('n2'), :depth => 16) else raise "COLOR_GRAY_ALPHA unexpected bpp #@bpp" end when COLOR_RGBA # ALLOWED_DEPTHS: 8, 16 case @bpp when 32 # RGBA 8-bit/sample extend Mixins::RGBA32 return self[x] when 64 # RGBA 16-bit/sample return Color.new(*raw.unpack('n4'), :depth => 16 ) else raise "COLOR_RGBA unexpected bpp #@bpp" end else raise "unexpected color mode #{image.hdr.color}" end # case img.hdr.color end |
#[]=(x, color) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/zpng/scan_line.rb', line 90 def []= x, color case image.hdr.color when COLOR_INDEXED # ALLOWED_DEPTHS: 1, 2, 4, 8 color_idx = image.palette.find_or_add(color) raise "no color #{color.inspect} in palette" unless color_idx mask = 2**@bpp-1 shift = 8-(x%(8/@bpp)+1)*@bpp raise "invalid shift #{shift}" if shift < 0 || shift > 7 pos = x*@bpp/8 b = decoded_bytes.getbyte(pos) b = (b & (0xff-(mask<<shift))) | ((color_idx & mask) << shift) decoded_bytes.setbyte(pos, b) # TODO: transparency in TRNS when COLOR_GRAYSCALE # ALLOWED_DEPTHS: 1, 2, 4, 8, 16 raw = color.to_depth(@bpp).to_grayscale pos = x*@bpp/8 if @bpp == 16 decoded_bytes[pos,2] = [raw].pack('n') else mask = 2**@bpp-1 shift = 8-(x%(8/@bpp)+1)*@bpp raise "invalid shift #{shift}" if shift < 0 || shift > 7 b = decoded_bytes[pos].ord b = (b & (0xff-(mask<<shift))) | ((raw & mask) << shift) decoded_bytes[pos] = b.chr end # TODO: transparency in TRNS when COLOR_RGB # ALLOWED_DEPTHS: 8, 16 case @bpp when 24; decoded_bytes[x*3,3] = color.to_depth(8).to_a.pack('C3') when 48; decoded_bytes[x*6,6] = color.to_depth(16).to_a.pack('n3') else raise "unexpected bpp #@bpp" end # TODO: transparency in TRNS when COLOR_GRAY_ALPHA # ALLOWED_DEPTHS: 8, 16 case @bpp when 16; decoded_bytes[x*2,2] = color.to_depth(8).to_gray_alpha.pack('C2') when 32; decoded_bytes[x*4,4] = color.to_depth(16).to_gray_alpha.pack('n2') else raise "unexpected bpp #@bpp" end when COLOR_RGBA # ALLOWED_DEPTHS: 8, 16 case @bpp when 32; decoded_bytes[x*4,4] = color.to_depth(8).to_a.pack('C4') when 64; decoded_bytes[x*8,8] = color.to_depth(16).to_a.pack('n4') else raise "unexpected bpp #@bpp" end else raise "unexpected color mode #{image.hdr.color}" end # case image.hdr.color end |
#bad? ⇒ Boolean
ScanLine is BAD if it has no filter
45 46 47 |
# File 'lib/zpng/scan_line.rb', line 45 def bad? !@filter end |
#crop!(x, w) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/zpng/scan_line.rb', line 356 def crop! x, w @size = nil # unmemoize self size b/c it's changed after crop if @BPP # great, crop is byte-aligned! :) decoded_bytes[0,x*@BPP] = '' decoded_bytes[w*@BPP..-1] = '' else # oh, no we have to shift bits in a whole line :( case @bpp when 1,2,4 cut_bits_head = @bpp*x if cut_bits_head > 8 # cut whole head bytes decoded_bytes[0,cut_bits_head/8] = '' end cut_bits_head %= 8 if cut_bits_head > 0 # bit-shift all remaining bytes (w*@bpp/8.0).ceil.times do |i| decoded_bytes[i] = (( (decoded_bytes[i].ord<<cut_bits_head) | (decoded_bytes[i+1].ord>>(8-cut_bits_head)) ) & 0xff).chr end end new_width_bits = w*@bpp diff = decoded_bytes.size*8 - new_width_bits raise if diff < 0 if diff > 8 # cut whole tail bytes decoded_bytes[(new_width_bits/8.0).ceil..-1] = '' end diff %= 8 if diff > 0 # zero tail bits of last byte decoded_bytes[-1] = (decoded_bytes[-1].ord & (0xff-(2**diff)+1)).chr end else raise "unexpected bpp=#@bpp" end end end |
#decode! ⇒ Object
304 305 306 307 |
# File 'lib/zpng/scan_line.rb', line 304 def decode! decoded_bytes true end |
#each_pixel ⇒ Object
412 413 414 415 416 |
# File 'lib/zpng/scan_line.rb', line 412 def each_pixel width.times do |i| yield self[i], i end end |
#export ⇒ Object
401 402 403 404 405 406 407 408 409 410 |
# File 'lib/zpng/scan_line.rb', line 401 def export # we export in FILTER_NONE mode # [] is for preventing spare tail bytes that can break scanlines sequence if @decoded_bytes FILTER_NONE.chr + decoded_bytes[0,size-1] else # scanline was never decoded => export it as-is to save memory & CPU @image.imagedata[@offset, size] end end |
#inspect ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/zpng/scan_line.rb', line 74 def inspect if image.interlaced? "#<ZPNG::ScanLine idx=%-2d offset=%-3d width=%-2d size=%-2d bpp=%d filter=%d>" % [idx, offset, width, size, bpp, filter] else "#<ZPNG::ScanLine idx=%-2d offset=%-3d size=%-2d bpp=%d filter=%d>" % [idx, offset, size, bpp, filter] end end |
#pixels ⇒ Object
418 419 420 |
# File 'lib/zpng/scan_line.rb', line 418 def pixels Pixels.new(self) end |
#raw_data ⇒ Object
309 310 311 |
# File 'lib/zpng/scan_line.rb', line 309 def raw_data @offset ? @image.imagedata[@offset, size] : '' end |
#raw_data=(data) ⇒ Object
313 314 315 316 317 318 319 |
# File 'lib/zpng/scan_line.rb', line 313 def raw_data= data if data.size == size @image.imagedata[@offset, size] = data else raise Exception, "raw data size must be #{size}, got #{data.size}" end end |
#raw_set(offset, value) ⇒ Object
set raw byte data at specified offset modifies @image, resets @decoded_bytes
323 324 325 326 327 328 329 330 |
# File 'lib/zpng/scan_line.rb', line 323 def raw_set offset, value @decoded_bytes = nil value = value.ord if value.is_a?(String) if offset == 0 @filter = value # XXX possible bugs with Singleton Modules end @image.imagedata.setbyte(@offset+offset, value) end |
#size ⇒ Object
total scanline size in bytes, INCLUDING leading ‘filter’ byte
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/zpng/scan_line.rb', line 54 def size @size ||= begin if @BPP width*@BPP+1 else (width*@bpp/8.0+1).ceil end end end |
#to_ascii(*args) ⇒ Object
84 85 86 87 88 |
# File 'lib/zpng/scan_line.rb', line 84 def to_ascii *args @image.width.times.map do |i| self[i].to_ascii(*args) end.join end |
#valid_filter? ⇒ Boolean
49 50 51 |
# File 'lib/zpng/scan_line.rb', line 49 def valid_filter? VALID_FILTERS.include?(@filter) end |
#width ⇒ Object
scanline width in pixels
66 67 68 69 70 71 72 |
# File 'lib/zpng/scan_line.rb', line 66 def width if image.interlaced? image.adam7.scanline_width(idx) else image.width end end |