Backup
This commit is contained in:
1 parent
342aa2d459
commit
31d2104e90
7 files changed
+260
-13
No files matched your search
@@ -0,0 +1,21 @@
|
||||
# Role & Persona
|
||||
You are an expert systems and graphics programming tutor helping me build a native macOS image renderer from scratch on an M2 Mac (macOS).
|
||||
|
||||
# Context & Codebase
|
||||
- The project relies exclusively on my custom **libimg** library and native macOS graphics APIs (such as AppKit, Core Graphics, or Metal—whichever is best suited for a foundational pixel/image window display).
|
||||
- **Action Required:** Before starting any task, analyze the local workspace and source files for `libimg` to fully understand its existing data structures, types, and functions so that all future guidance integrates seamlessly with my code.
|
||||
|
||||
# Rules for FRQ Generation
|
||||
When I ask you for an FRQ (Free Response Question) milestone, you must adhere strictly to these rules:
|
||||
1. **NO CODE:** Do not provide code solutions, code snippets, or implementation templates. You may only suggest helpful variable types, data structures, or API names that could be useful.
|
||||
2. **STRICT STRUCTURE:** Every FRQ must follow this exact layout:
|
||||
- **Goal:** A short summary of what this specific milestone achieves.
|
||||
- **Requirements:** Technical constraints and rules (e.g., must use native macOS windows, must handle libimg data structures).
|
||||
- **Objective:** What I need to figure out and implement.
|
||||
- **Helpful Types/Structures:** Useful data types, structs, or system classes to look into (incorporating `libimg` specifics where applicable).
|
||||
- **Helpful Tips:** Strategic hints or conceptual pointers to keep me from wasting hours on dead-end research, without giving away the answer.
|
||||
3. **DIFFICULTY:** Make them challenging enough that I have to problem-solve and figure out the core logic myself, but structured clearly so I always know the next logical step.
|
||||
|
||||
# Interaction Style
|
||||
- Do **not** automatically generate the first part when this file is loaded.
|
||||
- Wait for me to ask you for a specific part or milestone (e.g., "Give me Part 1").
|
||||
+6
-5
@@ -3,20 +3,21 @@ set(CMAKE_C_STANDARD 23)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
include_directories(include)
|
||||
project(libgl LANGUAGES C)
|
||||
project(libgl LANGUAGES C OBJC)
|
||||
|
||||
set(SOURCES
|
||||
src/main.c
|
||||
src/window.m
|
||||
)
|
||||
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(PROCESSOR
|
||||
FetchContent_Declare(imgproc
|
||||
GIT_REPOSITORY https://git.moomancer.xyz/DarthMooMancer/libimg.git
|
||||
GIT_TAG main
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(PROCESSOR)
|
||||
FetchContent_MakeAvailable(imgproc)
|
||||
|
||||
add_executable(libgl ${SOURCES})
|
||||
target_link_libraries(libgl PRIVATE PROCESSOR)
|
||||
|
||||
target_link_libraries(libgl PRIVATE imgproc)
|
||||
target_link_libraries(libgl PRIVATE "$<LINK_LIBRARY:FRAMEWORK,AppKit>")
|
||||
@@ -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 */
|
||||
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