Texture reduction

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
lune
Prole
Posts: 9
Joined: Thu Sep 22, 2016 10:03 am
Location: France

Texture reduction

Post by lune »

Hello,

To continue a very interesting discussion on #love, i present you my attempt of texture reduction.
Thanks to Amadiro, Slime, and z0rg , i've made a lot of progress.

The goal is to reduce a canvas to a more exploitable thing, to do color quantization.

It can be done without shader, but i've been told than with a shader, the results will be more accurate.
I present you my version without custom shader (it uses the default pixel shader of love).

It's just copying pixels of the "big" canvas into a smaller canvas (1/8th of the big canvas).
For keeping the code small, i did not implemented the "color counting" part, but it's just a question of extracting data from canvas with "Canvas:newImageData" and then read the string with getString on the image data.

Here is the code, it's working but it can be done better, so i'm waiting for your comments ;)

Code: Select all

local win_dim = 512
local canvas -- will be 512x512, same as win dimensions
local mini_canvas -- 1/8 of the canvas
local shader_mini_canvas

function love.draw()
  mini_canvas:renderTo(
  function()
    love.graphics.draw(canvas, 0,0, 0, 1/8, 1/8)
  end
  )

  love.graphics.draw(mini_canvas)
end


function love.load()
  love.window.setMode(win_dim, win_dim) --a square window
  -- now create the big canvas (same dimensions than window)
  canvas = love.graphics.newCanvas(win_dim, win_dim)
  canvas:renderTo(
    function()
      local rec_dim = win_dim/2
      local colors={ {255,0,0},{0,255,0},{0,0,255}, {255,255,255} }
      local pos={ 0,0, rec_dim,0, 0,rec_dim, rec_dim,rec_dim }
      for i=1,#colors do
        love.graphics.setColor(colors[i])
        love.graphics.rectangle("fill", pos[i*2-1], pos[i*2], rec_dim,rec_dim)
      end
    end
  )

  mini_canvas = love.graphics.newCanvas(win_dim/8,win_dim/8)
end

function love.keypressed(key, scancode, isrepeated)
  if key == "escape" then love.event.quit() end
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 42 guests