textures: Implement animated gif decoding using libnsgif

This commit is contained in:
Joel16 2020-09-13 17:24:34 -04:00
parent 102c7c0154
commit e22a9ffe58
7 changed files with 142 additions and 418 deletions

View File

@ -39,8 +39,8 @@ set(VITA_MKSFOEX_FLAGS "${VITA_MKSFOEX_FLAGS} -d PARENTAL_LEVEL=1")
# Add any additional include paths here
include_directories(
libs/imgui
libs/giflib
libs/libnsbmp
libs/libnsgif
libs/libtiff
libs/turbojpeg
libs
@ -63,6 +63,8 @@ add_executable(${PROJECT_NAME}
libs/imgui/imgui_vita_touch.cpp
libs/imgui/imgui_widgets.cpp
libs/libnsbmp/libnsbmp.c
libs/libnsgif/libnsgif.c
libs/libnsgif/lzw.c
source/fs.cpp
source/gui.cpp
source/keyboard.cpp
@ -76,7 +78,6 @@ add_executable(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
tiff
webp
gif
turbojpeg
jpeg
png

View File

@ -1,13 +1,15 @@
#ifndef _VITALBUM_TEXTURES_H_
#define _VITALBUM_TEXTURES_H_
#include <vitaGL.h>
#include <string>
#include <vector>
#include <vitaGL.h>
typedef struct {
GLuint id = 0;
int width = 0;
int height = 0;
int delay = 0;
} Tex;
extern Tex folder_texture, file_texture, image_texture;
@ -15,7 +17,7 @@ extern Tex folder_texture, file_texture, image_texture;
namespace Textures {
bool LoadImageFile(const std::string &path, Tex *texture);
bool LoadImageBMP(const std::string &path, Tex *texture);
bool LoadImageGIF(const std::string &path, Tex *texture, unsigned int *frames);
bool LoadImageGIF(const std::string &path, std::vector<Tex> &textures);
bool LoadImageICO(const std::string &path, Tex *texture);
bool LoadImageJPEG(const std::string &path, Tex *texture);
bool LoadImagePCX(const std::string &path, Tex *texture);

View File

@ -1,19 +0,0 @@
The GIFLIB distribution is Copyright (c) 1997 Eric S. Raymond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -1,312 +0,0 @@
/******************************************************************************
gif_lib.h - service library for decoding and encoding GIF images
*****************************************************************************/
#ifndef _GIF_LIB_H_
#define _GIF_LIB_H_ 1
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define GIFLIB_MAJOR 5
#define GIFLIB_MINOR 1
#define GIFLIB_RELEASE 4
#define GIF_ERROR 0
#define GIF_OK 1
#include <stddef.h>
#include <stdbool.h>
#define GIF_STAMP "GIFVER" /* First chars in file - GIF stamp. */
#define GIF_STAMP_LEN sizeof(GIF_STAMP) - 1
#define GIF_VERSION_POS 3 /* Version first character in stamp. */
#define GIF87_STAMP "GIF87a" /* First chars in file - GIF stamp. */
#define GIF89_STAMP "GIF89a" /* First chars in file - GIF stamp. */
typedef unsigned char GifPixelType;
typedef unsigned char *GifRowType;
typedef unsigned char GifByteType;
typedef unsigned int GifPrefixType;
typedef int GifWord;
typedef struct GifColorType {
GifByteType Red, Green, Blue;
} GifColorType;
typedef struct ColorMapObject {
int ColorCount;
int BitsPerPixel;
bool SortFlag;
GifColorType *Colors; /* on malloc(3) heap */
} ColorMapObject;
typedef struct GifImageDesc {
GifWord Left, Top, Width, Height; /* Current image dimensions. */
bool Interlace; /* Sequential/Interlaced lines. */
ColorMapObject *ColorMap; /* The local color map */
} GifImageDesc;
typedef struct ExtensionBlock {
int ByteCount;
GifByteType *Bytes; /* on malloc(3) heap */
int Function; /* The block function code */
#define CONTINUE_EXT_FUNC_CODE 0x00 /* continuation subblock */
#define COMMENT_EXT_FUNC_CODE 0xfe /* comment */
#define GRAPHICS_EXT_FUNC_CODE 0xf9 /* graphics control (GIF89) */
#define PLAINTEXT_EXT_FUNC_CODE 0x01 /* plaintext */
#define APPLICATION_EXT_FUNC_CODE 0xff /* application block */
} ExtensionBlock;
typedef struct SavedImage {
GifImageDesc ImageDesc;
GifByteType *RasterBits; /* on malloc(3) heap */
int ExtensionBlockCount; /* Count of extensions before image */
ExtensionBlock *ExtensionBlocks; /* Extensions before image */
} SavedImage;
typedef struct GifFileType {
GifWord SWidth, SHeight; /* Size of virtual canvas */
GifWord SColorResolution; /* How many colors can we generate? */
GifWord SBackGroundColor; /* Background color for virtual canvas */
GifByteType AspectByte; /* Used to compute pixel aspect ratio */
ColorMapObject *SColorMap; /* Global colormap, NULL if nonexistent. */
int ImageCount; /* Number of current image (both APIs) */
GifImageDesc Image; /* Current image (low-level API) */
SavedImage *SavedImages; /* Image sequence (high-level API) */
int ExtensionBlockCount; /* Count extensions past last image */
ExtensionBlock *ExtensionBlocks; /* Extensions past last image */
int Error; /* Last error condition reported */
void *UserData; /* hook to attach user data (TVT) */
void *Private; /* Don't mess with this! */
} GifFileType;
#define GIF_ASPECT_RATIO(n) ((n)+15.0/64.0)
typedef enum {
UNDEFINED_RECORD_TYPE,
SCREEN_DESC_RECORD_TYPE,
IMAGE_DESC_RECORD_TYPE, /* Begin with ',' */
EXTENSION_RECORD_TYPE, /* Begin with '!' */
TERMINATE_RECORD_TYPE /* Begin with ';' */
} GifRecordType;
/* func type to read gif data from arbitrary sources (TVT) */
typedef int (*InputFunc) (GifFileType *, GifByteType *, int);
/* func type to write gif data to arbitrary targets.
* Returns count of bytes written. (MRB)
*/
typedef int (*OutputFunc) (GifFileType *, const GifByteType *, int);
/******************************************************************************
GIF89 structures
******************************************************************************/
typedef struct GraphicsControlBlock {
int DisposalMode;
#define DISPOSAL_UNSPECIFIED 0 /* No disposal specified. */
#define DISPOSE_DO_NOT 1 /* Leave image in place */
#define DISPOSE_BACKGROUND 2 /* Set area too background color */
#define DISPOSE_PREVIOUS 3 /* Restore to previous content */
bool UserInputFlag; /* User confirmation required before disposal */
int DelayTime; /* pre-display delay in 0.01sec units */
int TransparentColor; /* Palette index for transparency, -1 if none */
#define NO_TRANSPARENT_COLOR -1
} GraphicsControlBlock;
/******************************************************************************
GIF encoding routines
******************************************************************************/
/* Main entry points */
GifFileType *EGifOpenFileName(const char *GifFileName,
const bool GifTestExistence, int *Error);
GifFileType *EGifOpenFileHandle(const int GifFileHandle, int *Error);
GifFileType *EGifOpen(void *userPtr, OutputFunc writeFunc, int *Error);
int EGifSpew(GifFileType * GifFile);
const char *EGifGetGifVersion(GifFileType *GifFile); /* new in 5.x */
int EGifCloseFile(GifFileType *GifFile, int *ErrorCode);
#define E_GIF_SUCCEEDED 0
#define E_GIF_ERR_OPEN_FAILED 1 /* And EGif possible errors. */
#define E_GIF_ERR_WRITE_FAILED 2
#define E_GIF_ERR_HAS_SCRN_DSCR 3
#define E_GIF_ERR_HAS_IMAG_DSCR 4
#define E_GIF_ERR_NO_COLOR_MAP 5
#define E_GIF_ERR_DATA_TOO_BIG 6
#define E_GIF_ERR_NOT_ENOUGH_MEM 7
#define E_GIF_ERR_DISK_IS_FULL 8
#define E_GIF_ERR_CLOSE_FAILED 9
#define E_GIF_ERR_NOT_WRITEABLE 10
/* These are legacy. You probably do not want to call them directly */
int EGifPutScreenDesc(GifFileType *GifFile,
const int GifWidth, const int GifHeight,
const int GifColorRes,
const int GifBackGround,
const ColorMapObject *GifColorMap);
int EGifPutImageDesc(GifFileType *GifFile,
const int GifLeft, const int GifTop,
const int GifWidth, const int GifHeight,
const bool GifInterlace,
const ColorMapObject *GifColorMap);
void EGifSetGifVersion(GifFileType *GifFile, const bool gif89);
int EGifPutLine(GifFileType *GifFile, GifPixelType *GifLine,
int GifLineLen);
int EGifPutPixel(GifFileType *GifFile, const GifPixelType GifPixel);
int EGifPutComment(GifFileType *GifFile, const char *GifComment);
int EGifPutExtensionLeader(GifFileType *GifFile, const int GifExtCode);
int EGifPutExtensionBlock(GifFileType *GifFile,
const int GifExtLen, const void *GifExtension);
int EGifPutExtensionTrailer(GifFileType *GifFile);
int EGifPutExtension(GifFileType *GifFile, const int GifExtCode,
const int GifExtLen,
const void *GifExtension);
int EGifPutCode(GifFileType *GifFile, int GifCodeSize,
const GifByteType *GifCodeBlock);
int EGifPutCodeNext(GifFileType *GifFile,
const GifByteType *GifCodeBlock);
/******************************************************************************
GIF decoding routines
******************************************************************************/
/* Main entry points */
GifFileType *DGifOpenFileName(const char *GifFileName, int *Error);
GifFileType *DGifOpenFileHandle(int GifFileHandle, int *Error);
int DGifSlurp(GifFileType * GifFile);
GifFileType *DGifOpen(void *userPtr, InputFunc readFunc, int *Error); /* new one (TVT) */
int DGifCloseFile(GifFileType * GifFile, int *ErrorCode);
#define D_GIF_SUCCEEDED 0
#define D_GIF_ERR_OPEN_FAILED 101 /* And DGif possible errors. */
#define D_GIF_ERR_READ_FAILED 102
#define D_GIF_ERR_NOT_GIF_FILE 103
#define D_GIF_ERR_NO_SCRN_DSCR 104
#define D_GIF_ERR_NO_IMAG_DSCR 105
#define D_GIF_ERR_NO_COLOR_MAP 106
#define D_GIF_ERR_WRONG_RECORD 107
#define D_GIF_ERR_DATA_TOO_BIG 108
#define D_GIF_ERR_NOT_ENOUGH_MEM 109
#define D_GIF_ERR_CLOSE_FAILED 110
#define D_GIF_ERR_NOT_READABLE 111
#define D_GIF_ERR_IMAGE_DEFECT 112
#define D_GIF_ERR_EOF_TOO_SOON 113
/* These are legacy. You probably do not want to call them directly */
int DGifGetScreenDesc(GifFileType *GifFile);
int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType);
int DGifGetImageDesc(GifFileType *GifFile);
int DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
int DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel);
int DGifGetComment(GifFileType *GifFile, char *GifComment);
int DGifGetExtension(GifFileType *GifFile, int *GifExtCode,
GifByteType **GifExtension);
int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension);
int DGifGetCode(GifFileType *GifFile, int *GifCodeSize,
GifByteType **GifCodeBlock);
int DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock);
int DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
/******************************************************************************
Color table quantization (deprecated)
******************************************************************************/
int GifQuantizeBuffer(unsigned int Width, unsigned int Height,
int *ColorMapSize, GifByteType * RedInput,
GifByteType * GreenInput, GifByteType * BlueInput,
GifByteType * OutputBuffer,
GifColorType * OutputColorMap);
/******************************************************************************
Error handling and reporting.
******************************************************************************/
extern const char *GifErrorString(int ErrorCode); /* new in 2012 - ESR */
/*****************************************************************************
Everything below this point is new after version 1.2, supporting `slurp
mode' for doing I/O in two big belts with all the image-bashing in core.
******************************************************************************/
/******************************************************************************
Color map handling from gif_alloc.c
******************************************************************************/
extern ColorMapObject *GifMakeMapObject(int ColorCount,
const GifColorType *ColorMap);
extern void GifFreeMapObject(ColorMapObject *Object);
extern ColorMapObject *GifUnionColorMap(const ColorMapObject *ColorIn1,
const ColorMapObject *ColorIn2,
GifPixelType ColorTransIn2[]);
extern int GifBitSize(int n);
extern void *
reallocarray(void *optr, size_t nmemb, size_t size);
/******************************************************************************
Support for the in-core structures allocation (slurp mode).
******************************************************************************/
extern void GifApplyTranslation(SavedImage *Image, GifPixelType Translation[]);
extern int GifAddExtensionBlock(int *ExtensionBlock_Count,
ExtensionBlock **ExtensionBlocks,
int Function,
unsigned int Len, unsigned char ExtData[]);
extern void GifFreeExtensions(int *ExtensionBlock_Count,
ExtensionBlock **ExtensionBlocks);
extern SavedImage *GifMakeSavedImage(GifFileType *GifFile,
const SavedImage *CopyFrom);
extern void GifFreeSavedImages(GifFileType *GifFile);
/******************************************************************************
5.x functions for GIF89 graphics control blocks
******************************************************************************/
int DGifExtensionToGCB(const size_t GifExtensionLength,
const GifByteType *GifExtension,
GraphicsControlBlock *GCB);
size_t EGifGCBToExtension(const GraphicsControlBlock *GCB,
GifByteType *GifExtension);
int DGifSavedExtensionToGCB(GifFileType *GifFile,
int ImageIndex,
GraphicsControlBlock *GCB);
int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB,
GifFileType *GifFile,
int ImageIndex);
/******************************************************************************
The library's internal utility font
******************************************************************************/
#define GIF_FONT_WIDTH 8
#define GIF_FONT_HEIGHT 8
extern const unsigned char GifAsciiTable8x8[][GIF_FONT_WIDTH];
extern void GifDrawText8x8(SavedImage *Image,
const int x, const int y,
const char *legend, const int color);
extern void GifDrawBox(SavedImage *Image,
const int x, const int y,
const int w, const int d, const int color);
extern void GifDrawRectangle(SavedImage *Image,
const int x, const int y,
const int w, const int d, const int color);
extern void GifDrawBoxedText8x8(SavedImage *Image,
const int x, const int y,
const char *legend,
const int border, const int bg, const int fg);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _GIF_LIB_H */
/* end */

Binary file not shown.

View File

@ -2,11 +2,13 @@
#include <vitaGL.h>
#include "fs.h"
#include "imgui_internal.h"
#include "keyboard.h"
#include "textures.h"
#include "utils.h"
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui_internal.h"
namespace Renderer {
static void Start(void) {
vglStartRendering();
@ -21,6 +23,17 @@ namespace Renderer {
ImGui_ImplVitaGL_RenderDrawData(ImGui::GetDrawData());
vglStopRendering();
}
void SetupWindow(void) {
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(960.0f, 544.0f), ImGuiCond_Once);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
}
void ExitWindow(void) {
ImGui::End();
ImGui::PopStyleVar();
}
}
namespace GUI {
@ -30,31 +43,37 @@ namespace GUI {
GUI_STATE_GIF_PREVIEW
};
static int gui_state = GUI_STATE_HOME;
static int frame_count = 0;
static void ImageWindow(SceIoDirent *entry, Tex *texture) {
ImGui::SetNextWindowPos({0.0f, 0.0f}, ImGuiCond_Once);
ImGui::SetNextWindowSize({960.0f, 544.0f}, ImGuiCond_Once);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
Renderer::SetupWindow();
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
if (ImGui::Begin(entry->d_name, nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse))
if (ImGui::Begin(entry->d_name, nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar)) {
ImGui::SetCursorPos((ImGui::GetWindowSize() - ImVec2(texture->width, texture->height)) * 0.5f);
ImGui::Image(reinterpret_cast<ImTextureID>(texture->id), ImVec2(texture->width, texture->height));
}
ImGui::End();
Renderer::ExitWindow();
ImGui::PopStyleVar();
}
static void GifWindow(SceIoDirent *entry, Tex **texture, unsigned int *frames) {
ImGui::SetNextWindowPos({0.0f, 0.0f}, ImGuiCond_Once);
ImGui::SetNextWindowSize({960.0f, 544.0f}, ImGuiCond_Once);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
if (ImGui::Begin(entry->d_name, nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) {
for (unsigned int i = 0; i < *frames; i++) {
//ImGui::SetCursorPosY(1.0f);
ImGui::Image(reinterpret_cast<ImTextureID>(texture[i]->id), ImVec2(texture[i]->width, texture[i]->height));
}
static void GifWindow(SceIoDirent *entry, std::vector<Tex> textures) {
Renderer::SetupWindow();
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
if (ImGui::Begin(entry->d_name, nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar)) {
frame_count++;
ImGui::SetCursorPos((ImGui::GetWindowSize() - ImVec2(textures[frame_count].width, textures[frame_count].height)) * 0.5f);
sceKernelDelayThread(textures[frame_count].delay * 10000);
ImGui::Image(reinterpret_cast<ImTextureID>(textures[frame_count].id), ImVec2(textures[frame_count].width, textures[frame_count].height));
// Reset frame counter
if (frame_count == textures.size() - 1)
frame_count = 0;
}
ImGui::End();
Renderer::ExitWindow();
ImGui::PopStyleVar();
}
@ -139,15 +158,13 @@ namespace GUI {
SceOff selected = 0;
Tex texture; // Common image formats
Tex *textures; // GIFs
unsigned int frames = 0; // GIFs
std::vector<Tex> textures; // GIFs
int gui_state = GUI_STATE_HOME;
while (window) {
Renderer::Start();
ImGui::SetNextWindowPos({0.0f, 0.0f}, ImGuiCond_Once);
ImGui::SetNextWindowSize({960.0f, 544.0f}, ImGuiCond_Once);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
Renderer::SetupWindow();
if (ImGui::Begin("VITAlbum", &window, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) {
ImGui::TextColored(ImVec4(1.00f, 1.00f, 1.00f, 1.00f), FS::CWD.c_str());
@ -194,12 +211,9 @@ namespace GUI {
gui_state = GUI_STATE_IMAGE_PREVIEW;
}
else if (ext == ".GIF") {
// SceBool image_ret = Textures::LoadImageGIF(path, &textures, &frames);
// IM_ASSERT(image_ret);
// gui_state = GUI_STATE_GIF_PREVIEW;
SceBool image_ret = Textures::LoadImageGIF(path, &texture, &frames);
SceBool image_ret = Textures::LoadImageGIF(path, textures);
IM_ASSERT(image_ret);
gui_state = GUI_STATE_IMAGE_PREVIEW;
gui_state = GUI_STATE_GIF_PREVIEW;
}
else if (ext == ".ICO") {
SceBool image_ret = Textures::LoadImageICO(path, &texture);
@ -248,15 +262,14 @@ namespace GUI {
break;
case GUI_STATE_GIF_PREVIEW:
GUI::GifWindow(&entries[selected], &textures, &frames);
GUI::GifWindow(&entries[selected], textures);
break;
default:
break;
}
ImGui::End();
ImGui::PopStyleVar();
Renderer::ExitWindow();
Renderer::End(clear_color);
Utils::ReadControls();
@ -281,9 +294,11 @@ namespace GUI {
case GUI_STATE_GIF_PREVIEW:
if (pressed & SCE_CTRL_CANCEL) {
for (int i = 0; i < frames; i++) {
for (int i = 0; i < textures.size(); i++) {
Textures::Free(&textures[i]);
}
textures.clear();
frame_count = 0;
gui_state = GUI_STATE_HOME;
}

View File

@ -4,7 +4,7 @@
#include "libnsbmp.h"
// GIF
#include "gif_lib.h"
#include "libnsgif.h"
// JPEG
#include "turbojpeg.h"
@ -73,6 +73,44 @@ namespace BMP {
}
}
namespace GIF {
static void *bitmap_create(int width, int height) {
/* ensure a stupidly large bitmap is not created */
if ((static_cast<long long>(width) * static_cast<long long>(height)) > (MAX_IMAGE_BYTES/BYTES_PER_PIXEL))
return nullptr;
return std::calloc(width * height, BYTES_PER_PIXEL);
}
static void bitmap_set_opaque(void *bitmap, bool opaque) {
(void) opaque; /* unused */
(void) bitmap; /* unused */
assert(bitmap);
}
static bool bitmap_test_opaque(void *bitmap) {
(void) bitmap; /* unused */
assert(bitmap);
return false;
}
static unsigned char *bitmap_get_buffer(void *bitmap) {
assert(bitmap);
return static_cast<unsigned char *>(bitmap);
}
static void bitmap_destroy(void *bitmap) {
assert(bitmap);
free(bitmap);
}
static void bitmap_modified(void *bitmap) {
(void) bitmap; /* unused */
assert(bitmap);
return;
}
}
namespace ICO {
static void *bitmap_create(int width, int height, unsigned int state) {
(void) state; /* unused */
@ -175,68 +213,67 @@ namespace Textures {
return ret;
}
bool LoadImageGIF(const std::string &path, Tex *texture, unsigned int *frames) {
bool LoadImageGIF(const std::string &path, std::vector<Tex> &textures) {
gif_bitmap_callback_vt bitmap_callbacks = {
GIF::bitmap_create,
GIF::bitmap_destroy,
GIF::bitmap_get_buffer,
GIF::bitmap_set_opaque,
GIF::bitmap_test_opaque,
GIF::bitmap_modified
};
bool ret = false;
int error = 0;
GifFileType *gif = DGifOpenFileName(path.c_str(), &error);
if (!gif)
return false;
gif_animation gif;
SceOff size = 0;
gif_result code = GIF_OK;
unsigned char *data = nullptr;
if (DGifSlurp(gif) == GIF_ERROR) {
DGifCloseFile(gif, &error);
return false;
}
gif_create(&gif, &bitmap_callbacks);
if (R_FAILED(FS::ReadFile(path, &data, &size)))
return ret;
do {
code = gif_initialise(&gif, size, data);
if (code != GIF_OK && code != GIF_WORKING) {
gif_finalise(&gif);
delete[] data;
return ret;
}
} while (code != GIF_OK);
bool gif_is_animated = gif.frame_count > 1;
//*frames = gif->ImageCount;
*frames = 0;
GraphicsControlBlock gcb;
if (gif_is_animated) {
textures.resize(gif.frame_count);
if (*frames > 1) {
for(int i = 0; i < *frames; i++) {
DGifSavedExtensionToGCB(gif, i, &gcb);
int pixels = gif->SavedImages[i].ImageDesc.Width * gif->SavedImages[i].ImageDesc.Height;
unsigned char *buffer = new unsigned char[pixels * 4];
unsigned char *image = buffer;
for (int j = 0; j < pixels; j++) {
GifByteType byte = gif->SavedImages[i].RasterBits[j];
GifColorType colour = gif->SColorMap->Colors[byte];
buffer[4 * j + 0] = colour.Red;
buffer[4 * j + 1] = colour.Green;
buffer[4 * j + 2] = colour.Blue;
buffer[4 * j + 3] = (byte == gcb.TransparentColor) ? 0 : 255;
for (unsigned int i = 0; i < gif.frame_count; i++) {
code = gif_decode_frame(&gif, i);
if (code != GIF_OK) {
delete[] data;
return false;
}
texture->width = gif->SWidth;
texture->height = gif->SHeight;
//ret = Textures::LoadImage(image, GL_RGBA, &texture[i], nullptr);
ret = Textures::LoadImage(image, GL_RGBA, texture, nullptr);
delete[] image;
textures[i].width = gif.width;
textures[i].height = gif.height;
textures[i].delay = gif.frames->frame_delay;
ret = Textures::LoadImage(static_cast<unsigned char *>(gif.frame_image), GL_RGBA, &textures[i], nullptr);
}
}
else {
// Get the first frame
DGifSavedExtensionToGCB(gif, 0, &gcb);
int pixels = gif->SavedImages[0].ImageDesc.Width * gif->SavedImages[0].ImageDesc.Height;
unsigned char *buffer = new unsigned char[pixels * 4];
unsigned char *image = buffer;
for (int i = 0; i < pixels; i++) {
GifByteType byte = gif->SavedImages[0].RasterBits[i];
GifColorType colour = gif->SColorMap->Colors[byte];
buffer[4 * i + 0] = colour.Red;
buffer[4 * i + 1] = colour.Green;
buffer[4 * i + 2] = colour.Blue;
buffer[4 * i + 3] = (byte == gcb.TransparentColor) ? 0 : 255;
code = gif_decode_frame(&gif, 0);
if (code != GIF_OK) {
delete[] data;
return false;
}
texture->width = gif->SWidth;
texture->height = gif->SHeight;
ret = Textures::LoadImage(image, GL_RGBA, texture, nullptr);
delete[] image;
textures[0].width = gif.width;
textures[0].height = gif.height;
ret = Textures::LoadImage(static_cast<unsigned char *>(gif.frame_image), GL_RGBA, &textures[0], nullptr);
}
DGifCloseFile(gif, &error);
gif_finalise(&gif);
delete[] data;
return ret;
}