xolatilization/xormat.h

136 lines
4.4 KiB
C

/// _
/// __ _____ _ __ _ __ ___ __ _| |_
/// \ \/ / _ \| '__| '_ ` _ \ / _` | __|
/// > < (_) | | | | | | | | (_| | |_
/// /_/\_\___/|_| |_| |_| |_|\__,_|\__|
///
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
///
/// xolatile@chud.cyou - xormat - Very simple file format wrapper for things I hate but have to use anyway...
///
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
///
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
/// for more details, if you dare, it is a lot of text that nobody wants to read...
/// Description
///
/// Xormat, simple and hacky for file formats that I dislike, but everyone else uses them, so I have to use them too. I hate technology. This
/// more normal library comparing to my other ones, include this header file and use simplistic functions however you want. Don't care too much
/// about abstractions I introduced here, but keep in mind that you have to link your program correctly, this isn't stb-like library, even tho
/// it's header only. You should define a macro of which format(s) you want to use before including this file in your project.
#ifdef use_png_library
#include <xolatile/xormat/png.h>
#endif
#ifdef use_jxl_library
#include <xolatile/xormat/jxl.h>
#endif
#ifdef use_jpg_library
#include <xolatile/xormat/jpg.h>
#endif
#ifdef use_tga_library
#include <xolatile/xormat/tga.h>
#endif
/// Return image raw 32-bit colour data, in RGBA channel format by providing file path to image (with or without extension, if there's no
/// extension it's load first file found in order declared below) and two pointers to store width and height values. All images will be
/// implicitly converted into 32-bit RGBA colour, 8 bits per channel.
///
/// natural_32 * image = format_image_import ("image.png", & width, & height); /// You can omit '.png' part in file path too.
static natural_32 * format_image_import (character * path, natural * width, natural * height) {
natural_32 * data = null;
#ifdef use_png_library
if (data == null) {
character buffer [256] = "";
if (file_exists (string_concatenate (string_copy (buffer, path), ".png")) == true) {
data = png_image_import (buffer, width, height);
}
}
#endif
#ifdef use_jxl_library
if (data == null) {
character buffer [256] = "";
if (file_exists (string_concatenate (string_copy (buffer, path), ".jxl")) == true) {
data = jxl_image_import (buffer, width, height);
}
}
#endif
#ifdef use_jpg_library
if (data == null) {
character buffer [256] = "";
if (file_exists (string_concatenate (string_copy (buffer, path), ".jpg")) == true) {
data = jpg_image_import (buffer, width, height);
}
}
#endif
#ifdef use_tga_library
if (data == null) {
character buffer [256] = "";
if (file_exists (string_concatenate (string_copy (buffer, path), ".tga")) == true) {
data = tga_image_import (buffer, width, height);
}
}
#endif
if (data == null) {
switch (file_type (path)) {
#ifdef use_png_library
case (file_type_png_image): {
if (file_exists (path) == true) {
data = png_image_import (path, width, height);
} else {
print ("/w File '/3%s/-' doesn't exist.\n", path);
}
} break;
#endif
#ifdef use_jxl_library
case (file_type_jxl_image): {
if (file_exists (path) == true) {
data = jxl_image_import (path, width, height);
} else {
print ("/w File '/3%s/-' doesn't exist.\n", path);
}
} break;
#endif
#ifdef use_jpg_library
case (file_type_jpg_image): {
if (file_exists (path) == true) {
data = jpg_image_import (path, width, height);
} else {
print ("/w File '/3%s/-' doesn't exist.\n", path);
}
} break;
#endif
#ifdef use_tga_library
case (file_type_tga_image): {
if (file_exists (path) == true) {
data = tga_image_import (path, width, height);
} else {
print ("/w File '/3%s/-' doesn't exist.\n", path);
}
} break;
#endif
default: {
print ("/w File '/3%s/-' doesn't exist or file type isn't supported.\n", path);
} break;
}
}
return (data);
}