Forum

> > CS2D > Scripts > Get baseline jpg/jpeg image width and height
Forums overviewCS2D overview Scripts overviewLog in to reply

English Get baseline jpg/jpeg image width and height

4 replies
To the start Previous 1 Next To the start

old Get baseline jpg/jpeg image width and height

MikuAuahDark
User Off Offline

Quote
This time i want to create a script again. I want to create a custom circle function which has radius depending by image size.
1
radius=math.max(width,height)/2

PNG and BMP image always returns correct value, but i'm getting 1 problem

My script doesn't return correct value when i'm getting baseline jpeg image size(maybe progressive too, but i test it in image 223x223 and it returns correct value). I test it in image which has 320x240 and 800x600 and it always return 120 and 160

Is there a way to get the correct jpg image width & height?

GetImageWidthHeight >


And yes it's Exif baseline jpg

old Re: Get baseline jpg/jpeg image width and height

Livia
User Off Offline

Quote
I made this quickly and it worked for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function getImageDimensions(filename)
    local file = io.open(filename, "rb")

    file:seek("set")
    if file:read(2) ~= "\255\216" then
        print("not jpg")
        return
    end

    local width = 0
    local height = 0

    -- while not EOF
    while file:read(1)~=nil do
        file:seek("cur", -1)

        -- search for next marker
        while file:read(1) ~= "\xFF" do end
        file:seek("cur", -1)

        -- throw out extra FFs
        while file:read(1) == "\xFF" do end
        file:seek("cur", -1)

        -- read marker
        local byte = file:read(1):byte()

        -- is SOF marker
        if byte >= 192 and byte <= 207 and byte ~= 196 and byte ~= 200 and byte ~= 204 then
            file:seek("cur", 3)

            height = file:read(1):byte()*256+file:read(1):byte()
            width = file:read(1):byte()*256+file:read(1):byte()
            break

        else
            -- read offset and substract 2 bytes we already read
            local offset = file:read(1):byte()*256+file:read(1):byte() - 2

            file:seek("cur", offset)
        end
    end
    file:close()

    return {width, height}
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview