Skip to content

Commit

Permalink
added a way to export png of sprites as shinies
Browse files Browse the repository at this point in the history
  • Loading branch information
kurayamiblackheart committed Dec 27, 2023
1 parent f14ace4 commit 139d269
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Data/Scripts/001_Settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Settings
# The version of your game. It has to adhere to the MAJOR.MINOR.PATCH format.
GAME_VERSION = '6.0.0'
IF_VERSION = "6.0.4"
GAME_VERSION_NUMBER = "0.11.16"
GAME_VERSION_NUMBER = "0.11.17"

POKERADAR_LIGHT_ANIMATION_RED_ID = 17
POKERADAR_LIGHT_ANIMATION_GREEN_ID = 18
Expand Down
203 changes: 203 additions & 0 deletions Data/Scripts/007_Objects and windows/008_AnimatedBitmap.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@

class ByteWriter
def initialize(filename)
@file = File.new(filename, "wb")
end

def <<(*data)
write(*data)
end

def write(*data)
data.each do |e|
if e.is_a?(Array) || e.is_a?(Enumerator)
e.each { |item| write(item) }
elsif e.is_a?(Numeric)
@file.putc e
else
raise "Invalid data for writing.\nData type: #{e.class}\nData: #{e.inspect[0..100]}"
end
end
end

def write_int(int)
self << ByteWriter.to_bytes(int)
end

def close
@file.close
@file = nil
end

def self.to_bytes(int)
return [
(int >> 24) & 0xFF,
(int >> 16) & 0xFF,
(int >> 8) & 0xFF,
int & 0xFF
]
end
end

#===============================================================================
#
#===============================================================================
Expand Down Expand Up @@ -213,6 +254,168 @@ def pbGetGreenChannel
# targetChannel
# end


# def export_bitmap_to_png(file_path)
# require 'zlib'
# # Ensure @bitmap is not nil
# return unless @bitmap

# # Open a file for writing in binary mode
# File.open(file_path, 'wb') do |file|
# # Write PNG signature
# file.write("\x89PNG\r\n\x1a\n".force_encoding('ASCII-8BIT'))

# # Write IHDR chunk (image header)
# ihdr_data = [
# @bitmap.bitmap.width.to_i, # Width
# @bitmap.bitmap.height.to_i, # Height
# 8, # Bit depth (8 bits per channel)
# 6, # Color type (RGBA)
# 0, # Compression method (deflate)
# 0, # Filter method (adaptive)
# 0 # Interlace method (no interlace)
# ].pack('N2C5')
# file.write([ihdr_data.length].pack('N'))
# file.write('IHDR')
# file.write(ihdr_data)
# file.write([Zlib.crc32('IHDR' + ihdr_data)].pack('N'))

# # Write IDAT chunk (image data)
# pixels = @bitmap.bitmap.export_pixels_to_str(0, 0, @bitmap.bitmap.width, @bitmap.bitmap.height, 'RGBA')
# zlib_data = Zlib.deflate(pixels)
# file.write([zlib_data.length].pack('N'))
# file.write('IDAT')
# file.write(zlib_data)
# file.write([Zlib.crc32('IDAT' + zlib_data)].pack('N'))

# # Write IEND chunk (image end)
# file.write([0].pack('N'))
# file.write('IEND')
# file.write([Zlib.crc32('IEND')].pack('N'))
# end
# end

def bitmap_to_png(filename)
return unless @bitmap
require 'zlib'
f = ByteWriter.new(filename)

#============================= Writing header ===============================#
# PNG signature
f << [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
# Header length
f << [0x00, 0x00, 0x00, 0x0D]
# IHDR
headertype = [0x49, 0x48, 0x44, 0x52]
f << headertype

# Width, height, compression, filter, interlacing
headerdata = ByteWriter.to_bytes(@bitmap.width).
concat(ByteWriter.to_bytes(@bitmap.height)).
concat([0x08, 0x06, 0x00, 0x00, 0x00])
f << headerdata

# CRC32 checksum
sum = headertype.concat(headerdata)
f.write_int Zlib::crc32(sum.pack("C*"))

#============================== Writing data ================================#
data = []
for y in 0...@bitmap.height
# Start scanline
data << 0x00 # Filter: None
for x in 0...@bitmap.width
px = @bitmap.bitmap.get_pixel(x, y)
# Write raw RGBA pixels
data << px.red
data << px.green
data << px.blue
data << px.alpha
end
end
# Zlib deflation
smoldata = Zlib::Deflate.deflate(data.pack("C*")).bytes
# data chunk length
f.write_int smoldata.size
# IDAT
f << [0x49, 0x44, 0x41, 0x54]
f << smoldata
# CRC32 checksum
f.write_int Zlib::crc32([0x49, 0x44, 0x41, 0x54].concat(smoldata).pack("C*"))

#============================== End Of File =================================#
# Empty chunk
f << [0x00, 0x00, 0x00, 0x00]
# IEND
f << [0x49, 0x45, 0x4E, 0x44]
# CRC32 checksum
f.write_int Zlib::crc32([0x49, 0x45, 0x4E, 0x44].pack("C*"))
f.close
return nil
end

# Not working?
def export_bitmap_to_png(file_path)
# Ensure @bitmap is not nil
return unless @bitmap
require 'zlib'

width = @bitmap.bitmap.width
height = @bitmap.bitmap.height

# Open a file for writing in binary mode
File.open(file_path, 'wb') do |file|
# Write PNG signature
file.write("\x89PNG\r\n\x1a\n".force_encoding('ASCII-8BIT'))

# Write IHDR chunk (image header)
ihdr_data = [
width, # Width
height, # Height
8, # Bit depth (8 bits per channel)
6, # Color type (RGBA)
0, # Compression method (deflate)
0, # Filter method (adaptive)
0 # Interlace method (no interlace)
].pack('N2C5')
write_chunk(file, 'IHDR', ihdr_data)

# Write IDAT chunk (image data)
pixels = generate_pixel_data(width, height)
zlib_data = Zlib.deflate(pixels)
write_chunk(file, 'IDAT', zlib_data)

# Write IEND chunk (image end)
write_chunk(file, 'IEND', '')
end
end

# Not working?
def generate_pixel_data(width, height)
pixel_data = ''.force_encoding('ASCII-8BIT')
for i in 0...width
for j in 0...height
alpha = @bitmap.bitmap.get_pixel(i, j).alpha
red = @bitmap.bitmap.get_pixel(i, j).red
green = @bitmap.bitmap.get_pixel(i, j).green
blue = @bitmap.bitmap.get_pixel(i, j).blue
# red = canalRed[i * (@bitmap.bitmap.height + 1) + j]
# green = canalGreen[i * (@bitmap.bitmap.height + 1) + j]
# blue = canalBlue[i * (@bitmap.bitmap.height + 1) + j]
pixel_data << [red, green, blue, alpha].pack('C4')
end
end
pixel_data
end

# Not working?
def write_chunk(file, type, data)
file.write([data.length].pack('N'))
file.write(type)
file.write(data)
file.write([Zlib.crc32(type + data)].pack('N'))
end

#KurayX - KURAYX_ABOUT_SHINIES Modified by ChatGPT
def pbGiveFinaleColor(shinyR, shinyG, shinyB, offset, shinyKRS)
# dontmodify = 0
Expand Down
5 changes: 3 additions & 2 deletions Data/Scripts/016_UI/015_UI_Options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1825,11 +1825,12 @@ def pbGetInGameOptions()
["Exported Pokemons will not be deleted.",
"Exported Pokemons will be deleted."]
)
options << EnumOption.new(_INTL("Export Sprite on Export"), [_INTL("On"), _INTL("Off")],
options << EnumOption.new(_INTL("Export Sprite on Export"), [_INTL("On"), _INTL("Off"), _INTL("Shiny")],
proc { $PokemonSystem.nopngexport },
proc { |value| $PokemonSystem.nopngexport = value },
["The .png of the Pokemon will also be exported.",
"The .png (appearence) of the Pokemon will not be exported."]
"The .png (appearence) of the Pokemon will not be exported.",
".png exported as shiny, shiny appearence but pokemon un-shinified."]
)
options << EnumOption.new(_INTL("Import Sprite on Import"), [_INTL("On"), _INTL("Read-Only"), _INTL("Off")],
proc { $PokemonSystem.nopngimport },
Expand Down
30 changes: 29 additions & 1 deletion Data/Scripts/016_UI/017_UI_PokemonStorage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,23 @@ def pbKurayNoEvo(selected, heldpoke)
end
end

def shinyPNGMake(pokemon, pathexport)
if pokemon.kuraycustomfile? == nil
exportbitmap = GameData::Species.sprite_bitmap_from_pokemon(pokemon)
else
if pbResolveBitmap(pokemon.kuraycustomfile?) && !pokemon.egg? && (!$PokemonSystem.kurayindividcustomsprite || $PokemonSystem.kurayindividcustomsprite == 0)
filename = pokemon.kuraycustomfile?
exportbitmap = (filename) ? AnimatedBitmap.new(filename) : nil
if pokemon.shiny?
exportbitmap.pbGiveFinaleColor(pokemon.shinyR?, pokemon.shinyG?, pokemon.shinyB?, pokemon.shinyValue?, pokemon.shinyKRS?)
end
else
exportbitmap = GameData::Species.sprite_bitmap_from_pokemon(pokemon)
end
end
exportbitmap.bitmap_to_png(pathexport)
end

#KurayX
def pbExport(selected, heldpoke, dodelete=0)
deletepkm = false
Expand Down Expand Up @@ -2441,7 +2458,14 @@ def pbExport(selected, heldpoke, dodelete=0)

# Marshal not working anymore !
# File.open(importname + ".pkm", 'wb') { |f| f.write(Marshal.dump(pokemon)) }
if $PokemonSystem.nopngexport != nil && $PokemonSystem.nopngexport == 2
pokemon.shiny = false
end
File.open(importname + ".json", 'w') { |f| f.write(pokemon.to_json) }
if $PokemonSystem.nopngexport != nil && $PokemonSystem.nopngexport == 2
# give shininess back after export
pokemon.shiny = true
end
logic_png(pokemon, importname)
# File.open(importname + ".json3", 'w') { |f| f.write(pokemon.to_s) }
# File.open(importname + ".json2", 'w') { |f| f.write(pokemon.self) }
Expand Down Expand Up @@ -2481,7 +2505,11 @@ def logic_png(pokemon, importname)
copyfile = pokemon.kuraycustomfile?
end
begin
File.copy(copyfile, importname + ".png")
if $PokemonSystem.nopngexport != nil && $PokemonSystem.nopngexport == 2
shinyPNGMake(pokemon, importname + ".png")
else
File.copy(copyfile, importname + ".png")
end
rescue => e
return
end
Expand Down
2 changes: 1 addition & 1 deletion Data/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.16
0.11.17

0 comments on commit 139d269

Please sign in to comment.