working on xpm support

This commit is contained in:
2023-04-05 01:11:49 +02:00
parent c85c12bfaf
commit 27000b6163
10 changed files with 110 additions and 82 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2023/04/04 14:54:12 by maldavid ### ########.fr */
/* Updated: 2023/04/05 01:11:36 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,14 @@
#include <renderer/images/texture.h>
#include <renderer/core/render_core.h>
#include <X11/X.h> // for LSBFirst
#include <cstdio>
#include <array>
#include <cstring>
#ifndef MLX_NO_XPM
#include <X11/xpm.h>
#endif
#include <iostream>
namespace mlx::core
{
@@ -62,15 +69,85 @@ namespace mlx::core
return map;
}
#ifndef MLX_NO_XPM
struct SafeXpmImage: public XpmImage // included in <X11/xpm.h>
{
~SafeXpmImage() { XpmFreeXpmImage(this); }
};
void* Application::newXpmTexture(char** data, int* w, int* h)
{
SafeXpmImage xpm_image;
if(XpmCreateXpmImageFromData(data, &xpm_image, nullptr) != XpmSuccess)
{
error::report(e_kind::error, "XPM reader : invalid xpm file");
return nullptr;
}
*w = xpm_image.width;
*h = xpm_image.height;
std::vector<uint8_t> formatted_data;
formatted_data.reserve((*w) * (*h) * 4);
uint32_t pixel_color;
for(int i = 0; i < *h; i++)
{
for(int j = 0, k = 0; k < *w; j += 4, k++)
{
pixel_color = -1;
for(int l = 0; l < xpm_image.ncolors; l++)
{
if(std::memcmp(xpm_image.colorTable[l].c_color, &xpm_image.data[(i * (*w)) + k], 1) == 0)
{
pixel_color = l;
break;
}
}
if(pixel_color < xpm_image.ncolors)
{
formatted_data[(i * (*w)) + j + 0] = xpm_image.colorTable[pixel_color].m_color[0];
formatted_data[(i * (*w)) + j + 1] = xpm_image.colorTable[pixel_color].m_color[1];
formatted_data[(i * (*w)) + j + 2] = xpm_image.colorTable[pixel_color].m_color[1];
formatted_data[(i * (*w)) + j + 3] = 0xFF;
}
else
{
formatted_data[(i * (*w)) + j + 0] = 0xFF;
formatted_data[(i * (*w)) + j + 1] = 0xFF;
formatted_data[(i * (*w)) + j + 2] = 0xFF;
formatted_data[(i * (*w)) + j + 3] = 0xFF;
}
}
}
std::shared_ptr<Texture> texture = std::make_shared<Texture>();
texture->create(pixels.get(), width, height, VK_FORMAT_R8G8B8A8_UNORM);
texture->create(formatted_data.data(), *w, *h, VK_FORMAT_R8G8B8A8_UNORM);
TextureID id = _texture_lib.addTextureToLibrary(texture);
_texture_ids.push_back(id);
return &_texture_ids.back();
}
void* Application::newXpmTexture(std::string filename, int* w, int* h)
{
SafeXpmImage xpm_image;
if(XpmReadFileToXpmImage(filename.c_str(), &xpm_image, nullptr) != XpmSuccess)
{
error::report(e_kind::error, "XPM reader : invalid xpm file");
return nullptr;
}
*w = xpm_image.width;
*h = xpm_image.height;
std::shared_ptr<Texture> texture = std::make_shared<Texture>();
texture->create(reinterpret_cast<uint8_t*>(xpm_image.data), *w, *h, VK_FORMAT_R8G8B8A8_UNORM);
TextureID id = _texture_lib.addTextureToLibrary(texture);
_texture_ids.push_back(id);
return &_texture_ids.back();
}
#endif
void Application::destroyTexture(void* ptr)
{
vkDeviceWaitIdle(Render_Core::get().getDevice().get());

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/04/04 13:43:04 by maldavid ### ########.fr */
/* Updated: 2023/04/04 20:53:21 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,8 @@
#include <list>
#include <memory>
#include <string>
#include <vector>
#include <utility>
#include <functional>
@@ -47,7 +49,10 @@ namespace mlx::core
inline void pixelPut(void* win_ptr, int x, int y, int color) const noexcept;
void* newTexture(int w, int h);
#ifndef MLX_NO_XPM
void* newXpmTexture(std::string filename, int* w, int* h);
void* newXpmTexture(char** data, int* w, int* h);
#endif
void* newStbTexture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...)
char* mapTexture(void* img_ptr, int* bits_per_pixel, int* size_line, int* endian);
inline void texturePut(void* win_ptr, void* img, int x, int y);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/04/04 13:44:12 by maldavid ### ########.fr */
/* Updated: 2023/04/04 21:02:29 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,10 @@
#include <renderer/core/render_core.h>
#include <filesystem>
#ifndef MLX_NO_XPM
#include <X11/xpm.h>
#endif
extern "C"
{
void* mlx_init()
@@ -136,15 +140,17 @@ extern "C"
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
}
#ifndef MLX_NO_XPM
void* mlx_xpm_file_to_image(void* mlx, char* filename, int* width, int* height)
{
//return static_cast<mlx::core::Application*>(mlx)->newXpmTexture(filename, width, height);
return static_cast<mlx::core::Application*>(mlx)->newXpmTexture(std::string(filename), width, height);
}
void* mlx_xpm_to_image(void* mlx, char** xpm_data, int* width, int* height)
{
return static_cast<mlx::core::Application*>(mlx)->newXpmTexture(xpm_data, width, height);
}
#endif
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
{