raze-gles/source/common/textures/formats/stbtexture.cpp
Christoph Oelckers 4a866b0320 - use stb_image to read the image formats supported by kplib but not by GZDoom.
(No, sorry, kplib must go - a utility library like that being utterly dependent on a multitude of global variables is a no-go.)
2019-10-06 08:48:07 +02:00

128 lines
No EOL
4.3 KiB
C++

/*
** 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 "files.h"
#include "bitmap.h"
#include "image.h"
#include "cache1d.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 w, int h);
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 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(x, y);
}
return nullptr;
}
//==========================================================================
//
//
//
//==========================================================================
FStbTexture::FStbTexture (int w, int h)
{
Width = w;
Height = h;
LeftOffset = 0;
TopOffset = 0;
}
//==========================================================================
//
//
//
//==========================================================================
int FStbTexture::CopyPixels(FBitmap *bmp, int conversion)
{
auto lump = kopenFileReader(Name, 0);
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;
}