What could be the best way to use transparent JPEGs?
Why JPEGs? If you have large images, photographies and such, PNG files need a long time to load and need much space on the disk. So JPEGs are the way to go. But! They have no transparency layer.
The idea is, to combine the benefits of both in some way.
First I tried to create a program that reads the transparency data out of an 24+8 Bit PNG and creates a JPEG and a PNG out of that. Maybe I don't need the memblocks Pixel by Pixel, maybe I could use it also with some Mask-Commands, don't know.
It is a prototype.
So the result has to be more tiny in space and better looking in visual terms than what you could get out of the PNG optimised directly from tinypng or such programs. And in some way, this does.
// Project: TransparentJPEG
// Created: 2019-01-31
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "TransparentJPEG" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//img = LoadImage("Image-furniture_chassis_frog_house_tinypng.png")
img = LoadImage("Image-furniture_chassis_frog_house.png")
mem = CreateMemblockFromImage(img)
offset_read = 12
offset_write = 12
For y = 0 to GetImageHeight(img)-1
For x = 0 to GetImageWidth(img)-1
Byte = GetMemblockByte(mem, offset_read + 3) // read Alpha
For RGB_ = 0 to 2
SetMemblockByte(mem, offset_write + RGB_, Byte) // write RGB gray
Next
SetMemblockByte(mem, offset_write + 3, 255) // write full Alpha, no transparency
inc offset_write, 4
inc offset_read, 4
Next
Next
img_save = CreateImageFromMemblock(mem)
SaveImage(img_save,"Image-furniture_chassis_frog_house-AGK.png")
SaveImage(img,"Image-furniture_chassis_frog_house-AGK.jpg")
spr = LoadSprite("Test_BG_2018_tex.jpg")
SetSpriteScale(spr,1,1.5)
Img = LoadImage("Image-furniture_chassis_frog_house-AGK.jpg")
ImgMask = LoadImage("Image-furniture_chassis_frog_house-AGK.png")
SetImageMask(Img,ImgMask, 4,1,0,0)
spr_=CreateSprite(Img)
SetImageTransparentColor(spr_,0,0,0)
SetSpriteTransparency( spr_, 1 )
do
Print( ScreenFPS() )
Sync()
loop
So the question is: Is that the best way? Could it be in some way faster? Or Would it better, to slice big pictures in smaller parts, create more sprites and have only transparency on the edges? So maybe we could have a JPEG in the middle, but at the corner, there are some PNGs with transparency saved in it or so.
And how much would the performance suffer from this? Maybe there is a program, that does, what I tried to do. Maybe it does it much better
For what would that be a use case? For animations with real people, or for unique visuals not only created out of tiles.