Class: C64PhotoScrap

Inherits:
C64GeosFile show all
Defined in:
lib/native_file_types/c64/C64PhotoScrap.rb

Instance Attribute Summary collapse

Attributes inherited from NativeFileType

#contents

Instance Method Summary collapse

Methods inherited from C64GeosFile

#contents, #header_filename, #icon_background, #icon_foreground, #icon_format, #icon_height, #icon_tag, #icon_width, #info_block, #parent_application, #record_nos, #to_hex_dump, #to_icon

Methods inherited from CbmFile

file_system_file_types

Methods inherited from NativeFileType

#<=>, #==, all_native_file_types, best_fit, code_for_tests, compatability_score, #data_without_header, file_type_matches?, #full_filename, #header_length, is_valid_file_if, load_address, matching_score, native_file_types_possible_on_file_system, non_matching_score, #to_hex_dump, #to_info_dump

Methods included from SubclassTracking

extended

Constructor Details

#initialize(parent_file, filename, contents) ⇒ C64PhotoScrap

Returns a new instance of C64PhotoScrap.



42
43
44
45
46
47
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 42

def initialize(parent_file,filename,contents)
	   @parent_file=parent_file
	   @filename=filename
	@contents=contents
    @meta_data={}
end

Instance Attribute Details

#parent_fileObject (readonly)

Returns the value of attribute parent_file.



15
16
17
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 15

def parent_file
  @parent_file
end

Instance Method Details

#aux_codeObject



38
39
40
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 38

def aux_code
	parent_file.aux_code
end

#file_system_imageObject



26
27
28
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 26

def file_system_image
	parent_file.file_system_image
end

#file_typeObject



30
31
32
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 30

def file_type
	parent_file.file_type
end

#filenameObject



23
24
25
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 23

def filename
	@filename
end

#icon_bitmapObject



20
21
22
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 20

def icon_bitmap
	return parent_file.icon_bitmap
end

#load_addressObject



34
35
36
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 34

def load_address
	parent_file.load_address
end

#meta_dataObject



131
132
133
134
135
136
137
138
139
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 131

def 
	@meta_data["photo_scrap_contents_length"]=contents.length
	@meta_data["photo_scrap_width"]=picture_width
	@meta_data["photo_scrap_height"]=picture_height
	@meta_data["photo_scrap_unpacked_bitmap_length"]=unpacked_bytes[0].length
	@meta_data["photo_scrap_unpacked_colour_table_length"]=unpacked_bytes[1].length

	super
end

#picture_formatObject



56
57
58
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 56

def picture_format
  :png
end

#picture_heightObject



53
54
55
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 53

def picture_height
	(contents[1]+(contents[2]*0x100))
end

#picture_widthObject



50
51
52
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 50

def picture_width
	contents[0]*8
end

#to_pictureObject



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
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 99

def to_picture
  require 'PatchedPNG'
  (bitmap_bytes,colour_bytes)=unpacked_bytes
  width_in_bytes=picture_width/8
  canvas = PNG::Canvas.new picture_width,picture_height, C64::COLOR_LTGRAY
  picture_height.times do |line|

  	line_bytes= bitmap_bytes[width_in_bytes*line,width_in_bytes]
  	width_in_bytes.times do |i|
  		pattern_byte=line_bytes[i]
  		card_number=line/8+i
  		colour_byte=colour_bytes[card_number]
  		colour_byte=0x0F if colour_byte.nil?
  		foreground_colour=C64::COLOR_MAP[colour_byte/0x10]
  		background_colour=C64::COLOR_MAP[colour_byte%0x10]
  		y=line
  		bitmask=0b10000000
  		8.times do |bit|
  			x=(i*8)+bit  			
  			canvas[x,y]=(((pattern_byte & bitmask)==0) ? background_colour : foreground_colour )
  			bitmask=bitmask/2
  			
  		end
  	end
  	canvas
  end
  png = PNG.new canvas
  result=png.raw_bytes
  result
end

#type_descriptionObject



17
18
19
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 17

def type_description
 	"Photo Scrap (#{parent_file.header_filename})"
end

#unpacked_bytesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/native_file_types/c64/C64PhotoScrap.rb', line 60

def unpacked_bytes
	bitmap_length=(picture_width/8)*picture_height
	colour_length=(bitmap_length)/8

	if @unpacked_bytes.nil? then
		@unpacked_bytes=Array.new(bitmap_length+colour_length)
		in_p=3
		out_p=0
		while (in_p<contents.length) do
			count=contents[in_p]
			in_p+=1
			case count
			when 00..127 then
				repeat=count
				b=contents[in_p]
				in_p+=1
				repeat.times do 
					@unpacked_bytes[out_p]=b
					out_p+=1
				end				
			when 128..220 then
				repeat=count-128
				repeat.times do 
					b=contents[in_p]
					in_p+=1
					@unpacked_bytes[out_p]=b
					out_p+=1
				end				
			when 221..225 then
				raise "BIGCOUNT not implemented yet"
			end
		end
#		puts "#{filename} #{in_p} #{out_p} of #{bitmap_length} #{count} #{picture_width}x#{picture_height}"

	end
	return [@unpacked_bytes[0,bitmap_length],@unpacked_bytes[bitmap_length,colour_length]]
	
end