mirror of
https://github.com/ZDoom/gzdoom-gles.git
synced 2025-01-18 22:51:39 +00:00
- added a new texture class using stb_image to read a more formats.
This implementation only reads GIF, BMP and PIC formats. No animated GIF support because stb_image does not handle that. PNG, JPG and TGA are still being handled by the existing dedicated implementations. PSD and HDR are impractical for reading texture data and thus are disabled. PnM could be enabled, if its identification semantics were stronger. stb_image only checks the first two characters which simply would falsely identify several flats with the right colors in the first two bytes. This is more or less a waste product of getting stb_image to work with something actually testable, so it is just provided as-is.
This commit is contained in:
parent
5cbb1c86ab
commit
5a72e0bfb7
5 changed files with 7735 additions and 2 deletions
|
@ -1048,6 +1048,7 @@ set (PCH_SOURCES
|
|||
gamedata/textures/formats/emptytexture.cpp
|
||||
gamedata/textures/formats/shadertexture.cpp
|
||||
gamedata/textures/formats/tgatexture.cpp
|
||||
gamedata/textures/formats/stbtexture.cpp
|
||||
gamedata/textures/hires/hqresize.cpp
|
||||
gamedata/textures/hires/hirestex.cpp
|
||||
gamedata/fonts/singlelumpfont.cpp
|
||||
|
|
7559
src/gamedata/textures/formats/stb_image.h
Normal file
7559
src/gamedata/textures/formats/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
173
src/gamedata/textures/formats/stbtexture.cpp
Normal file
173
src/gamedata/textures/formats/stbtexture.cpp
Normal file
|
@ -0,0 +1,173 @@
|
|||
/*
|
||||
** stbtexture.cpp
|
||||
** Texture class for reading textures with stb_image
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2019 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STBI_NO_STDIO
|
||||
// Undefine formats we do not want to support here.
|
||||
#define STBI_NO_JPEG
|
||||
#define STBI_NO_PNG
|
||||
#define STBI_NO_TGA
|
||||
#define STBI_NO_PSD
|
||||
#define STBI_NO_HDR
|
||||
#define STBI_NO_PNM
|
||||
#include "stb_image.h"
|
||||
|
||||
|
||||
#include "doomtype.h"
|
||||
#include "files.h"
|
||||
#include "w_wad.h"
|
||||
#include "v_video.h"
|
||||
#include "bitmap.h"
|
||||
#include "imagehelpers.h"
|
||||
#include "image.h"
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// A texture backed by stb_image
|
||||
// This will load GIF, BMP and PICT images.
|
||||
// PNG, JPG and TGA are still being handled by the existing dedicated implementations.
|
||||
// PSD and HDR are impractical for reading texture data and thus are disabled.
|
||||
// PnM could be enabled, if its identification semantics were stronger.
|
||||
// stb_image only checks the first two characters which simply would falsely identify several flats with the right colors in the first two bytes.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FStbTexture : public FImageSource
|
||||
{
|
||||
|
||||
public:
|
||||
FStbTexture (int lumpnum, int w, int h);
|
||||
TArray<uint8_t> CreatePalettedPixels(int conversion) override;
|
||||
int CopyPixels(FBitmap *bmp, int conversion) override;
|
||||
};
|
||||
|
||||
|
||||
static stbi_io_callbacks callbacks = {
|
||||
[](void* user,char* data,int size) -> int { return (int)reinterpret_cast<FileReader*>(user)->Read(data, size); },
|
||||
[](void* user,int n) { reinterpret_cast<FileReader*>(user)->Seek(n, FileReader::SeekCur); },
|
||||
[](void* user) -> int { return reinterpret_cast<FileReader*>(user)->Tell() >= reinterpret_cast<FileReader*>(user)->GetLength(); }
|
||||
};
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FImageSource *StbImage_TryCreate(FileReader & file, int lumpnum)
|
||||
{
|
||||
int x, y, comp;
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
int result = stbi_info_from_callbacks(&callbacks, &file, &x, &y, &comp);
|
||||
if (result == 1)
|
||||
{
|
||||
return new FStbTexture(lumpnum, x, y);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FStbTexture::FStbTexture (int lumpnum, int w, int h)
|
||||
: FImageSource(lumpnum)
|
||||
{
|
||||
Width = w;
|
||||
Height = h;
|
||||
LeftOffset = 0;
|
||||
TopOffset = 0;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
TArray<uint8_t> FStbTexture::CreatePalettedPixels(int conversion)
|
||||
{
|
||||
FBitmap bitmap;
|
||||
bitmap.Create(Width, Height);
|
||||
CopyPixels(&bitmap, conversion);
|
||||
FMemLump lump = Wads.ReadLump (SourceLump);
|
||||
const uint8_t *data = bitmap.GetPixels();
|
||||
|
||||
uint8_t *dest_p;
|
||||
int dest_adv = Height;
|
||||
int dest_rew = Width * Height - 1;
|
||||
|
||||
TArray<uint8_t> Pixels(Width*Height, true);
|
||||
dest_p = Pixels.Data();
|
||||
|
||||
bool doalpha = conversion == luminance;
|
||||
// Convert the source image from row-major to column-major format and remap it
|
||||
for (int y = Height; y != 0; --y)
|
||||
{
|
||||
for (int x = Width; x != 0; --x)
|
||||
{
|
||||
int b = *data++;
|
||||
int g = *data++;
|
||||
int r = *data++;
|
||||
int a = *data++;
|
||||
if (a < 128) *dest_p = 0;
|
||||
else *dest_p = ImageHelpers::RGBToPalette(doalpha, r, g, b);
|
||||
dest_p += dest_adv;
|
||||
}
|
||||
dest_p -= dest_rew;
|
||||
}
|
||||
return Pixels;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FStbTexture::CopyPixels(FBitmap *bmp, int conversion)
|
||||
{
|
||||
auto lump = Wads.OpenLumpReader (SourceLump);
|
||||
int x, y, chan;
|
||||
auto image = stbi_load_from_callbacks(&callbacks, &lump, &x, &y, &chan, STBI_rgb_alpha);
|
||||
if (image)
|
||||
bmp->CopyPixelDataRGB(0, 0, image, x, y, 4, x*4, 0, CF_RGBA);
|
||||
stbi_image_free(image);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -331,6 +331,7 @@ FImageSource *JPEGImage_TryCreate(FileReader &, int lumpnum);
|
|||
FImageSource *DDSImage_TryCreate(FileReader &, int lumpnum);
|
||||
FImageSource *PCXImage_TryCreate(FileReader &, int lumpnum);
|
||||
FImageSource *TGAImage_TryCreate(FileReader &, int lumpnum);
|
||||
FImageSource *StbImage_TryCreate(FileReader &, int lumpnum);
|
||||
FImageSource *RawPageImage_TryCreate(FileReader &, int lumpnum);
|
||||
FImageSource *FlatImage_TryCreate(FileReader &, int lumpnum);
|
||||
FImageSource *PatchImage_TryCreate(FileReader &, int lumpnum);
|
||||
|
@ -348,6 +349,7 @@ FImageSource * FImageSource::GetImage(int lumpnum, ETextureType usetype)
|
|||
{ JPEGImage_TryCreate, ETextureType::Any },
|
||||
{ DDSImage_TryCreate, ETextureType::Any },
|
||||
{ PCXImage_TryCreate, ETextureType::Any },
|
||||
{ StbImage_TryCreate, ETextureType::Any },
|
||||
{ TGAImage_TryCreate, ETextureType::Any },
|
||||
{ RawPageImage_TryCreate, ETextureType::MiscPatch },
|
||||
{ FlatImage_TryCreate, ETextureType::Flat },
|
||||
|
|
|
@ -945,8 +945,6 @@ void FPlayerSoundHashTable::MarkUsed()
|
|||
|
||||
static void S_ClearSoundData()
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
S_StopAllChannels();
|
||||
S_UnloadAllSounds();
|
||||
S_sfx.Clear();
|
||||
|
|
Loading…
Reference in a new issue