Aspect ratios of images: finding out dimensions

Guiserver, GTK-server, OpenGL, PostScript,
HTML 5, MIDI, IDE
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Aspect ratios of images: finding out dimensions

Post by cormullion »

How would you scale an image up to fill the available window without distortion. I can see that if you can find out in advance the dimensions of an image, you can find out the larger of the height/width and then calculate a scale factor that scales that up to the container.

If there's no other way to get proportional scaling (and how often do you want non-proportional scaling?) what's a good way to get the dimensions of an image? For my personal MacOS X use, I can use 'sips'. But what's a good x-platform way?

Best from a user's point of view would be a flag saying 'scale to fit, while maintaining aspect ratio', but I suspect that's not easy to implement?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You could go here: http://www.wotsit.org/ find out about the file format and read the image size from the header of the file. Some usage of 'open' 'read-buffer' and 'unpack' may be required, but it will probably be fast and short.

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks. It looks difficult. Until I can understand how to do it, I'll be using a sips call to get the dimensions:

Code: Select all

(set 'image-data (exec (format "sips -g pixelHeight -g pixelWidth '%s' " fname)))
(set 'image-height (int (nth 1 (parse (image-data 1))) 0 10))
(set 'image-width  (int (nth 1 (parse (image-data 2))) 0 10))

(if (> image-width image-height)
	; landscape format
	(set 'scale-factor (div  width image-width)) ; container width
	(set 'scale-factor (div  height image-height))) 

(set 'w (floor (mul image-width scale-factor)))
(set 'h (floor (mul image-height scale-factor)))

(gs:draw-image 'I fname 0 0 w h)))))
although that would make my code MacOS specific...

Locked