Expert Challenge: Get width and height from an image file reading as few bytes as possible.

Talk about anything
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Expert Challenge: Get width and height from an image file reading as few bytes as possible.

07 Aug 2019, 23:04

Introduction

I've written an AutoHotkey function that I use to get the the size of a image in a file -- bitmap, jpg, etc. My function works plenty fast but I've always wondered if there was a way to do it without loading the entire image into memory. For large image files, it could speed up some tasks considerably.

I stumbled across this stackoverflow post that included a few solutions. Paulo Scardine posted some Python code that looks like the most complete way to do it. He later enhanced and converted his solution to Rust which he published here. I've looked at the Rust code. It's small but there more than a few things in the code that I don't understand.

Expert Challenge

Anyone smarter and more patient than me interesting in converting the Rust code into an AutoHotkey function? I'm sure I've got a few internet or karma point laying around that can be used as payment. Or if you already have some AutoHotkey code to do it, I would love to see it.

Challenge Extended!


Edit: The latest (last) release can be found here:
https://www.autohotkey.com/boards/viewtopic.php?p=290795#p290795
Last edited by jballi on 20 Sep 2019, 01:16, edited 1 time in total.
User avatar
jNizM
Posts: 2509
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

08 Aug 2019, 02:08

Hey jballi,

you can take a look at the Exif function from shajul (https://github.com/shajul/Autohotkey/blob/master/COM/Exif/Exif%20File.ahk)

The informations you need are 011A for XResolution and 011B for YResolution.
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

08 Aug 2019, 23:40

OK, I'm making progress. Not much but some.

I've got GIF and PNG working for the most part. The file formats are fairly straightforward although the conversion to big endian caught me off guard there for a second. I still need to find some files with the older file format to finalize this part.

I'm working on JPG now. I'm making some progress but I'm still having difficulty understanding some of the syntax (more on that later). The Exif function from shajul (thanks jNizM) is helpful but it is limited to working on the Exif file format. Many jpg files don't have Exif tags. Also, the script doesn't work on x64. That's another problem altogether.

I'm stuck on this line of Python code:

Code: Select all

input.read(int(struct.unpack(">H", input.read(2))[0])-2)
This instruction reads from the file and converts some of the data but it doesn't do anything with the results. It's only real purpose is the advance the file pointer. Can anyone tell me with some certainty how many bytes are read here? It's at least 6 bytes but I can't identify the exact value.
User avatar
jNizM
Posts: 2509
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

09 Aug 2019, 01:45

If you're at the ahk-discord, you can ask @Sjc1000 to help you understand and translate this python function.
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
just me
Posts: 6517
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

09 Aug 2019, 06:18

Code: Select all

; ----------------------------------------------------------------------------------------------------------------------
GetImgageWidthAndHeight(ImagePath) {
   Static Shell := ComObjCreate("Shell.Application")
   SplitPath, ImagePath, Name , Dir
   If (Dir = "")
      Dir := A_WorkingDir
   If (Folder := Shell.NameSpace(Dir)) && (Item := Folder.ParseName(Name)) {
      W := Folder.GetDetailsOf(Item, 176) ; Width
      H := Folder.GetDetailsOf(Item, 178) ; Height
   }
   Return ((W <> "") && (H <> "")) ? {W: RegExReplace(W, "\D") , H: RegExReplace(H, "\D")} : 0
}
?
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

09 Aug 2019, 08:18

just me wrote:
09 Aug 2019, 06:18
?
I'm not sure if the file system is a reliable source for this information. In your example, I was able to get the function to return some information (was not the right information but it was something) for a jpg file but only if the file had Exif tags. Edit: Now that I think about it, this information is probably only available on NTFS. Something tells me that this wouldn't work on anything else. FAT32, CD, DVD, floppy, etc.

Also, I'm not sure if using a fixed column number is reliable. On my computer, there was some folder name stuff in columns 176 and 178. I was able to find image size stuff in columns 31 and 33. The information was the same for both columns. The format was something like "?3257 x 2246?" where the "?" character is an unprintable character.

Thanks for your feedback. I appreciate it.
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

10 Aug 2019, 00:50

OK, quick update.

I finally got JPEG file format working before I crashed. But who knows what it will look like in the light of day. The JPEG file format will be tough to thoroughly test because the format has changed many (many) times over the years and it supports a large number of tags. This should be fun.

I also got BMP working. I plan to get a few more formats working (TIF, WMF (maybe), and others) before calling it done deal. If anyone is interested in helping me to test it, I will publish a prototype here before I published the function in the Scripts forum.
User avatar
jeeswg
Posts: 6825
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

10 Aug 2019, 01:02

Thanks very much for working on this interesting project.
Two formats I'd be interested in: jxr/wdp and webp. Hopefully they're simple.
(The svg format is also worth mentioning.)
(There's also icon files: (min/)max size.)

I'd be interested in animated gifs: is animated/frame count.
If you've happened to find any interesting links re. those.

Using your script, are you getting significantly better speeds for non-jpegs? How about jpegs?

What were you using before your script?
There's GetDetailsOf. (See just me's script above.)
And the WIA object. E.g.:
Move files of a certain dimension to another folder? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26840&p=126321#p126321
Anything else? Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

10 Aug 2019, 04:39

jeeswg wrote:
10 Aug 2019, 01:02
Thanks very much for working on this interesting project.
Thank you for your interest.
jeeswg wrote:
10 Aug 2019, 01:02
Two formats I'd be interested in: jxr/wdp and webp. Hopefully they're simple.
(The svg format is also worth mentioning.)
(There's also icon files: (min/)max size.)
Re: jxr, wdp, and webp. Obscure file types. I don't have any on my computer. Would need to find some examples in order to identify and test.

Re: svg. XML generated image. Not likely. My image viewer needed to use a shareware DLL just to render the image.

Re: icons. This is tough one because many icons with different sizes are stored in a single file. I use a standard system function to get the "default" icon size.
jeeswg wrote:
10 Aug 2019, 01:02
I'd be interested in animated gifs: is animated/frame count.
If you've happened to find any interesting links re. those.
I've created a class (adapted from code published by other AHK authors) that uses this information so this is definitely get-able information. The method used is 99%+ reliable. Unfortunately there are a very small number of gifs out there where the frame count does not match the actual number of frames in the file. A problem for another topic.
jeeswg wrote:
10 Aug 2019, 01:02
Using your script, are you getting significantly better speeds for non-jpegs? How about jpegs?
It's too soon the in the development process to identify but I suspect for most files, there won't be much improvement simply because of the way the file system accesses files. The whole purpose of this project is to improve performance so yes, this is definitely on the radar.
jeeswg wrote:
10 Aug 2019, 01:02
What were you using before your script?
I have a function that loads the image as icon, cursor, or bitmap and then uses built-in system functions to identify the image size. I'll still need this function because it would impractical to write code for many things, especially images loaded in modules (Ex: MyStuff.dll).
jeeswg wrote:
10 Aug 2019, 01:02
There's GetDetailsOf. (See just me's script above.)
As I mentioned, this is not very reliable.
jeeswg wrote:
10 Aug 2019, 01:02
And the WIA object. E.g.:
Move files of a certain dimension to another folder? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26840&p=126321#p126321
Interesting. I'll take a look see.
jeeswg wrote:
10 Aug 2019, 01:02
Anything else? Thanks.
That's all for now. Thank you for your interest.
User avatar
joedf
Posts: 7285
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

10 Aug 2019, 07:54

Interesting stuff, I hope I’ll get to see some of this code after it’s all working! :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

11 Aug 2019, 06:29

Quick update.

I finally got the TIFF format working. This format was much more complex than I imagined and finding someone who had written code to extract this information was not easy.

I need your help. The function supports TIFF in the little endian format and big endian format but finding TIFF images stored in the big endian format has proven to be very difficult. If anyone has or knows where I can find TIFF files in the big endian format, please let me know. These would be old TIFF files created on Motorola or IBM chips. Stuff created on old Apple Mac for example. The files will have "MM" in the first two bytes of the file.

Thank you for your assistance.
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

12 Aug 2019, 08:09

OK. Here we go.

Attached is the first pass at the function. I've attached a near-ready-to run script that includes the necessary functions. You will need to make a minor change to point to image files on your computer. Please note that there is still some debug code in the function.

Success or failure, I would like to know about it. Here's the rub, if you have a problem, I can't fix it unless I can get my hands on the image file. If you can't part with it, that's OK but if you can, let me know where I can find it or PM me with a copy. If you request it, I will destroy the image after testing.

Thank you for your assistance.
Attachments
GetImageSize Test - Release 1.ahk
(17.79 KiB) Downloaded 26 times
ahkrpa
Posts: 13
Joined: 16 Apr 2019, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

12 Aug 2019, 10:22

jballi,

This is a beautiful thing... It appears to be working flawlessly... Very much appreciated.
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

13 Aug 2019, 15:36

Added limited support for EMF and WMF. See the function documentation for more information. It would be very helpful if someone would test these image types.

As before, I've attached a near-ready-to run script that includes the necessary functions. You will need to make a minor change to point to image files on your computer.

Feedback would be appreciated, especially if you find a problem. Thank you for your assistance.
Attachments
GetImageSize Test - Release 2.ahk
(21.2 KiB) Downloaded 33 times
SomeGuest

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

14 Aug 2019, 08:39

Example 1 works, but Example 2 fails (says BuildGui label is missing, which is odd as it is clearly there)
SomeGuest

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

14 Aug 2019, 10:03

Downloaded it again, works now. Perhaps I deleted a {}() char while pointing it to my images folder. Anyway, false alarm. My bad.
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

15 Aug 2019, 09:39

OK. The accuracy problem for EMF has been fixed. However, the calculation was adjusted to match GDI+. See the function documentation for more information.

As before, I've attached a near-ready-to run script that includes the necessary functions. You will need to make a minor change to point to image files on your computer.

I have very few EMF and WMF files for testing. Some of the EMF files were generated from other file types just to create more EMF files to test. I fear that my test results for EMF and WMF data is skewed and doesn't really represent what really out there. If you have any EMF and/or WMF files, please include those files in your tests.

I'm also still looking for big endian TIFF files. These would be old TIFF files created on Motorola or IBM chips. Stuff created on an old Mac for example. The files will have "MM" in the first two bytes of the file.

Feedback would be appreciated, especially if you find a problem. Thank you for your assistance.
Attachments
GetImageSize Test - Release 3.ahk
(22.95 KiB) Downloaded 24 times
SomeGuest

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

16 Aug 2019, 08:44

Some old(er) MM tiffs here www . simplesystems . org / libtiff/images.html - about 50% work, 50% incorrect
User avatar
jballi
Posts: 579
Joined: 29 Sep 2013, 17:34

Re: Expert Challenge: Get width and height from an image file reading as few bytes as possible.

16 Aug 2019, 11:02

Thanks for the TIFF files SomeGuest. :thumbup: They are very useful. I will publish an update that will include corrections for TIFF files shortly.

Return to “Offtopic”

Who is online

Users browsing this forum: No registered users and 10 guests