Aspect ratios of images: finding out dimensions

Started by cormullion, July 23, 2007, 07:41:16 AM

Previous topic - Next topic

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

#1
You could go here: http://www.wotsit.org/">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

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


(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...