ICO parsing of 32-bit icons now possible, added default_icon image for missing app icon

This commit is contained in:
2022-02-06 08:35:38 -06:00
parent af95acbd4d
commit aef62b423f
5 changed files with 53 additions and 18 deletions

View File

@@ -2,6 +2,8 @@ require "stringio"
class W3DHub
class ICO
PNG_IDENT = "\211PNG\r\n\032\n".force_encoding("ASCII-8BIT").freeze
IconDirectory = Struct.new(:reserved, :type, :image_count)
IconDirectoryEntity = Struct.new(
@@ -50,6 +52,9 @@ class W3DHub
read_u32,
read_u32
)
@images.last.width = 256 if @images.last.width == 0
@images.last.height = 256 if @images.last.height == 0
end
end
@@ -65,26 +70,56 @@ class W3DHub
@file.read(4).unpack1("V")
end
def to_rgba32_blob(image)
@file.pos = image.image_offset
buf = @file.read(image.image_size)
File.write("TEMP.bmp", buf)
end
def select_pngs
@images.select do |image|
image_png?(image)
end
end
def image_png?(image)
@file.pos = image.image_offset
buf = @file.read(8).unpack1("a*")
buf == "\211PNG\r\n\032\n".force_encoding("ASCII-8BIT")
end
buf == PNG_IDENT
end
def select_bmps
@images.select do |image|
image_bmp?(image)
end
end
def image_bmp?(image)
!image_png?(image)
end
def to_rgba32_blob(image)
@file.pos = image.image_offset
buf = StringIO.new(@file.read(image.image_size))
raise NotImplementedError "Cannot parse png based icons!" unless image_bmp?(image)
raise NotImplementedError "Cannot parse #{image.bit_depth}" unless image.bit_depth == 32
blob = "".force_encoding("ASCII-8BIT")
image.height.times do |y|
image.width.times do |x|
buf.pos = ((image.height - y) * image.width + x) * 4
blue = buf.read(1)
green = buf.read(1)
red = buf.read(1)
alpha = buf.read(1)
blob << red
blob << green
blob << blue
blob << alpha
end
end
Gosu::Image.from_blob(image.width, image.height, blob)
end
end
end
data = W3DHub::ICO.new(file: "/home/cyberarm/Downloads/icos/ar.ico")
pp data.select_pngs.size, data.images.size
# data.to_rgba32_blob(data.images.first)

View File

@@ -28,7 +28,7 @@ class W3DHub
background task.application.color
flow(width: 0.70, height: 1.0) do
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png"
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png"
@application_image = image image_path, height: 1.0
stack(margin_left: 8, width: 0.75) do

View File

@@ -40,7 +40,7 @@ class W3DHub
image "#{GAME_ROOT_PATH}/media/ui_icons/return.png", width: 1.0, color: Gosu::Color::GRAY if Store.application_manager.updateable?(game.id, game.channels.first.id)
image "#{GAME_ROOT_PATH}/media/ui_icons/import.png", width: 0.5, color: 0x88_ffffff unless Store.application_manager.installed?(game.id, game.channels.first.id)
end
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{game.id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{game.id}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png"
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{game.id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{game.id}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png"
image image_path, height: 48, color: Store.application_manager.installed?(game.id, game.channels.first.id) ? 0xff_ffffff : 0x88_ffffff
end

View File

@@ -26,7 +26,7 @@ class W3DHub
@filters.each do |app_id, enabled|
app = Store.applications.games.find { |a| a.id == app_id.to_s }
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{app_id}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png"
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{app_id}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png"
image image_path, tip: "#{app.name}", height: 1.0,
border_thickness_bottom: 1, border_color_bottom: 0x00_000000,
@@ -361,7 +361,7 @@ class W3DHub
end
def game_icon(server)
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png"
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png"
if server.status.password
@server_locked_icons[server.game] ||= Gosu.render(96, 96) do

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB