Class: Tb::PNMWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/tb/pnm.rb

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ PNMWriter

Returns a new instance of PNMWriter.



172
173
174
175
176
# File 'lib/tb/pnm.rb', line 172

def initialize(io)
  @io = io
  @ary = []
  @fields = {}
end

Instance Method Details

#finishObject



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
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
303
304
305
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
# File 'lib/tb/pnm.rb', line 192

def finish
  undefined_field = ['x', 'y', 'component', 'value'] - @fields.keys
  if !undefined_field.empty?
    raise ArgumentError, "field not defined: #{undefined_field.inspect[1...-1]}"
  end
  pnm_type = nil
  width = height = nil
  comments = []
  max_value = nil
  max_x = max_y = 0
  values = { 0.0 => true, 1.0 => true }
  components = {}
  @ary.each {|rec|
    case rec['component']
    when 'pnm_type'
      pnm_type = rec['value']
    when 'width'
      width = rec['value'].to_i
    when 'height'
      height = rec['value'].to_i
    when 'max'
      max_value = rec['value'].to_i
    when 'comment'
      comments << rec['value']
    when 'R', 'G', 'B', 'V'
      components[rec['component']] = true
      x = rec['x'].to_i
      y = rec['y'].to_i
      max_x = x if max_x < x
      max_y = y if max_y < y
      values[rec['value'].to_f] = true
    end
  }
  if !(components.keys - %w[V]).empty? &&
     !(components.keys - %w[R G B]).empty?
    raise ArgumentError, "inconsistent color component: #{components.keys.sort.inspect[1...-1]}"
  end
  case pnm_type
  when 'P1', 'P4' then raise ArgumentError, "unexpected compoenent for PBM: #{components.keys.sort.inspect[1...-1]}" if !(components.keys - %w[V]).empty?
  when 'P2', 'P5' then raise ArgumentError, "unexpected compoenent for PGM: #{components.keys.sort.inspect[1...-1]}" if !(components.keys - %w[V]).empty?
  when 'P3', 'P6' then raise ArgumentError, "unexpected compoenent for PPM: #{components.keys.sort.inspect[1...-1]}" if !(components.keys - %w[R G B]).empty?
  end
  comments.each {|c|
    if /[\r\n]/ =~ c
      raise ArgumentError, "comment cannot contain a newline: #{c.inspect}"
    end
  }
  if !width
    width = max_x + 1
  end
  if !height
    height = max_y + 1
  end
  if !max_value
    min_interval = 1.0
    values.keys.sort.each_cons(2) {|v1, v2|
      d = v2-v1
      min_interval = d if d < min_interval
    }
    if min_interval < 0.0039 # 1/255 = 0.00392156862745098...
      max_value = 0xffff
    elsif min_interval < 1.0 || !(components.keys & %w[R G B]).empty?
      max_value = 0xff
    else
      max_value = 1
    end
  end
  if pnm_type
    if !pnm_type.kind_of?(String) || /\AP[123456]\z/ !~ pnm_type
      raise ArgumentError, "unexpected PNM type: #{pnm_type.inspect}"
    end
  else
    if (components.keys - ['V']).empty?
      if max_value == 1
        pnm_type = 'P4' # PBM
      else
        pnm_type = 'P5' # PGM
      end
    else
      pnm_type = 'P6' # PPM
    end
  end
  header = "#{pnm_type}\n"
  comments.each {|c| header << '#' << c << "\n" }
  header << "#{width} #{height}\n"
  header << "#{max_value}\n" if /P[2536]/ =~ pnm_type
  if /P[14]/ =~ pnm_type # PBM
    max_value = 1
  end
  bytes_per_component = bytes_per_line = component_fmt = component_template = nil
  case pnm_type
  when 'P1' then bytes_per_component = 1; raster = '1' * (width * height)
  when 'P4' then bytes_per_line = (width + 7) / 8; raster = ["1"*width].pack("B*") * height
  when 'P2' then bytes_per_component = max_value.to_s.length+1; component_fmt = "%#{bytes_per_component}d"; raster = (component_fmt % 0) * (width * height)
  when 'P5' then bytes_per_component, component_template = max_value < 0x100 ? [1, 'C'] : [2, 'n']; raster = "\0" * (bytes_per_component * width * height)
  when 'P3' then bytes_per_component = max_value.to_s.length+1; component_fmt = "%#{bytes_per_component}d"; raster = (component_fmt % 0) * (3 * width * height)
  when 'P6' then bytes_per_component, component_template = max_value < 0x100 ? [1, 'C'] : [2, 'n']; raster = "\0" * (bytes_per_component * 3 * width * height)
  else
    raise
  end
  raster.force_encoding("ASCII-8BIT") if raster.respond_to? :force_encoding
  @ary.each {|rec|
    c = rec['component']
    next if /\A[RGBV]\z/ !~ c
    x = rec['x'].to_i
    y = rec['y'].to_i
    next if x < 0 || width <= x
    next if y < 0 || height <= y
    v = rec['value'].to_f
    if v < 0
      v = 0
    elsif 1 < v
      v = 1
    end
    case pnm_type
    when 'P1'
      v = v < 0.5 ? '1' : '0'
      raster[y * width + x] = v
    when 'P4'
      xhi, xlo = x.divmod(8)
      i = y * bytes_per_line + xhi
      byte = raster[i].ord
      if v < 0.5
        byte |= 0x80 >> xlo
      else
        byte &= 0xff7f >> xlo
      end
      raster[i] = [byte].pack("C")
    when 'P2'
      v = (v * max_value).round
      raster[(y * width + x) * bytes_per_component, bytes_per_component] = component_fmt % v
    when 'P5'
      v = (v * max_value).round
      raster[(y * width + x) * bytes_per_component, bytes_per_component] = [v].pack(component_template)
    when 'P3'
      v = (v * max_value).round
      i = (y * width + x) * 3
      if c == 'G' then i += 1
      elsif c == 'B' then i += 2
      end
      raster[i * bytes_per_component, bytes_per_component] = component_fmt % v
    when 'P6'
      v = (v * max_value).round
      i = (y * width + x) * 3
      if c == 'G' then i += 1
      elsif c == 'B' then i += 2
      end
      raster[i * bytes_per_component, bytes_per_component] = [v].pack(component_template)
    else
      raise
    end
  }
  if pnm_type == 'P1'
    raster.gsub!(/[01]{#{width}}/, "\\&\n")
    if 70 < width
      raster.gsub!(/[01]{70}/, "\\&\n")
    end
    raster << "\n" if /\n\z/ !~ raster
  elsif /P[23]/ =~ pnm_type
    components_per_line = /P2/ =~ pnm_type ? width : 3 * width
    raster.gsub!(/  +/, ' ')
    raster.gsub!(/( \d+){#{components_per_line}}/, "\\&\n")
    raster.gsub!(/(\A|\n) +/, '\1')
    raster.gsub!(/.{71,}\n/) {
      $&.gsub(/(.{1,69})[ \n]/, "\\1\n")
    }
    raster << "\n" if /\n\z/ !~ raster
  end
  @io << (header+raster)
end

#header_generator=(gen) ⇒ Object



182
183
# File 'lib/tb/pnm.rb', line 182

def header_generator=(gen)
end

#header_required?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/tb/pnm.rb', line 178

def header_required?
  false
end

#put_hash(pairs) ⇒ Object



185
186
187
188
189
190
# File 'lib/tb/pnm.rb', line 185

def put_hash(pairs)
  @ary << pairs
  pairs.each {|k, v|
    @fields[k] = true
  }
end