Scaling down images?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Scaling down images?

Post by OmarShehata »

So let's say you've got your game with big HD-sized images and assets. Now that takes up a lot of RAM. And the game freezes during loading for some people because they run out of RAM!

If the images were smaller, that uses way less RAM (of course at the expense of looking worse, but that won't be noticable on smaller resolutions for example), so I want to do this automatically. What's the best way to do this? Render the image to a canvas, take the image data, save it to disk? What if the user doesn't have canvas support? Better to just ship the game with different sizes of the images we want optimized?
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Scaling down images?

Post by Ranguna259 »

Maybe a pixel shader on a canvas or on a huge atlas ?
That'd make it have the same height and width thought.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Scaling down images?

Post by micha »

Here are some other ideas (not filtered by usefulness)
  • If the assets are made in a vector graphics program, then ship the original source files of the assets and render them to pixel graphics locally. That will probably not work via LÖVE and it will take a lot of time. (But it will fully avoid any redundancy, you'd have if you shipped different resolution version of the assets)
  • You can also manually scale the assets by setting pixels in imageData. That wouldn't require any canvases then, but you'd have to loop over all pixels in the original assets in Lua, which might be slow.
  • Another way to avoid the canvas is to use the main screen to resize images, then make screenshots and clear the screen again, before actual drawing. I think transparency gets lost on the way, though.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Scaling down images?

Post by slime »

OmarShehata wrote:Better to just ship the game with different sizes of the images we want optimized?
That's probably the best way if you try out DXT texture compression and the lossiness of the compression is too noticeable.

But you should try DXT texture compression first. :)

That type of texture compression will compress your image files on disk, and they will also stay compressed when loaded into RAM and into VRAM.
That means the images will load very quickly (because there's no decompression into raw pixel data), they'll take up much less space in RAM, they'll take up much less space in VRAM, and they'll give better performance when drawing, because the GPU can keep more of the image in its fast-access cache during rendering since it doesn't take up as much space.

The two downsides are that you'll have to use a tool to convert your images from PNG or whatever format to DXT-compressed DDS (DDS is the container file format for compressed textures like these), and the compression is lossy which might (but might not) be noticeable ingame - most AAA games use this type of compression for most of their art assets.

I listed some tools which can convert textures here: viewtopic.php?f=4&t=77364&p=163755#p163754

How much RAM space will you save? That depends on which DXT format you use.
DXT5 is good for textures with transparency, and only takes up 8 bits per pixel instead of the normal 32 (they take up 1/4 the space in RAM/VRAM.)
DXT1 is only good for textures with no transparency, and it only takes up 4 bits per pixel instead of 32 (so that's 1/8th the space of a regular texture.)

In LÖVE, If you call love.graphics.newImage with a filename as an argument, it will automatically detect whether the file is a DDS with a compressed DXT image inside it, and load it directly without decompressing. Otherwise you can use [wiki]love.image.newCompressedData[/wiki] and then call love.graphics.newImage(compresseddata).
The [wiki]CompressedFormat[/wiki] wiki page also details the formats LÖVE can use.


If you scale down an image a lot when drawing (regardless of whether it's compressed or not), you might notice some unwanted aliasing / grainy effects. Mipmapping can help with that, e.g. Image:setMipmapFilter("nearest", 1).
Another benefit to using DXT-compressed textures is that the tools which compress them usually automatically generate mipmaps for them and store them directly in the same file. That means the mipmaps will probably be higher quality than if you enabled them for regular images (since the tool doesn't have to be as quick about creating them as a GPU driver would be), and they'll load faster too.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Scaling down images?

Post by OmarShehata »

Oh wow! Thanks for the awesome post Slime! :D

Just tried DXT, it's like magic!

(Also the scaling down and saving of the images shaves off a lot, especially useful for if you're playing the game on a lower resolution, there won't be any difference in quality)
Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests