Skip to content

Commit

Permalink
Resources: Use libgui's TGA loader
Browse files Browse the repository at this point in the history
libdoomsday had the old TGA loader that works with FS1. That is now replaced with a call to
de::Image::fromData() that uses Qt for now, and stb_image in the future. libgui's TGA loader can
read RLE-formatted images unlike the old TGA loader.

libgui's TGA loader was modified to allow reading colormapped 8-bit images.

IssueID #2381
  • Loading branch information
skyjake committed Dec 26, 2019
1 parent c738884 commit 5f3810c
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 230 deletions.
@@ -1,6 +1,6 @@
/** @file tga.h Truevision TGA (a.k.a Targa) image reader
*
* @authors Copyright © 2003-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2003-2019 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2009-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
Expand All @@ -18,17 +18,13 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDOOMSDAY_RESOURCE_TGA_H
#define LIBDOOMSDAY_RESOURCE_TGA_H
#pragma once

#ifdef __cplusplus

#include <doomsday/filesys/filehandle.h>
#include <de/Vector>

/// @addtogroup resource
///@{

/**
* Loads a 24-bit or a 32-bit image (24-bit color + 8-bit alpha).
*
Expand All @@ -37,15 +33,6 @@
*
* @return Non-zero iff the image is loaded successfully.
*/
LIBDOOMSDAY_PUBLIC uint8_t *TGA_Load(de::FileHandle &file, de::Vector2ui &outSize, int &pixelSize);

/**
* @return Textual message detailing the last error encountered else @c 0.
*/
LIBDOOMSDAY_PUBLIC char const *TGA_LastError();

///@}
uint8_t *TGA_Load(de::FileHandle &file, de::Vector2ui &outSize, int &pixelSize);

#endif // __cplusplus

#endif // LIBDOOMSDAY_RESOURCE_TGA_H
18 changes: 9 additions & 9 deletions doomsday/apps/client/src/resource/image.cpp
Expand Up @@ -20,6 +20,7 @@

#include "de_platform.h"
#include "resource/image.h"
#include "resource/tga.h"

#include <doomsday/resource/patch.h>
#include <doomsday/resource/colorpalettes.h>
Expand All @@ -35,7 +36,6 @@

#include <doomsday/res/Composite>
#include <doomsday/resource/pcx.h>
#include <doomsday/resource/tga.h>

#include "gl/gl_tex.h"

Expand All @@ -58,7 +58,7 @@ struct GraphicFileType

bool (*interpretFunc)(FileHandle &hndl, String filePath, image_t &img);

char const *(*getLastErrorFunc)(); ///< Can be NULL.
//char const *(*getLastErrorFunc)(); ///< Can be NULL.
};

static bool interpretPcx(FileHandle &hndl, String /*filePath*/, image_t &img)
Expand All @@ -82,16 +82,16 @@ static bool interpretTga(FileHandle &hndl, String /*filePath*/, image_t &img)
{
Image_Init(img);
img.pixels = TGA_Load(hndl, img.size, img.pixelSize);
return (0 != img.pixels);
return img.pixels != nullptr;
}

// Graphic resource types.
static GraphicFileType const graphicTypes[] = {
{ "PNG", "png", interpretPng, 0 },
{ "JPG", "jpg", interpretJpg, 0 }, // TODO: add alternate "jpeg" extension
{ "TGA", "tga", interpretTga, TGA_LastError },
{ "PCX", "pcx", interpretPcx, PCX_LastError },
{ "", "", 0, 0 } // Terminate.
{ "PNG", "png", interpretPng /*, 0*/ },
{ "JPG", "jpg", interpretJpg /*, 0*/ }, // TODO: add alternate "jpeg" extension
{ "TGA", "tga", interpretTga /*, TGA_LastError*/ },
{ "PCX", "pcx", interpretPcx /*, PCX_LastError*/ },
{ "", "", nullptr /*, 0*/ } // Terminate.
};

static GraphicFileType const *guessGraphicFileTypeFromFileName(String fileName)
Expand All @@ -109,7 +109,7 @@ static GraphicFileType const *guessGraphicFileTypeFromFileName(String fileName)
}
}
}
return 0; // Unknown.
return nullptr; // Unknown.
}

static void interpretGraphic(FileHandle &hndl, String filePath, image_t &img)
Expand Down
60 changes: 60 additions & 0 deletions doomsday/apps/client/src/resource/tga.cpp
@@ -0,0 +1,60 @@
/** @file tga.cpp Truevision TGA (a.k.a Targa) image reader
*
* @authors Copyright © 2003-2019 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2009-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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, see:
* http://www.gnu.org/licenses</small>
*/

#include "resource/tga.h"
#include "dd_share.h"

#include <de/memory.h>
#include <de/Image>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace de;

uint8_t *TGA_Load(FileHandle &file, Vector2ui &outSize, int &pixelSize)
{
const size_t initPos = file.tell();

// Read the file into a memory buffer.
Block fileContents(file.length() - initPos);
file.read(fileContents.data(), fileContents.size());
file.seek(initPos, SeekSet);

Image img = Image::fromData(fileContents, ".tga");
if (img.isNull())
{
return nullptr;
}

// De-index colormapped images.
if (img.toQImage().format() == QImage::Format_Indexed8)
{
img = img.toQImage().convertToFormat(QImage::Format_RGBA8888);
}

outSize = img.size();
pixelSize = img.depth() / 8;

// Return a copy of the pixel data.
const size_t numBytes = size_t(img.byteCount());
uint8_t * retBuf = reinterpret_cast<uint8_t *>(M_Malloc(numBytes));
memcpy(retBuf, img.bits(), numBytes);
return retBuf;
}
202 changes: 0 additions & 202 deletions doomsday/apps/libdoomsday/src/resource/tga.cpp

This file was deleted.

0 comments on commit 5f3810c

Please sign in to comment.