How can I operate on a size_t and end up with a CGFloat? - ios

To determine the ratio at which to scale an image, I'm using the following code (borrowed from Trevor Harmon's UIImage+Resize):
CGFloat horizontalRatio = 600 / CGImageGetWidth(imageRef);
CGFloat verticalRatio = 600 / CGImageGetHeight(imageRef);
CGFloat ratio = MAX(horizontalRatio, verticalRatio);
The 600 represents the maximum size I want for the scaled image. CGImageGetWidth and CGImageGetHeight return a size_t which, according to ARC, evaluate to an unsigned long on the iPhone platform (iOS 5).
The problem with the present code is that ratio always evaluates 0.0000.
The width and height of imageRef are actually w=768, h=780, so the ratio should be MAX(0.781, 0.769) = 0.78. How do I this?
P.S. When I used the code above for UIImage's initWithCGImage:scale:orientation: I found that scale works differently than I'd expected: passing in a ratio of 0.78 enlarged the image. Dividing the width or height by the desired size (as in CGImageGetWidth(imageRef) /600, etc.) fixed the problem.

You need one value to be a float to do proper division. Integer division always truncates the floating point numbers. The easiest solution is to turn your numbers into float literals.
CGFloat horizontalRatio = 600.0f / CGImageGetWidth(imageRef);
CGFloat verticalRatio = 600.0f / CGImageGetHeight(imageRef);
CGFloat ratio = MAX(horizontalRatio, verticalRatio);

Related

Which is the right way to calculate number of points of an image?

View.Bounds gave me values of (0.0, 0.0, 375.0, 667.0) to help calculate view dimensions. And CGImage, size of bitmap gave me pixel count of 490 - pixel width and 751 - pixel height. I don't understand why I get UIView bounds content size less than CGImage pixel width and height when scale factor gave me value of 2. But how can I calculate number of points from the dimensions ? How can I take out 375.0 and 667.0 and calculate ? Below code helped me to get view dimensions and scale factor.
let ourImage: UIImage? = imageView.image
let viewBounds = view.bounds
print("\(viewBounds)")
var scale: CGFloat = UIScreen.mainScreen().scale
print("\(scale)")
And this is the code I worked to receive pixel height and width of 490 * 751.
public init?(image: UIImage) {
guard let cgImage = image.CGImage else { return nil }
// Redraw image for correct pixel format
let colorSpace = CGColorSpaceCreateDeviceRGB()
var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue
width = Int(image.size.width)
height = Int(image.size.height)
.... }
Or can I use (pixelWidth * 2 + pixelHeight * 2) as to calculate number of points ? I need to calculate or fix number of points (n) to substitute in further equation of image segmentation using active contour method.
An image view does not resize to match the image property you set to it. Nor does the image you set resize to match the image view's frame.
Instead, the image view presents a representation of that image scaled to whatever size matches the image view (based on the rules you pick, aspect fit, aspect fill, scale to fill, others, etc).
So the actual size of the image is exactly whatever is returns from the image's size property.
To get the actual image's actual width, use:
image.size.width
Likewise, to get the actual image's actual height, use:
image.size.height

Calculate real width based on picture, knowing distance

I know the distance between the camera and the object
I know the type of camera used
I know the width in pixel on the picture
Can I figure the real life width of the object?
you have to get the angle of camera. For example, iphone 5s is 61.4 in vertical and 48.0 horizontal. call it alpha.
then you calculate the width of object by this way:
viewWidth = distance * tan(alpha / 2) * 2;
objWidth = viewWidth * (imageWidth / screenWidth)

Inconsistent "Zoom level" for CA-tiled layer class

So I am using CATiled layer to create my own floor plan. My floor plan works just fine on older devices like the 4, 4s, 5, 5s and 6. However, when I run my program on the 6 plus, my arithmetic for zoom level is off. Here's why: When I call my draw rect method, I get different tile width's based on the device. When I call my images, I call them in the sequence of ZoomLevel_COL_ROW.image . My zoom level is multiples of 2. So every time you pinch to zoom, my ZoomLevel is called in order: 2, 4, 8, 16 and 32. My problem I am having is I can't figure out how to divide the tiles width by 512 to get me the appropriate zoom level. So on a 6plus, my initial tile width when the view loads is 85.53. So 512/85.3 gets me 6 - which is not apart of my sequence. However, on a 5s I get a tile width of 128. So 512/128 gets me 4 - which is apart of my sequence, however skips level 2. So I am having a tough time determining what would be a consistent arithmetic to get all zoom level sequences every time you pinch to zoom. Recall that my sequence is 2, 4, 8, 16,32. Hopefully I am clear, and everyone understands. Thanks!
- (void)drawRect:(CGRect)rect
{
// NSLog(#"drawrect %#", NSStringFromCGRect(rect));
zoomLevel = (512 / (CGRectGetWidth(rect)));
int size = 512;
int zoom = 1;
while (size > rect.size.width) {
size /= 2;
zoom ++;
}
NSLog(#"Zoomlevel %d", zoomLevel);
CGSize tileSize = rect.size;
int firstCol = floorf(CGRectGetMinX(rect) / tileSize.width);
int lastCol = floorf((CGRectGetMaxX(rect)-1) / tileSize.width);
int firstRow = floorf(CGRectGetMinY(rect) / tileSize.height);
int lastRow = floorf((CGRectGetMaxY(rect)-1) / tileSize.height);
for (int row = firstRow; row <= lastRow; row++) {
for (int col = firstCol; col <= lastCol; col++) {
UIImage *tile = [self tileAtCol:col row:row];
float width = tileSize.width;
float height = tileSize.height;
if (tile.size.width != tile.size.height) {
// make sure that smaller tiles aren't stretched
float widthRatio = tile.size.width / 512;
float heightRatio = tile.size.height / 512;
width *= widthRatio;
height *= heightRatio;
}
CGRect tileRect = CGRectMake(tileSize.width * col,
tileSize.height * row,
width, height);
tileRect = CGRectIntersection(self.bounds, tileRect);
[tile drawInRect:tileRect];
}
}
}
Figured out why. It had to do with scale Factor! Because iPhone 6 plus has different resolution. Check out the code below:
scaleFactor = [UIScreen mainScreen].scale;
float width = rect.size.width;
width *= scaleFactor;
zoomLevel = (512/ width);

the following lua code does not make sense

I have the following bit of lua code. I am not sure what it is doing
width = aspectRatio > 1.5 and 320 or math.ceil( 480 / aspectRatio )
is it a short circuit?
It is indeed a short circuit. It's equivalence in c would be:
width = aspectRatio > 1.5 ? 320 : math.ceil( 480 / aspectRatio )
Or in english: if the aspect ratio is greater than 1.5, set the width to 320, otherwise set the width to the smallest integral value that is greater than or equal to the division of 480 and the aspect ratio.
Reference
http://www.lua.org/pil/3.3.html

Finding the largest 16:9 rectangle within another rectangle

I am working on this Lua script and I need to be able to find the largest 16:9 rectangle within another rectangle that doesn't have a specific aspect ratio. So can you tell me how I can do that? You don't have to write Lua - pseudocode works too.
Thanks!
This I have tried and can confirm that won't work on lower ratio outer rects.
if wOut > hOut then
wIn = wOut
hIn = (wIn / 16) *9
else
hIn = hOut
wIn = (hIn / 9) * 16
end
heightCount = originalHeight / 9;
widthCount = originalWidth / 16;
if (heightCount == 0 || widthCount == 0)
throw "No 16/9 rectangle";
recCount = min(heightCount, widthCount);
targetHeight = recCount * 9;
targetWidth = recCount * 16;
So far, any rectangle with left = 0..(originalWidth - targetWidth) and top = 0..(originalHeight - targetHeight) and width = targetWidth and height = targetHeight should satisfy your requirements.
Well, your new rectangle can be described as:
h = w / (16/9)
w = h * (16/9)
Your new rectangle should then be based on the width of the outer rectangle, so:
h = w0 / (16/9)
w = w0
Depending on how Lua works with numbers, you might want to make sure it is using real division as opposed to integer division - last time I looked was 2001, and my memory is deteriorating faster than coffee gets cold, but I seem to remember all numbers being floats anyway...

Resources