Backup
This commit is contained in:
1 parent
342aa2d459
commit
31d2104e90
7 files changed
+260
-13
No files matched your search
Vendored
BIN
Binary file not shown.
+6
-8
@@ -1,5 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <window.h>
|
||||
|
||||
#include "image/filters.h"
|
||||
#include "image/imgio.h"
|
||||
@@ -10,17 +10,15 @@ int
|
||||
main()
|
||||
{
|
||||
JPEG img;
|
||||
PPM out;
|
||||
img.name = "../assets/test.jpeg";
|
||||
|
||||
unsigned int* buf = jpeg_load(&img);
|
||||
// PPM out;
|
||||
img.name = "../assets/IMG_1649.jpg";
|
||||
//
|
||||
unsigned int* buf = read_jpeg(&img);
|
||||
for (int i = 0; i < img.len; i++)
|
||||
{
|
||||
buf[i] = u32_grayscale(buf[i]);
|
||||
}
|
||||
out = inherit_from_jpeg(&img, '3');
|
||||
out.name = "../assets/test.ppm";
|
||||
ppm_save(buf, out, out.name);
|
||||
window_run(buf, img.width, img.height);
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
#import <AppKit/AppKit.h>
|
||||
#include "window.h"
|
||||
|
||||
static const CGFloat kScreenMargin = 40.0; /* keep some breathing room */
|
||||
|
||||
/*
|
||||
* The renderer view. It knows one thing: an image, and how to draw it.
|
||||
* Aspect-fit + centered, so it survives window resizes.
|
||||
*
|
||||
* NOTE the ownership discipline: CGImage is a CoreFoundation type, which
|
||||
* ARC does NOT manage. We CFRetain on init and CFRelease in dealloc.
|
||||
*/
|
||||
@interface ImageRenderView : NSView
|
||||
{
|
||||
CGImageRef _image;
|
||||
}
|
||||
- (instancetype)initWithFrame:(NSRect)frame image:(CGImageRef)image;
|
||||
@end
|
||||
|
||||
@implementation ImageRenderView
|
||||
|
||||
- (instancetype)initWithFrame:(NSRect)frame image:(CGImageRef)image
|
||||
{
|
||||
self = [super initWithFrame:frame];
|
||||
if (self)
|
||||
{
|
||||
/* We take our own +1 so the caller keeps ownership of theirs. */
|
||||
_image = image ? CFRetain(image) : NULL;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
if (_image != NULL)
|
||||
{
|
||||
CFRelease(_image);
|
||||
}
|
||||
/* NOTE: no [super dealloc] call under ARC — the compiler inserts it. */
|
||||
}
|
||||
|
||||
- (void)drawRect:(NSRect)rect
|
||||
{
|
||||
if (_image == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CGContextRef ctx = [[NSGraphicsContext currentContext] CGContext];
|
||||
|
||||
/* Native size of the image, in pixels. */
|
||||
CGFloat imgW = CGImageGetWidth(_image);
|
||||
CGFloat imgH = CGImageGetHeight(_image);
|
||||
if (imgW <= 0 || imgH <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* Aspect-fit: uniform scale that makes the image fit in our bounds. */
|
||||
CGFloat scale = MIN(self.bounds.size.width / imgW,
|
||||
self.bounds.size.height / imgH);
|
||||
|
||||
CGFloat drawW = imgW * scale;
|
||||
CGFloat drawH = imgH * scale;
|
||||
|
||||
/* Center it in our bounds. */
|
||||
CGRect dest = CGRectMake((self.bounds.size.width - drawW) / 2.0,
|
||||
(self.bounds.size.height - drawH) / 2.0,
|
||||
drawW,
|
||||
drawH);
|
||||
|
||||
CGContextDrawImage(ctx, dest, _image);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
* The delegate. Same as before, plus the image plumbing:
|
||||
* stores buf/w/h, builds the CGImage and the render view at launch,
|
||||
* sizes the window TO the image, then shows it.
|
||||
*/
|
||||
@interface WindowDelegate : NSObject <NSApplicationDelegate>
|
||||
@property (strong) NSWindow* window;
|
||||
@property (strong) ImageRenderView* renderView;
|
||||
@property (nonatomic) const unsigned int* buf; /* read-only peephole into main's buffer */
|
||||
@property (nonatomic) int width;
|
||||
@property (nonatomic) int height;
|
||||
@end
|
||||
|
||||
@implementation WindowDelegate
|
||||
|
||||
// /*
|
||||
// * Wrap
|
||||
- (void)applicationDidFinishLaunching:(NSNotification*)note
|
||||
{
|
||||
if (self.buf == NULL || self.width <= 0 || self.height <= 0)
|
||||
{
|
||||
return; /* nothing to draw; don't crash */
|
||||
}
|
||||
|
||||
/*
|
||||
* 1. Wrap the C buffer in a Core Graphics image.
|
||||
*
|
||||
* Data provider = a window into main.c's memory. The release
|
||||
* callback is NULL, meaning "I don't own these bytes, never free
|
||||
* them." The bytes stay valid because main only frees buf AFTER
|
||||
* window_run returns (the pool drains, the window/view/image get
|
||||
* released, THEN main frees). Keep that ordering in mind forever.
|
||||
*/
|
||||
size_t bytesPerRow = (size_t)self.width * 4;
|
||||
CGDataProviderRef provider = CGDataProviderCreateWithData(
|
||||
NULL, self.buf, bytesPerRow * self.height, NULL);
|
||||
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
|
||||
/* The pixel-format decision — this is the "get it wrong = red/blue
|
||||
* swap" part. libimg stores host-endian 0xAARRGGBB. On your M2
|
||||
* (little-endian) the byte order in memory is B,G,R,A, which is
|
||||
* exactly what:
|
||||
* kCGImageAlphaFirst | kCGBitmapByteOrder32Host
|
||||
* means: alpha first in the word, 32-bit host byte order. */
|
||||
CGImageRef image = CGImageCreate(
|
||||
self.width, self.height, /* pixel dimensions */
|
||||
8, 32, /* 8 bits/channel, 32 bits/px */
|
||||
bytesPerRow, /* no padding in libimg rows */
|
||||
colorSpace,
|
||||
kCGImageAlphaFirst | kCGBitmapByteOrder32Host,
|
||||
provider, NULL, NO, /* translate, decode, interpol*/
|
||||
kCGRenderingIntentDefault);
|
||||
|
||||
CGDataProviderRelease(provider); /* image keeps its own ref */
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
|
||||
/*
|
||||
* 2. Size the window TO the image, capped by the screen.
|
||||
* scale = min(1, ...) => never upscale past native pixels.
|
||||
*/
|
||||
CGRect visible = [[NSScreen mainScreen] visibleFrame];
|
||||
CGFloat capW = visible.size.width - 2.0 * kScreenMargin;
|
||||
CGFloat capH = visible.size.height - 2.0 * kScreenMargin;
|
||||
|
||||
CGFloat scale = MIN(1.0, MIN(capW / (CGFloat)self.width,
|
||||
capH / (CGFloat)self.height));
|
||||
NSSize contentSize = NSMakeSize(round(self.width * scale),
|
||||
round(self.height * scale));
|
||||
|
||||
/*
|
||||
* 3. Build the window with content rect = the fitted size.
|
||||
* initWithContentRect: means this IS the drawable area; the title
|
||||
* bar is added on top by the style mask.
|
||||
*/
|
||||
NSWindow* w = [[NSWindow alloc]
|
||||
initWithContentRect:NSMakeRect(0, 0, contentSize.width, contentSize.height)
|
||||
styleMask:(NSWindowStyleMaskTitled |
|
||||
NSWindowStyleMaskClosable |
|
||||
NSWindowStyleMaskResizable)
|
||||
backing:NSBackingStoreBuffered
|
||||
defer:NO];
|
||||
w.releasedWhenClosed = NO; /* we own the lifetime (ARC strong ref) */
|
||||
|
||||
/*
|
||||
* 4. Install the renderer as the content view. Creating it with a
|
||||
* zero rect is fine — setting it as contentView sizes it to the
|
||||
* window's content area. The view CFRetains the image; we drop
|
||||
* OUR +1 now that the view holds it.
|
||||
*/
|
||||
ImageRenderView* view = [[ImageRenderView alloc] initWithFrame:NSZeroRect
|
||||
image:image];
|
||||
CGImageRelease(image); /* view now owns the only reference */
|
||||
|
||||
w.contentView = view;
|
||||
self.window = w;
|
||||
self.renderView = view;
|
||||
|
||||
[w setTitle:@"libgl"];
|
||||
[w center];
|
||||
[NSApp activate]; /* make it a real frontmost app */
|
||||
[w makeKeyAndOrderFront:nil]; /* show + focus */
|
||||
}
|
||||
|
||||
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender
|
||||
{
|
||||
return YES; /* closing the window = clean exit, code 0 */
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
int
|
||||
window_run(const unsigned int* buf, int width, int height)
|
||||
{
|
||||
@autoreleasepool
|
||||
{
|
||||
NSApplication* app = [NSApplication sharedApplication];
|
||||
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
|
||||
|
||||
/*
|
||||
* Buffer rides along in the delegate so it's available in the
|
||||
* launch callback. Delegate must stay alive (strong local)
|
||||
* through run — NSApplication.delegate is ASSIGN, not strong.
|
||||
*/
|
||||
WindowDelegate* delegate = [[WindowDelegate alloc] init];
|
||||
delegate.buf = buf;
|
||||
delegate.width = width;
|
||||
delegate.height = height;
|
||||
[app setDelegate:delegate];
|
||||
|
||||
[app run]; /* blocks until the app terminates */
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user