crop a image not draw part of it?

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.
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

crop a image not draw part of it?

Post by Shadowblitz16 »

is there a way to crop a part of a image and get a texture/image out of it?
I don't want to just draw it but actually get a object out of it so I can store it in a array.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: crop a image not draw part of it?

Post by pgimeno »

I strongly recommend against that, as there are usually alternatives, but if you only have an Image object and you really really need to crop it, the only way I can think of is to draw the image to a canvas of the final size and then use Canvas:newImageData. However, if you load the image first to an ImageData object, then you can use the Data:getString method and slice the string with string.sub to form the cropped image, which you would then load into a new ImageData object. Or even better, you can use Data:getPointer and do the data manipulation with FFI, which I expect would be faster. Maybe someone can think of something simpler; I have that terrible feeling that I'm missing something obvious and it must be simpler.

So, let's go with the alternatives. Rather than storing a cropped image in the array, why don't you store both the original image and the quad that crops it, and use both when you want to draw the cropped image?
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: crop a image not draw part of it?

Post by Sir_Silver »

Uh, wouldn't a quad be perfect for this?

https://love2d.org/wiki/love.graphics.newQuad

Edit: didn't see above post already mentioned quads, nonetheless, I think that's what you want to use.
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: crop a image not draw part of it?

Post by Shadowblitz16 »

@pgimeno so love doesn't have a simple image.crop function? thats dumb.
@Sir_Silver like I said I don't want to draw it I need to crop it and store it into a tile array

I guess I could post a suggestion, but I am starting to feel that every game engine/framework that is easy to use ends up being sandboxed or made by devs that think that people should download libraries or code the function themselves.

its kinda of annoying
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: crop a image not draw part of it?

Post by ivan »

Shadowblitz16 wrote: Wed Apr 25, 2018 3:11 am @pgimeno so love doesn't have a simple image.crop function? thats dumb.
It's not efficient to duplicate an image in memory, considering that you can draw cropped images using quads.
It's smart that such a function doesn't exist since it encourages people to use better options (like quads).
Shadowblitz16 wrote: Wed Apr 25, 2018 3:11 am @Sir_Silver like I said I don't want to draw it I need to crop it and store it into a tile array
Ideally, you should crop your images ahead of time using an image editor.
Shadowblitz16 wrote: Wed Apr 25, 2018 3:11 am I guess I could post a suggestion, but I am starting to feel that every game engine/framework that is easy to use ends up being sandboxed or made by devs that think that people should download libraries or code the function themselves.
Love2d provides the most common functionality that you need.
Of course you still have to write your own functions, what did you expect. :)
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: crop a image not draw part of it?

Post by grump »

Code: Select all

local img = love.image.newImageData('image.jpg')
local cropped = love.image.newImageData(cropWidth, cropHeight)
cropped:paste(img, 0, 0, cropX, cropY, cropWidth, cropHeight)
Last edited by grump on Wed Apr 25, 2018 7:16 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: crop a image not draw part of it?

Post by zorg »

Shadowblitz16 wrote: Wed Apr 25, 2018 3:11 am
I guess I could post a suggestion, but I am starting to feel that every game engine/framework that is easy to use ends up being sandboxed or made by devs that think that people should download libraries or code the function themselves.

its kinda of annoying
You know, you could just go the easy route and learn to develop for consoles that worked in the way you want, like the NES.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: crop a image not draw part of it?

Post by Sir_Silver »

What grump posted looks like it's exactly what you want!

Worth mentioning that, after you do the code grump posted which was

Code: Select all

local img = love.image.newImageData('image.jpg')
local cropped = love.image.newImageData(cropWidth, cropHeight)
cropped:paste(img, 0, 0, cropX, cropY, cropWidth, cropHeight)
If you decide you want to draw it, you'll need to create a drawable from that imageData so you'll need to do something like

Code: Select all

cropped = love.graphics.newImage(cropped)
Now you can just store this image in your array!
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: crop a image not draw part of it?

Post by Nelvin »

If you want to have at least a chance for good performance (as long as you use version >= 11 with automatic batching) you could use something like this

Code: Select all

local function crop( image, srcX, srcY, width, height )
    return {
        quad = love.graphics.newQuad( srcX, srcY, width, height, image:getDimensions() ),
        image = image,
    }
end

love.graphics.drawTile = function( tile, x, y )
    love.graphics.draw( tile.quad, tile.image, x, y )
end
Create some tiles in whatever way fits your project/assets

Code: Select all

local myTileArray = {}
myTileArray[1] = crop( myPreviouslyLoadedImage, 0, 0, 32, 32 )
and later on

Code: Select all

love.graphics.drawTile( myTileArray[myTileIdx], myDrawXPos, myDrawYPos)
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: crop a image not draw part of it?

Post by Shadowblitz16 »

@grump thankyou this is very helpful
@zorg I want to make a GB Zelda editor thats why I don't want to learn console programming
@Nelvin ya I would like it to be as fast as possible but I want to store tiles individually so idk
Post Reply

Who is online

Users browsing this forum: No registered users and 200 guests