Skip to content

Commit

Permalink
missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Jun 16, 2024
1 parent b06b4a7 commit be74779
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
66 changes: 66 additions & 0 deletions image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "image.h"

#include <algorithm>
#include <cstring>
#include <span>
#include <stdexcept>
#include <string>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

Image::Image(std::span<const uint8_t> data) {
data_ = (uint32_t *)stbi_load_from_memory(data.data(), data.size(), &width,
&height, nullptr, 4);
if (data_ == nullptr) {
throw std::runtime_error("failed to load image");
}
}

Image::Image(const std::string &path) {
data_ = (uint32_t *)stbi_load(path.c_str(), &width, &height, nullptr, 4);
if (data_ == nullptr) {
throw std::runtime_error("failed to load image");
}
}

Image::Image(int width, int height) : width(width), height(height) {
data_ = (uint32_t *)malloc(width * height * 4);
std::memset(data_, 0, width * height * 4);
}

Image::Image(Image &&other) noexcept {
data_ = other.data_;
width = other.width;
height = other.height;

other.data_ = nullptr;
other.width = 0;
other.height = 0;
}

Image::Image(const char *data, size_t size) {
data_ = (uint32_t *)stbi_load_from_memory((stbi_uc *)data, size, &width,
&height, nullptr, 4);
if (data_ == nullptr) {
throw std::runtime_error("failed to load image");
}
}

Image::~Image() { free(data_); }

void Image::save_png(const std::string &path) {
stbi_write_png(path.c_str(), width, height, 4, data_, width * 4);
}

const void *Image::get_png(int *len) {
return stbi_write_png_to_mem((const unsigned char *)data_, width * 4, width,
height, 4, len);
}

void Image::save_png_from_data(const std::string &path, const void *data, int w,
int h) {
stbi_write_png(path.c_str(), w, h, 4, data, w * 4);
}
62 changes: 62 additions & 0 deletions image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include <span>
#include <string>
#include <vector>

struct S32Vec2 {
int x;
int y;
};

struct FVec2 {
float x;
float y;
};

struct U16Vec2 {
uint16_t x;
uint16_t y;
};

class Image {
uint32_t *data_ = nullptr;
int width = 0, height = 0;

public:
Image() {}
Image(std::span<const uint8_t> data);
Image(const std::string &path);
Image(int width, int height);
Image(S32Vec2 size) : Image(size.x, size.y) {}
Image(const char *data, size_t size);

Image(Image &&other) noexcept;
Image(const Image &) = delete;
Image &operator=(const Image &) = delete;

~Image();

void save_png(const std::string &path);
const void *get_png(int *len);

S32Vec2 size() const { return {width, height}; }
uint32_t *data() { return data_; }
const uint32_t *data() const { return data_; }

uint32_t operator()(int x, int y) const { return data_[x + y * width]; }
uint32_t &operator()(int x, int y) { return data_[x + y * width]; }

bool operator==(const Image &other) const {
if (width != other.width || height != other.height)
return false;
for (int i = 0; i < width * height; ++i) {
if (data_[i] != other.data_[i])
return false;
}
return true;
}

static void save_png_from_data(const std::string &path, const void *data,
int w, int h);
};

0 comments on commit be74779

Please sign in to comment.