forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
September 10, 2010, 05:47:03 AM
.
News: 06/10/2009 - New Features For Forum.
Friendly URLs, twitter for profile, and video site URLs will auto-display the video.
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read May 07, 2008, 12:58:28 PM #0
osgeld

Get the resolution of a PNG file before attempting to load it

WHAT
-- this script reads the header info off of a png image file and reports back the image resolution

-- this script is my first use of my studying bytecoded chunk data structures...
- (such as images and 3d models)

-- altho this (very) simple utility wasn't what i had in mind for a first "look at me" release...
-- its knowledge was requested in the evilmana forums, and i cant turn down info i know to a
-- fellow scripter (or 2)  thats not our way now is it

-- ok enugh BS lets get down with the sickness

PNG file structure
the first 8 bytes in the file are for format recognition, that way if you chopped off the *.png extension your display program still knows its a png
after that is the IHDR chunk, it has to be the first data chunk in the file this contains all the good suff like
    Width:              4 bytes
    Height:             4 bytes
    Bit depth:          1 byte
    Color type:         1 byte
    Compression method: 1 byte
    Filter method:      1 byte
    Interlace method:   1 byte 

in my example file it looks like this


we want to know the width and height of the image, we see from above that they are saved in 4 byte integers so we count the first 8 charaters in the file past where it says IHDR


in the script i will open the file using io.open, read the first handful of characters find the IHDR chunk tag and read the first 8 characters after that

using string.byte we get the numerical value of the character and convert from 4 byte integers to decimal humon form


updated code here
Code:
updated


sources

PNG (Portable Network Graphics) Specification, Version 1.2
Lua 5 string functions
« Last Edit: September 20, 2008, 11:14:57 AM by osgeld »
Offline  
Read May 07, 2008, 02:12:48 PM #1
emcp

Re: Get the resolution of a PNG file before attempting to load it

why not just load it and then get the size that way
Image:width()*Image:height()

you are opening it anyway

that is correct isnt it just multiply the width by the height to get the resolution

Quote
-- this script is my first use of my studying bytecoded chunk data structures...
- (such as images and 3d models)
also good luck

lol you said dollars then you said pennies, its cents in america, your from the uk, why not just say pounds?
« Last Edit: May 07, 2008, 02:22:14 PM by emcp »

Offline  
Read May 07, 2008, 02:35:43 PM #2
osgeld

Re: Get the resolution of a PNG file before attempting to load it

cause if you attempt to load a png thats over 512x512 it crashes luaplayer to an error state where it stays untill you power down the psp, this is so you can check before (ie dumbass user loaded images)

and im not loading the entire image, just the first 32 bytes

cause those little copper things that infest our pockets are called pennies, not cents, cents can be a penny a dime a quarter.... a penny = 1 cent, a quarter = 25 cents

and its not luck you can find explanations to most 3d file formats, its all about finding chunks and decoding data, im already loading verticies out of a 3ds file into luaplayer
« Last Edit: May 07, 2008, 02:46:45 PM by osgeld »
Offline  
Read May 07, 2008, 04:05:15 PM #3
emcp

Re: Get the resolution of a PNG file before attempting to load it

cause if you attempt to load a png thats over 512x512 it crashes luaplayer to an error state where it stays untill you power down the psp, this is so you can check before (ie dumbass user loaded images)

and im not loading the entire image, just the first 32 bytes

cause those little copper things that infest our pockets are called pennies, not cents, cents can be a penny a dime a quarter.... a penny = 1 cent, a quarter = 25 cents

and its not luck you can find explanations to most 3d file formats, its all about finding chunks and decoding data, im already loading verticies out of a 3ds file into luaplayer

calm down, lol
also good point about the user loaded images


Offline  
Read May 10, 2008, 06:21:30 PM #4
TyPR124

Re: Get the resolution of a PNG file before attempting to load it

Hey osgeld, just to make sure you know, if the way your converting the HEX to decimal is what you have in that first post:

Quote
(0 * 1024) + (128 * 512) + (57 * 256) + 16

The first byte does represent 1's, and the second does represent 256's, but the third should be 256^2, and fourth 256^3

The reason being is that the first digit always represents (B being the base) b^0 (which equals 1 no matter what b is), and the second digit represents b^1, third digit represents b^2, etc

So, if you had (in the example you gave) 0 128 57 16 as a base 256 (2 hex digits), that would give you
0*(256^3) + 128*(256^2) + 57*(256^1) + 16*(256^0) which is 8403216 (if my calculater is right lol)


Code:
-- The Univers's script

createParticles()
createStarts()
createPlanets()
createGalaxys()
createOthers()

while LIVING_PEOPLE do --we are here ^.^
 Earth.health = Earth.health - 1
 
 if Earth.health <= 0 then
  Earth.Destroy()
  LIVING_PEOPLE = false
 end
end
Offline  
Read May 10, 2008, 07:03:16 PM #5
osgeld

Re: Get the resolution of a PNG file before attempting to load it

ugh your right  Waaahhhhh

ill update the script tommarow Smile
« Last Edit: May 10, 2008, 07:24:42 PM by osgeld »
Offline  
Read May 10, 2008, 07:35:40 PM #6
TyPR124

Re: Get the resolution of a PNG file before attempting to load it

EDIT: nvm, this post was a waste lol since you beat me to it...
« Last Edit: May 10, 2008, 07:37:47 PM by TyPR124 »

Code:
-- The Univers's script

createParticles()
createStarts()
createPlanets()
createGalaxys()
createOthers()

while LIVING_PEOPLE do --we are here ^.^
 Earth.health = Earth.health - 1
 
 if Earth.health <= 0 then
  Earth.Destroy()
  LIVING_PEOPLE = false
 end
end
Offline  
Read September 20, 2008, 11:10:07 AM #7
osgeld

Re: Get the resolution of a PNG file before attempting to load it

finally fixed, and made into a function

Code:
function png_size(image_name)

local png_hdr_dta = io.open (image_name):read(32)
-- open and read the file in ascii mode
-- only read the first 32 charaters, we really dont need to even go this far
-- but it seemed like a easy safe number



local ihdr_st = string.find(png_hdr_dta, "IHDR") + 4
-- find where the IHDR chunk starts
-- sting.find returns 2 numbers
-- i just added 4 (since were trying to find then skip over the phrase IHDR)

local width  = 0 -- output
local height = 0 -- output

width = width +
string.byte(png_hdr_dta, 3 + ihdr_st) +
string.byte(png_hdr_dta, 2 + ihdr_st) * 256 +
string.byte(png_hdr_dta, 1 + ihdr_st) * 65536 +
string.byte(png_hdr_dta, 0 + ihdr_st) * 16777216

height = height +
string.byte(png_hdr_dta, 7 + ihdr_st) +
string.byte(png_hdr_dta, 6 + ihdr_st) * 256 +
string.byte(png_hdr_dta, 5 + ihdr_st) * 65536 +
string.byte(png_hdr_dta, 4 + ihdr_st) * 16777216

return width, height
end


-- demo
color = Color.new(255,255,255)
x, y  = png_size("untitled.png")

while not Controls.read():start() do
screen:clear()

if x > 512  or y > 512 then
screen:print(0,100, "too big", color)
else
screen:print(0,100, "good to go", color)
end

screen:print(0,120, "X = " .. x .. " | Y = " .. y, color)

screen.flip()
end
Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re