瀏覽代碼

replaced png reader with the png reader from the gaming core

Kajetan Johannes Hammerle 3 年之前
父節點
當前提交
cabcdf10fb
共有 6 個文件被更改,包括 35 次插入102 次删除
  1. 17 6
      client/rendering/FileTexture.cpp
  2. 16 1
      client/rendering/FileTexture.h
  3. 0 85
      client/utils/PNGReader.cpp
  4. 0 8
      client/utils/PNGReader.h
  5. 1 1
      gaming-core
  6. 1 1
      meson.build

+ 17 - 6
client/rendering/FileTexture.cpp

@@ -1,12 +1,23 @@
 #include "client/rendering/FileTexture.h"
-#include "client/utils/PNGReader.h"
 
 FileTexture::FileTexture(const char* path) {
-    int maxWidth = 256;
-    int maxHeight = 256;
-    int buffer[256 * 256];
-    if(PNGReader::load(path, buffer, maxWidth, maxHeight)) {
-        texture.setColorData(maxWidth, maxHeight, reinterpret_cast<Color4*>(buffer));
+    PNGReader png(path);
+    if(png.hasError()) {
+        return;
+    }
+    switch(png.getChannels()) {
+        case 1:
+            read<1>(png);
+            break;
+        case 2:
+            read<2>(png);
+            break;
+        case 3:
+            read<3>(png);
+            break;
+        case 4:
+            read<4>(png);
+            break;
     }
 }
 

+ 16 - 1
client/rendering/FileTexture.h

@@ -1,15 +1,30 @@
 #ifndef FILE_TEXTURE_H
 #define FILE_TEXTURE_H
 
+#include <iostream>
+
 #include "client/rendering/wrapper/Texture.h"
+#include "gaming-core/images/PNGReader.h"
 
 class FileTexture final {
     Texture texture;
-    
+
 public:
     FileTexture(const char* path);
 
     void bind(int index) const;
+
+private:
+
+    template<int N>
+    void read(PNGReader& png) {
+        Color<N>* buffer = new Color<N>[png.getBufferSize()];
+        if(png.readData(buffer)) {
+            return;
+        }
+        texture.setColorData(png.getWidth(), png.getHeight(), buffer);
+        delete[] buffer;
+    }
 };
 
 #endif

+ 0 - 85
client/utils/PNGReader.cpp

@@ -1,85 +0,0 @@
-#include <iostream>
-#include <png.h>
-#include <cstring>
-
-#include "client/utils/PNGReader.h"
-
-static bool checkSignature(const char* path, FILE* file) {
-    unsigned char buffer[8];
-    if(fread(buffer, sizeof (char), 8, file) != 8) {
-        std::cout << "cannot read signature of texture '" << path << "'\n";
-        return true;
-    }
-    if(png_sig_cmp(buffer, 0, 8)) {
-        std::cout << "file '" << path << "' is not a texture\n";
-        return true;
-    }
-    return false;
-}
-
-static bool load(const char* path, FILE* file, int* buffer, int& maxWidth, int& maxHeight) {
-    if(checkSignature(path, file)) {
-        return false;
-    }
-
-    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
-    if(png == nullptr) {
-        std::cout << "cannot create texture data structure\n";
-        return false;
-    }
-
-    png_infop info = png_create_info_struct(png);
-    if(info == nullptr) {
-        std::cout << "cannot create image info structure\n";
-        return false;
-    }
-
-    int** rowPointers = nullptr;
-
-    if(setjmp(png_jmpbuf(png))) { // set callback for errors
-        if(rowPointers != nullptr) {
-            png_free(png, rowPointers);
-        }
-        png_destroy_read_struct(&png, &info, nullptr);
-        std::cout << "texture '" << path << "' has used error callback\n";
-        return false;
-    }
-
-    png_init_io(png, file); // set reading function
-    png_set_sig_bytes(png, 8); // notify about already used signature bytes
-
-    png_read_info(png, info); // read info data
-    
-    int width = png_get_image_width(png, info);
-    int height = png_get_image_height(png, info);
-    if(width > maxWidth || height > maxHeight) {
-        std::cout << "texture '" << path << "' is too big: " << maxWidth << " x " << maxHeight << " allowed\n";
-        return false;
-    }
-    maxWidth = width;
-    maxHeight = height;
-    
-    // allocate and set row pointer to correct places in block
-    rowPointers = static_cast<int**> (png_malloc(png, height * (sizeof (int*))));
-    for(int i = 0; i < height; i++) {
-        rowPointers[i] = (buffer + i * width);
-    }
-    png_set_rows(png, info, reinterpret_cast<png_bytepp> (rowPointers));
-
-    png_read_image(png, reinterpret_cast<png_bytepp> (rowPointers));
-
-    png_free(png, rowPointers);
-    png_destroy_read_struct(&png, &info, nullptr);
-    return true;
-}
-
-bool PNGReader::load(const char* path, int* buffer, int& maxWidth, int& maxHeight) {
-    FILE* file = fopen(path, "r");
-    if(file == nullptr) {
-        std::cout << "texture '" << path << "' cannot be read: " << strerror(errno) << "\n";
-        return false;
-    }
-    bool result = load(path, file, buffer, maxWidth, maxHeight);
-    fclose(file);
-    return result;
-}

+ 0 - 8
client/utils/PNGReader.h

@@ -1,8 +0,0 @@
-#ifndef PNGREADER_H
-#define PNGREADER_H
-
-namespace PNGReader {
-    bool load(const char* path, int* buffer, int& maxWidth, int& maxHeight);
-}
-
-#endif

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 375ceefe325e4b3394b613f6a8952eef20410f3d
+Subproject commit bff2959facb9b6d78684bc188f5b9e326dbc95d6

+ 1 - 1
meson.build

@@ -43,7 +43,7 @@ sourcesClient = ['client/Main.cpp',
     'client/rendering/wrapper/VertexBuffer.cpp',
     'client/rendering/wrapper/StreamBuffer.cpp',
     'client/rendering/wrapper/Texture.cpp',
-    'client/utils/PNGReader.cpp',
+    'gaming-core/images/PNGReader.cpp',
     'client/rendering/wrapper/GLWrapper.cpp',
     'client/rendering/Renderer.cpp',
     'client/rendering/renderer/WorldRenderer.cpp',