From 31d2104e909bf26aafcfd631d7c5690699527d27 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 5 Nov 2026 10:15:01 -0500 Subject: [PATCH] Backup --- .DS_Store | Bin 10244 -> 0 bytes AGENTS.md | 21 +++++ CMakeLists.txt | 11 +-- include/window.h | 16 ++++ src/.DS_Store | Bin 6148 -> 0 bytes src/main.c | 14 ++-- src/window.m | 211 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 260 insertions(+), 13 deletions(-) delete mode 100644 .DS_Store create mode 100644 AGENTS.md create mode 100644 include/window.h delete mode 100644 src/.DS_Store create mode 100644 src/window.m diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index ff5db70f65b37fe244ec8c68fd62febe5734bfc2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10244 zcmeGhZEPIXac1o#yUm8Ijh!!Y@`X!6lsMQ<9EeGQ^V#`o5}f+%`1}#f-tGFlWN+8( z?ww<-AfpOeJ|rqii})3203m+3EoudA1r7btBDJI{ijay_8kI_gKZqack5mFWZ{ME1 zcV|d{AOvXF+Ba`z-pt#Xn|brzGXP+!sC59u0YIdeK}Du!iHGsoTk{A3Wj#S8d4NgK z&3s2XXBVbwMn{Cm1Ca+J4@4e_JP>){tKb2u+3XQlau~Id2Oh;0!>Els5P6{11MKX+9BzahsPxvH-|JX9RqzXTZ*SJLvqTpk zxN_f1JK>G)+A-Qy$4pIz?+N^@t*7tEg8^RU9m4z{NdE#aJpb0lf&Rze`Xie^#9y3y zDSpMg_%w-cG{f^?0Y3UWg4#6-u^ZUb@j`ZuMk&=E5(!PNqai&sb0}d zsm>_F25j5Rmiub_)$gnhfI%O9v&Glu|oFPi0G_I_h zo!#BFr!~I2eb1TJ_-xm%_SSgkO`T`XNMduMZD0TRDYIbN53`S^B`#PBVHL+0+DE8Y zVUy{Wdbmw>>9S4llx>o}wNhRc8_Eoi$YU8JXIp9GUVU6y?UG@K2|e!UB{FCzXPd=> z<&G;2In~S+P1V!8O_L2K+G;oF*rs{hb`8(4t>KKPpZ3a%Dp;8j*LJ)fU)i{#6kWYv zCtXHK?_A@5qAF=6PA)PSwhfTb4ni;e5nZ@6w_ELJ_btR+*- z8ut_pZ$?RoBA;VZEFmes62~<9$YZv~G-^I4wkdXlD2_6EkE2t|$@2DCtE8ynfJ+YI zD%#`@NpYotQxpPRv{SxGR5WSORI|FtNnP?jQ8^+FmAK`c*d*UBN*TIUE08fRr=?=O zqVk4#C!6@F>Uw=sx>f$9RP2x_j<}PmqxVfy0G1_=ilYC+5}k?c*9^~gd~e*bDHa=M zs=75z*U0D%>()2M(y>8Nd5dHe!zvjR^7LEq$QKjkXiFrw^KRmN?vmg0eJnMV2LzB6 z_!w-3y;RT#U;=EoA0CCX@Jo0VUV}H`ZFm>{1s}o{_%D16pI{xz*oYf(Gw#GTY{w3~ z1^3~tcpDzU0UX2=n8j0QqJ=hkcn0soIlLbq#K-W5_&ELupTeKwIeZSE$9a4iU%_AF z@9|IgXS{%a!FTcB_&$Dsm+@o#1V0rTgbl)GVT-U;XcO9nUBYhRfN)SaE({Am?6A6-GU`Vm|r^GS`HpM{2lq|S3ceTb`49gYnz(atc`Ek-qyMI*6xKc ze$4xEQ5~;56XljcN(%k-XC_zQdqe~Yi;@9;dn?#KN_{5$>=->Zt` zW0jGNm#ZVWly)rBw(>-0(Z39`P^1EFg7yt5lOcRu+)AFi#xV}AAa^ul3l!d|kb*2rXrRmV}Syq=ml=@+CvD`k70U07J9 z{3+8@YK{tP;`Wbt@JNZ|Hr`WUp(xa@{)zJ%AH}OfB6*yniRAt{-V*X2<7jI@zW)FedC`fQ{^WG2n#V+kD}Pvw?dTxQni><%Zn~ z;BN>6xhj{k&y1eP1Ca-=;(") diff --git a/include/window.h b/include/window.h new file mode 100644 index 0000000..12559e8 --- /dev/null +++ b/include/window.h @@ -0,0 +1,16 @@ +#ifndef WINDOW_H +#define WINDOW_H + +/* + * The pure-C contract, unchanged in spirit. + * window_run now takes the image: a read-only pixel buffer plus its + * dimensions. Nobody here cares that the implementation is AppKit. + * + * main.c loads the image (libimg owns the buffer), passes it in, + * and frees it AFTER window_run returns. The window layer only reads. + */ + +int +window_run(const unsigned int* buf, int width, int height); + +#endif /* WINDOW_H */ diff --git a/src/.DS_Store b/src/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 #include +#include #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; } diff --git a/src/window.m b/src/window.m new file mode 100644 index 0000000..593219e --- /dev/null +++ b/src/window.m @@ -0,0 +1,211 @@ +#import +#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 +@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; + } +}