Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capability to resize Images #119

Open
EstebanBorai opened this issue Jun 3, 2024 · 3 comments
Open

Capability to resize Images #119

EstebanBorai opened this issue Jun 3, 2024 · 3 comments

Comments

@EstebanBorai
Copy link
Contributor

It would be nice to have support to set a fixed height and width for an image.

Im using cacao::image::Image instance with Image::with_data to set an image from bytes into
a view, but I don't seem to find a way to set image dimensions.

I have found the following snippet on StackOverflow on how could it be done:

- (NSImage *)imageResize:(NSImage*)anImage newSize:(NSSize)newSize {
    NSImage *sourceImage = anImage;
    [sourceImage setScalesWhenResized:YES];
    
    // Report an error if the source isn't a valid image
    if (![sourceImage isValid]){
        NSLog(@"Invalid Image");
    } else {
        NSImage *smallImage = [[NSImage alloc] initWithSize: newSize];
        [smallImage lockFocus];
        [sourceImage setSize: newSize];
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
        [sourceImage drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, newSize.width, newSize.height) operation:NSCompositingOperationCopy fraction:1.0];
        [smallImage unlockFocus];
        return smallImage;
    }
    return nil;
}

https://stackoverflow.com/questions/11949250/how-to-resize-nsimage

But given that Im not very familiar with Objective-C Im not pretty sure if there could be a simpler approach.

Thanks in advance!

@ryanmcgrath
Copy link
Owner

Hmm, does using ResizeBehavior::Stretch not hit the behavior you're after?

cacao/src/image/image.rs

Lines 18 to 32 in e3bbb93

/// Specifies resizing behavior for image drawing.
#[derive(Copy, Clone, Debug)]
pub enum ResizeBehavior {
/// Fit to the aspect ratio.
AspectFit,
/// Fill the aspect ratio.
AspectFill,
/// Stretch as necessary.
Stretch,
/// Center and then let whatever else flow around it.
Center
}

(Alternatively, you could implement a custom draw closure - maybe that'd help?)

@EstebanBorai
Copy link
Contributor Author

Thanks for the quick reply @ryanmcgrath!

Im trying with Image::draw this time.

        let img = Image::draw(DrawConfig {
            source: (512., 512.), // Original Image Size
            target: (64., 64.),   // Expected Size
            resize: ResizeBehavior::AspectFit, // Maintain Aspect Ratio
        }, |cg_rect, ctx| {
            // My understanding is that here I could attempt load my Image bytes into
            // a `CGImage` instance but I didn't found any `from_bytes` like method
            // in `core-graphics`
            ctx.draw_image(cg_rect, /* need a CGImage instance representing my file */);
            true
        });

Any suggestions?

@ryanmcgrath
Copy link
Owner

ryanmcgrath commented Jun 6, 2024

Hmmm, yeah, you'd need to dig around in Core Graphics to find an equivalent for drawing the image directly - it's been a bit since I've had to spelunk that low so my memory is unfortunately hazy there.

One odd path you could also consider: creating a cacao::image::Image and calling CGImageForProposedRect:context:hints: to get a CGImageRef from the underlying NSImage, then taking that into a new image (or custom draw handler).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants