1
This commit is contained in:
@@ -15,19 +15,21 @@ file(GLOB_RECURSE HEADERS "include/*.h")
|
||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||
file(COPY "testImages" DESTINATION "${PROJECT_BINARY_DIR}")
|
||||
|
||||
find_package(PNG REQUIRED)
|
||||
|
||||
include_directories("include" "/usr/include/libpng16")
|
||||
|
||||
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
|
||||
message(FATAL_ERROR "In-Source builds are not allowed")
|
||||
endif()
|
||||
|
||||
add_library(ReTexture SHARED ${SOURCES} ${HEADERS})
|
||||
if (UNIX AND NOT APPLE)
|
||||
add_library(ReTexture SHARED ${SOURCES} ${HEADERS})
|
||||
endif()
|
||||
|
||||
if (WIN32)
|
||||
add_library(ReTexture STATIC ${SOURCES} ${HEADERS})
|
||||
endif()
|
||||
|
||||
target_include_directories(ReTexture PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||
add_executable(ReTextureTest main.cpp)
|
||||
|
||||
set_target_properties(ReTexture PROPERTIES LINKER_LANGUAGE CXX)
|
||||
set_target_properties(ReTextureTest PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
target_link_libraries(ReTexture PUBLIC PNG::PNG)
|
||||
target_link_libraries(ReTextureTest PUBLIC ReTexture)
|
||||
|
@@ -3,7 +3,6 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <ReTexture/flags.h>
|
||||
#include <png.h>
|
||||
|
||||
class RTexture {
|
||||
private:
|
||||
@@ -12,9 +11,10 @@ private:
|
||||
void loadPNG(const std::string& file);
|
||||
void invertY();
|
||||
public:
|
||||
uint width;
|
||||
uint height;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
RTextureFormat format;
|
||||
std::vector<unsigned char> pixelData;
|
||||
RTexture(const std::string& file, const std::vector<RTextureFlag>& args);
|
||||
explicit RTexture(const std::string& file);
|
||||
};
|
7988
include/stb_image.h
Normal file
7988
include/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,10 @@
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <ReTexture/rTexture.h>
|
||||
#include <iostream>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
void RTexture::load(const std::string& file) {
|
||||
std::ifstream ifStream(file, std::ios::in | std::ios::binary);
|
||||
unsigned char bmpFileHeader[14];
|
||||
@@ -49,75 +52,23 @@ void RTexture::loadBMP(const std::string& file) {
|
||||
}
|
||||
|
||||
void RTexture::loadPNG(const std::string &file) {
|
||||
FILE *fp = fopen(file.c_str(), "rb");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "Could not open file %s for reading\n", file.c_str());
|
||||
int channels, w, h;
|
||||
|
||||
unsigned char *imageData = stbi_load(file.c_str(), &w, &h, &channels, 0);
|
||||
|
||||
if (imageData == nullptr)
|
||||
return;
|
||||
}
|
||||
|
||||
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
if (!png) {
|
||||
fprintf(stderr, "Could not create png read struct\n");
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
width = w;
|
||||
height = h;
|
||||
if (channels == 3)
|
||||
format = RTextureFormat::RGB;
|
||||
|
||||
png_infop info = png_create_info_struct(png);
|
||||
if (!info) {
|
||||
fprintf(stderr, "Could not create png info struct\n");
|
||||
png_destroy_read_struct(&png, nullptr, nullptr);
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png))) {
|
||||
fprintf(stderr, "Error during png creation\n");
|
||||
png_destroy_read_struct(&png, &info, nullptr);
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
|
||||
png_init_io(png, fp);
|
||||
png_read_info(png, info);
|
||||
|
||||
width = png_get_image_width(png, info);
|
||||
height = png_get_image_height(png, info);
|
||||
png_byte color_type = png_get_color_type(png, info);
|
||||
png_byte bit_depth = png_get_bit_depth(png, info);
|
||||
|
||||
if (bit_depth == 16)
|
||||
png_set_strip_16(png);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_palette_to_rgb(png);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
|
||||
png_set_expand_gray_1_2_4_to_8(png);
|
||||
|
||||
if (png_get_valid(png, info, PNG_INFO_tRNS))
|
||||
png_set_tRNS_to_alpha(png);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
||||
png_set_gray_to_rgb(png);
|
||||
|
||||
png_read_update_info(png, info);
|
||||
|
||||
pixelData.resize(png_get_rowbytes(png, info) * height);
|
||||
std::vector<png_bytep> row_pointers(height);
|
||||
|
||||
for (unsigned y = 0; y < height; ++y) {
|
||||
row_pointers[y] = pixelData.data() + y * png_get_rowbytes(png, info);
|
||||
}
|
||||
|
||||
png_read_image(png, row_pointers.data());
|
||||
|
||||
fclose(fp);
|
||||
png_destroy_read_struct(&png, &info, nullptr);
|
||||
if (color_type & PNG_COLOR_MASK_ALPHA)
|
||||
if (channels == 4)
|
||||
format = RTextureFormat::RGBA;
|
||||
|
||||
if (!(color_type & PNG_COLOR_MASK_ALPHA))
|
||||
format = RTextureFormat::RGB;
|
||||
pixelData.assign(imageData, imageData + width * height * channels);
|
||||
stbi_image_free(imageData);
|
||||
}
|
||||
|
||||
RTexture::RTexture(const std::string &file, const std::vector<RTextureFlag> &args) {
|
||||
@@ -134,7 +85,7 @@ void RTexture::invertY() {
|
||||
rowSize = width * 4;
|
||||
|
||||
std::vector<unsigned char> temp(rowSize);
|
||||
for (uint y = 0; y < height / 2; ++y) {
|
||||
for (unsigned int y = 0; y < height / 2; ++y) {
|
||||
unsigned char* topRow = &pixelData[y * rowSize];
|
||||
unsigned char* bottomRow = &pixelData[(height - y - 1) * rowSize];
|
||||
std::copy(bottomRow, bottomRow + rowSize, temp.begin());
|
||||
@@ -142,3 +93,7 @@ void RTexture::invertY() {
|
||||
std::copy(temp.begin(), temp.end(), topRow);
|
||||
}
|
||||
}
|
||||
|
||||
RTexture::RTexture(const std::string& file) {
|
||||
*this = RTexture(file, {});
|
||||
}
|
||||
|
Reference in New Issue
Block a user