much better.

This commit is contained in:
Yggdrasil75
2025-11-06 09:56:57 -05:00
parent ce3014ce71
commit f4f9442cfd
7 changed files with 230 additions and 53 deletions

View File

@@ -8,9 +8,8 @@
#include <algorithm>
#include <filesystem>
#include "vec3.hpp"
// #include "../jxl/encode.h"
#include "timing_decorator.hpp"
#include <jxl/encode.h>
// #include "../jxl/thread_parallel_runner.h"
#include <jxl/thread_parallel_runner.h>
class JXLWriter {
@@ -64,10 +63,8 @@ private:
public:
// Save a 2D vector of Vec3 (RGB) colors as JXL
// Vec3 components: x = red, y = green, z = blue (values in range [0,1])
static bool saveJXL(const std::string& filename,
const std::vector<std::vector<Vec3>>& pixels,
float quality = 90.0f, // Quality setting (0-100)
int effort = 7) { // Encoding effort (3-9, higher = slower but better compression)
static bool saveJXL(const std::string& filename, const std::vector<std::vector<Vec3>>& pixels,
float quality = 90.0f, int effort = 7) {
if (pixels.empty() || pixels[0].empty()) {
return false;
}
@@ -85,12 +82,8 @@ public:
return saveJXL(filename, pixels, width, height, quality, effort);
}
// Alternative interface with width/height and flat vector (row-major order)
static bool saveJXL(const std::string& filename,
const std::vector<Vec3>& pixels,
int width, int height,
float quality = 90.0f,
int effort = 7) {
static bool saveJXL(const std::string& filename, const std::vector<Vec3>& pixels,
int width, int height, float quality = 90.0f, int effort = 7) {
if (pixels.size() != width * height) {
return false;
}
@@ -107,11 +100,9 @@ public:
}
// Save from 1D vector of uint8_t pixels (RGB order: pixels[i]=r, pixels[i+1]=g, pixels[i+2]=b)
static bool saveJXL(const std::string& filename,
const std::vector<uint8_t>& pixels,
int width, int height,
float quality = 90.0f,
int effort = 7) {
static bool saveJXL(const std::string& filename, const std::vector<uint8_t>& pixels,
int width, int height, float quality = 90.0f, int effort = 7) {
TIME_FUNCTION;
if (pixels.size() != width * height * 3) {
return false;
}
@@ -223,20 +214,14 @@ public:
}
private:
static bool saveJXL(const std::string& filename,
const std::vector<std::vector<Vec3>>& pixels,
int width, int height,
float quality,
int effort) {
// Create directory if needed
static bool saveJXL(const std::string& filename, const std::vector<std::vector<Vec3>>& pixels,
int width, int height, float quality, int effort) {
if (!createDirectoryIfNeeded(filename)) {
return false;
}
// Convert Vec3 pixels to interleaved RGB
std::vector<uint8_t> rgbData = convertToRGB(pixels, width, height);
// Use the existing uint8_t version
return saveJXL(filename, rgbData, width, height, quality, effort);
}
};

View File

@@ -9,6 +9,9 @@
#include <cstring>
#include <sstream>
#include <fstream>
#include <functional>
#include <unordered_map>
#include "timing_decorator.hpp"
#ifdef _WIN32
#include <winsock2.h>
@@ -28,6 +31,10 @@ private:
bool running;
std::string webRoot;
// Route handler type
using RouteHandler = std::function<std::pair<int, std::string>(const std::string&, const std::string&)>;
std::unordered_map<std::string, RouteHandler> routes;
// Read file content
std::string readFile(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
@@ -55,14 +62,17 @@ private:
// Send HTTP response
void sendResponse(int clientSocket, const std::string& content, const std::string& contentType = "text/html", int statusCode = 200) {
TIME_FUNCTION;
std::string statusText = "OK";
if (statusCode == 404) statusText = "Not Found";
if (statusCode == 500) statusText = "Internal Server Error";
if (statusCode == 405) statusText = "Method Not Allowed";
std::ostringstream response;
response << "HTTP/1.1 " << statusCode << " " << statusText << "\r\n";
response << "Content-Type: " << contentType << "\r\n";
response << "Content-Length: " << content.length() << "\r\n";
response << "Access-Control-Allow-Origin: *\r\n";
response << "Connection: close\r\n";
response << "\r\n";
response << content;
@@ -71,18 +81,23 @@ private:
send(clientSocket, responseStr.c_str(), responseStr.length(), 0);
}
// Extract method and path from HTTP request
std::pair<std::string, std::string> parseRequest(const std::string& request) {
TIME_FUNCTION;
std::istringstream iss(request);
std::string method, path;
iss >> method >> path;
return {method, path};
}
// Extract file path from HTTP request
std::string getFilePath(const std::string& request) {
// Find the start of the path after "GET "
size_t start = request.find("GET ") + 4;
if (start == std::string::npos) return "";
auto [method, path] = parseRequest(request);
// Find the end of the path (space or ?)
size_t end = request.find(" ", start);
if (end == std::string::npos) end = request.find("?", start);
if (end == std::string::npos) return "";
std::string path = request.substr(start, end - start);
// Only handle GET requests for files
if (method != "GET") {
return "";
}
// Default to index.html for root path
if (path == "/") {
@@ -96,6 +111,31 @@ private:
return path;
}
// Check if path is a registered route
bool isRoute(const std::string& path) {
return routes.find(path) != routes.end();
}
// Handle route request
void handleRoute(int clientSocket, const std::string& request) {
TIME_FUNCTION;
auto [method, path] = parseRequest(request);
// Extract request body if present
std::string body;
size_t bodyPos = request.find("\r\n\r\n");
if (bodyPos != std::string::npos) {
body = request.substr(bodyPos + 4);
}
if (routes.find(path) != routes.end()) {
auto [statusCode, response] = routes[path](method, body);
sendResponse(clientSocket, response, "application/json", statusCode);
} else {
sendResponse(clientSocket, "404 Not Found", "text/plain", 404);
}
}
public:
SimpleHTTPServer(int port = 8080, const std::string& webRoot = "web")
@@ -105,6 +145,11 @@ public:
stop();
}
// Add a route handler
void addRoute(const std::string& path, RouteHandler handler) {
routes[path] = handler;
}
bool start() {
#ifdef _WIN32
WSADATA wsaData;
@@ -186,21 +231,29 @@ public:
if (bytesReceived > 0) {
std::string request(buffer);
std::string filePath = getFilePath(request);
auto [method, path] = parseRequest(request);
if (!filePath.empty()) {
std::cout << "Serving: " << filePath << std::endl;
std::string fullPath = webRoot + "/" + filePath;
std::string content = readFile(fullPath);
if (!content.empty()) {
sendResponse(clientSocket, content, getContentType(filePath));
} else {
sendResponse(clientSocket, "404 Not Found: " + filePath, "text/plain", 404);
}
// Check if this is a registered route
if (isRoute(path)) {
handleRoute(clientSocket, request);
} else {
sendResponse(clientSocket, "400 Bad Request", "text/plain", 400);
// Handle file serving for GET requests
std::string filePath = getFilePath(request);
if (!filePath.empty()) {
std::cout << "Serving: " << filePath << std::endl;
std::string fullPath = webRoot + "/" + filePath;
std::string content = readFile(fullPath);
if (!content.empty()) {
sendResponse(clientSocket, content, getContentType(filePath));
} else {
sendResponse(clientSocket, "404 Not Found: " + filePath, "text/plain", 404);
}
} else {
sendResponse(clientSocket, "400 Bad Request", "text/plain", 400);
}
}
}

View File

@@ -47,10 +47,10 @@ public:
// Clear all statistics
static void clearStats();
static PercentileStats calculatePercentiles(const std::vector<double>& timings);
private:
static std::unordered_map<std::string, TimingStats> stats_;
static PercentileStats calculatePercentiles(const std::vector<double>& timings);
};
// Macro to easily time functions - similar to Python decorator