ericw-tools/include/common/imglib.hh

92 lines
2.7 KiB
C++

/* Copyright (C) 1996-1997 Id Software, Inc.
Copyright (C) 2017 Eric Wasylishen
This program is free software; 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 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
See file, 'COPYING', for details.
*/
#pragma once
#include <common/cmdlib.hh>
#include <common/bspfile.hh>
#include <common/qvec.hh>
#include <common/fs.hh>
namespace img
{
enum class ext
{
TGA,
WAL,
MIP
};
extern std::vector<qvec3b> palette;
// Palette
void init_palette(const gamedef_t *game);
struct texture_meta
{
std::string name;
uint32_t width, height;
ext extension;
// This member is only set before insertion into the table
// and not calculated by individual load functions.
qvec3b averageColor;
// Q2/WAL only
surfflags_t flags;
contentflags_t contents;
int32_t value;
std::string animation;
};
struct texture
{
texture_meta meta{};
std::vector<qvec4b> pixels;
};
extern std::unordered_map<std::string, texture, case_insensitive_hash, case_insensitive_equal> textures;
qvec3b calculate_average(const std::vector<qvec4b> &pixels);
const texture *find(const std::string_view &str);
// Load wal
std::optional<texture> load_wal(const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game);
// Load TGA
std::optional<texture> load_tga(const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game);
// Load Quake/Half Life mip (raw data)
std::optional<texture> load_mip(const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game);
// list of supported extensions and their loaders
constexpr struct { const char *suffix; ext id; decltype(load_wal) *loader; } extension_list[] = {
{ ".tga", ext::TGA, load_tga },
{ ".wal", ext::WAL, load_wal },
{ ".mip", ext::MIP, load_mip },
{ "", ext::MIP, load_mip }
};
// Attempt to load a texture from the specified name.
std::tuple<std::optional<texture>, fs::resolve_result, fs::data> load_texture(const std::string_view &name, bool meta_only, const gamedef_t *game, const settings::common_settings &options);
}; // namespace img