Using impack resize to create thumbnail

Hello @StarCrunch,

I am trying to use the impack plugin

  • “resize_rgb” and “resize_rgba”, to resize input image file into a thumbnail size data, and

  • “write.jpg” and “write.png”, to output the thumbnail data into a thumbnail image file.

However, it closes abruptly with this message on the simulator (on Windows) console,
The stdin connection has closed.

At the same time, a file appears in system.DocumentsDirectory with 0 size.

Below is the code I used for testing “jpg” images,

local function create_thumbnail( filename )
	local data, w, h, comp = impack.image.load( filename ) -- jpeg image in system.ResourceDirectory
	local scale = 0.15 -- scale for thumbnail
	local newW = scale * w 
	local newH = scale * h 
	local resized = impack.ops.resize_rgb( data, w, h, newW, newH, comp )
	print( filename, type( data ), string.len( data ), w, h, comp, newW, newH, type( resized ), string.len( resized ) )
	-- output thumbnail image with filename in system.DocumentsDirectory
	local okay, errMsg = impack.write.jpg( filename, newW, newH, comp, resized, { quality = 40 } )
	print( okay, errMsg ) -- 2nd print statement 
	return okay, errMsg 
end 

The first print statement works. But it fails before the second print statement.

Please help to advise if there is anything I have missed out. Thank you.

PS> I am not able to test this on an Android device, as it crashes the simulator (on Windows). Also, I tried using different images to no avail.

Oh, I think I’ve run into this. (A slight change in how I handled file paths, but broken at first.) I probably fixed it locally but forgot to upload the changes. I’ll try to do that later today.

I uploaded a new Windows binary. Let me know if the simulator has issues.

Tried to build Android just now but getting an interesting new error. :smile: (Probably something from the switch to Clang in recent NDKs.) Too tired to hunt for that; I’ll try to get to this and the other targets on Monday.

1 Like

Hello StarCrunch,

Many thanks for your prompt response and action. Here is the result of my quick testing,

  • I have tested my above “create_thumbnail” function with 3 jpeg and 3 png images, on Simulator (on Windows) and an Android device, without any problem now.

  • There is now always “A”, “B”, “C”, “D” printed in each line on the Simulator console which were not there before I updated the plugins. They are not a problem though.

However, I am testing another function below on both Simuator (on Windows) and Android device. This time, the “alpha transparency” of the png images does not seem to work well, as seen below on the LEFT side, using the function “png_resized_image” below. The right side is generated using a generic “bytemap.loadTexture”.

Here is the function I am using, for the LEFT image,

local function png_resized_image( filename ) -- png image in system.ResourceDirectory
	local format = "rgba" -- 4 channels for png image 
	local scale = 0.35
	local posnX = display.safeScreenOriginX + 100
	local posnY = display.safeScreenOriginY + 100
	local data, w, h, comp = impack.image.load( filename, system.ResourceDirectory )
	local newW = scale * w
	local newH = scale * h
	print( filename, type( data ), w, h, string.len( data ), comp )
	local resized = impack.ops.resize_rgba( data, w, h, newW, newH, comp )
	print( filename, type( resized ), w, h, string.len( resized ), newW, newH, comp )
	local map = byteMap.newTexture( { width = newW, height = newH, format = format } )
	print( filename, type( map ), map )
	local image = display.newImage( map.filename, map.baseDir, posnX, posnY )
	map:SetBytes( resized )
	map:invalidate()
	print( filename, image.x, image.y, image.width, image.height, image )
	return image 
end

Please help to advise if there is anything I have missed out. Thank you again.

This might just be our old friend premultiplied alpha not getting applied. If so, I’ve actually added a method to Bytemap to do that after loading (this library needs it). I should be able to upload new builds for it tomorrow.

I’ve also been making some plans to finally sit down and write up / update all kinds of plugin documentation. Bytemap should actually be one of the easier ones. :smiley:

1 Like

Hello StarCrunch,

Thanks again for your prompt response, and the reminder on “premultiplied alpha”.

I got it my test function “png_resized_image” to work normally now, by adding “premultiply” to this statement,

local data, w, h, comp = impack.image.load( filename, system.ResourceDirectory, { premultiply = true } )

It has been tested okay on the same 6 png/jpeg test images on both Simulator (on windows) and an Android device.

Once again. Thank you very much for your help and your plugins.

1 Like

Ah, excellent. I had actually forgotten about that flag, but glad to hear it works. :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.