Util: Move some image stuff around

This commit is contained in:
Vicki Pfau 2023-03-20 03:43:44 -07:00
parent e3fbb55854
commit 603c1800d5
20 changed files with 195 additions and 179 deletions

167
include/mgba-util/image.h Normal file
View File

@ -0,0 +1,167 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef M_IMAGE_H
#define M_IMAGE_H
#include <mgba-util/common.h>
CXX_GUARD_START
#ifdef COLOR_16_BIT
typedef uint16_t color_t;
#define BYTES_PER_PIXEL 2
#else
typedef uint32_t color_t;
#define BYTES_PER_PIXEL 4
#endif
#define M_R5(X) ((X) & 0x1F)
#define M_G5(X) (((X) >> 5) & 0x1F)
#define M_B5(X) (((X) >> 10) & 0x1F)
#define M_R8(X) (((((X) << 3) & 0xF8) * 0x21) >> 2)
#define M_G8(X) (((((X) >> 2) & 0xF8) * 0x21) >> 2)
#define M_B8(X) (((((X) >> 7) & 0xF8) * 0x21) >> 2)
#define M_RGB5_TO_BGR8(X) ((M_R5(X) << 3) | (M_G5(X) << 11) | (M_B5(X) << 19))
#define M_RGB5_TO_RGB8(X) ((M_R5(X) << 19) | (M_G5(X) << 11) | (M_B5(X) << 3))
#define M_RGB8_TO_BGR5(X) ((((X) & 0xF8) >> 3) | (((X) & 0xF800) >> 6) | (((X) & 0xF80000) >> 9))
#define M_RGB8_TO_RGB5(X) ((((X) & 0xF8) << 7) | (((X) & 0xF800) >> 6) | (((X) & 0xF80000) >> 19))
#ifndef COLOR_16_BIT
#define M_COLOR_RED 0x000000FF
#define M_COLOR_GREEN 0x0000FF00
#define M_COLOR_BLUE 0x00FF0000
#define M_COLOR_ALPHA 0xFF000000
#define M_COLOR_WHITE 0x00FFFFFF
#define M_RGB8_TO_NATIVE(X) (((X) & 0x00FF00) | (((X) & 0x0000FF) << 16) | (((X) & 0xFF0000) >> 16))
#elif defined(COLOR_5_6_5)
#define M_COLOR_RED 0x001F
#define M_COLOR_GREEN 0x07E0
#define M_COLOR_BLUE 0xF800
#define M_COLOR_ALPHA 0x0000
#define M_COLOR_WHITE 0xFFDF
#define M_RGB8_TO_NATIVE(X) ((((X) & 0xF8) << 8) | (((X) & 0xFC00) >> 5) | (((X) & 0xF80000) >> 19))
#else
#define M_COLOR_RED 0x001F
#define M_COLOR_GREEN 0x03E0
#define M_COLOR_BLUE 0x7C00
#define M_COLOR_ALPHA 0x1000
#define M_COLOR_WHITE 0x7FFF
#define M_RGB8_TO_NATIVE(X) M_RGB8_TO_BGR5(X)
#endif
enum mColorFormat {
mCOLOR_XBGR8 = 0x00001,
mCOLOR_XRGB8 = 0x00002,
mCOLOR_BGRX8 = 0x00004,
mCOLOR_RGBX8 = 0x00008,
mCOLOR_ABGR8 = 0x00010,
mCOLOR_ARGB8 = 0x00020,
mCOLOR_BGRA8 = 0x00040,
mCOLOR_RGBA8 = 0x00080,
mCOLOR_RGB5 = 0x00100,
mCOLOR_BGR5 = 0x00200,
mCOLOR_RGB565 = 0x00400,
mCOLOR_BGR565 = 0x00800,
mCOLOR_ARGB5 = 0x01000,
mCOLOR_ABGR5 = 0x02000,
mCOLOR_RGBA5 = 0x04000,
mCOLOR_BGRA5 = 0x08000,
mCOLOR_RGB8 = 0x10000,
mCOLOR_BGR8 = 0x20000,
mCOLOR_L8 = 0x40000,
mCOLOR_ANY = -1
};
#ifndef PYCPARSE
static inline color_t mColorFrom555(uint16_t value) {
#ifdef COLOR_16_BIT
#ifdef COLOR_5_6_5
color_t color = 0;
color |= (value & 0x001F) << 11;
color |= (value & 0x03E0) << 1;
color |= (value & 0x7C00) >> 10;
#else
color_t color = value;
#endif
#else
color_t color = M_RGB5_TO_BGR8(value);
color |= (color >> 5) & 0x070707;
#endif
return color;
}
ATTRIBUTE_UNUSED static unsigned mColorMix5Bit(int weightA, unsigned colorA, int weightB, unsigned colorB) {
unsigned c = 0;
unsigned a, b;
#ifdef COLOR_16_BIT
#ifdef COLOR_5_6_5
a = colorA & 0xF81F;
b = colorB & 0xF81F;
a |= (colorA & 0x7C0) << 16;
b |= (colorB & 0x7C0) << 16;
c = ((a * weightA + b * weightB) / 16);
if (c & 0x08000000) {
c = (c & ~0x0FC00000) | 0x07C00000;
}
if (c & 0x0020) {
c = (c & ~0x003F) | 0x001F;
}
if (c & 0x10000) {
c = (c & ~0x1F800) | 0xF800;
}
c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
#else
a = colorA & 0x7C1F;
b = colorB & 0x7C1F;
a |= (colorA & 0x3E0) << 16;
b |= (colorB & 0x3E0) << 16;
c = ((a * weightA + b * weightB) / 16);
if (c & 0x04000000) {
c = (c & ~0x07E00000) | 0x03E00000;
}
if (c & 0x0020) {
c = (c & ~0x003F) | 0x001F;
}
if (c & 0x8000) {
c = (c & ~0xF800) | 0x7C00;
}
c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
#endif
#else
a = colorA & 0xFF;
b = colorB & 0xFF;
c |= ((a * weightA + b * weightB) / 16) & 0x1FF;
if (c & 0x00000100) {
c = 0x000000FF;
}
a = colorA & 0xFF00;
b = colorB & 0xFF00;
c |= ((a * weightA + b * weightB) / 16) & 0x1FF00;
if (c & 0x00010000) {
c = (c & 0x000000FF) | 0x0000FF00;
}
a = colorA & 0xFF0000;
b = colorB & 0xFF0000;
c |= ((a * weightA + b * weightB) / 16) & 0x1FF0000;
if (c & 0x01000000) {
c = (c & 0x0000FFFF) | 0x00FF0000;
}
#endif
return c;
}
#endif
CXX_GUARD_END
#endif

View File

@ -1,10 +1,10 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau
/* Copyright (c) 2013-2023 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef EXPORT_H
#define EXPORT_H
#ifndef M_IMAGE_EXPORT_H
#define M_IMAGE_EXPORT_H
#include <mgba-util/common.h>
@ -12,8 +12,8 @@ CXX_GUARD_START
struct VFile;
bool exportPaletteRIFF(struct VFile* vf, size_t entries, const uint16_t* colors);
bool exportPaletteACT(struct VFile* vf, size_t entries, const uint16_t* colors);
bool mPaletteExportRIFF(struct VFile* vf, size_t entries, const uint16_t* colors);
bool mPaletteExportACT(struct VFile* vf, size_t entries, const uint16_t* colors);
CXX_GUARD_END

View File

@ -10,165 +10,14 @@
CXX_GUARD_START
#include <mgba-util/image.h>
#include <mgba-util/vector.h>
struct mCore;
struct mStateExtdataItem;
#ifdef COLOR_16_BIT
typedef uint16_t color_t;
#define BYTES_PER_PIXEL 2
#else
typedef uint32_t color_t;
#define BYTES_PER_PIXEL 4
#endif
#define M_R5(X) ((X) & 0x1F)
#define M_G5(X) (((X) >> 5) & 0x1F)
#define M_B5(X) (((X) >> 10) & 0x1F)
#define M_R8(X) (((((X) << 3) & 0xF8) * 0x21) >> 2)
#define M_G8(X) (((((X) >> 2) & 0xF8) * 0x21) >> 2)
#define M_B8(X) (((((X) >> 7) & 0xF8) * 0x21) >> 2)
#define M_RGB5_TO_BGR8(X) ((M_R5(X) << 3) | (M_G5(X) << 11) | (M_B5(X) << 19))
#define M_RGB5_TO_RGB8(X) ((M_R5(X) << 19) | (M_G5(X) << 11) | (M_B5(X) << 3))
#define M_RGB8_TO_BGR5(X) ((((X) & 0xF8) >> 3) | (((X) & 0xF800) >> 6) | (((X) & 0xF80000) >> 9))
#define M_RGB8_TO_RGB5(X) ((((X) & 0xF8) << 7) | (((X) & 0xF800) >> 6) | (((X) & 0xF80000) >> 19))
#ifndef COLOR_16_BIT
#define M_COLOR_RED 0x000000FF
#define M_COLOR_GREEN 0x0000FF00
#define M_COLOR_BLUE 0x00FF0000
#define M_COLOR_ALPHA 0xFF000000
#define M_COLOR_WHITE 0x00FFFFFF
#define M_RGB8_TO_NATIVE(X) (((X) & 0x00FF00) | (((X) & 0x0000FF) << 16) | (((X) & 0xFF0000) >> 16))
#elif defined(COLOR_5_6_5)
#define M_COLOR_RED 0x001F
#define M_COLOR_GREEN 0x07E0
#define M_COLOR_BLUE 0xF800
#define M_COLOR_ALPHA 0x0000
#define M_COLOR_WHITE 0xFFDF
#define M_RGB8_TO_NATIVE(X) ((((X) & 0xF8) << 8) | (((X) & 0xFC00) >> 5) | (((X) & 0xF80000) >> 19))
#else
#define M_COLOR_RED 0x001F
#define M_COLOR_GREEN 0x03E0
#define M_COLOR_BLUE 0x7C00
#define M_COLOR_ALPHA 0x1000
#define M_COLOR_WHITE 0x7FFF
#define M_RGB8_TO_NATIVE(X) M_RGB8_TO_BGR5(X)
#endif
#ifndef PYCPARSE
static inline color_t mColorFrom555(uint16_t value) {
#ifdef COLOR_16_BIT
#ifdef COLOR_5_6_5
color_t color = 0;
color |= (value & 0x001F) << 11;
color |= (value & 0x03E0) << 1;
color |= (value & 0x7C00) >> 10;
#else
color_t color = value;
#endif
#else
color_t color = M_RGB5_TO_BGR8(value);
color |= (color >> 5) & 0x070707;
#endif
return color;
}
ATTRIBUTE_UNUSED static unsigned mColorMix5Bit(int weightA, unsigned colorA, int weightB, unsigned colorB) {
unsigned c = 0;
unsigned a, b;
#ifdef COLOR_16_BIT
#ifdef COLOR_5_6_5
a = colorA & 0xF81F;
b = colorB & 0xF81F;
a |= (colorA & 0x7C0) << 16;
b |= (colorB & 0x7C0) << 16;
c = ((a * weightA + b * weightB) / 16);
if (c & 0x08000000) {
c = (c & ~0x0FC00000) | 0x07C00000;
}
if (c & 0x0020) {
c = (c & ~0x003F) | 0x001F;
}
if (c & 0x10000) {
c = (c & ~0x1F800) | 0xF800;
}
c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
#else
a = colorA & 0x7C1F;
b = colorB & 0x7C1F;
a |= (colorA & 0x3E0) << 16;
b |= (colorB & 0x3E0) << 16;
c = ((a * weightA + b * weightB) / 16);
if (c & 0x04000000) {
c = (c & ~0x07E00000) | 0x03E00000;
}
if (c & 0x0020) {
c = (c & ~0x003F) | 0x001F;
}
if (c & 0x8000) {
c = (c & ~0xF800) | 0x7C00;
}
c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
#endif
#else
a = colorA & 0xFF;
b = colorB & 0xFF;
c |= ((a * weightA + b * weightB) / 16) & 0x1FF;
if (c & 0x00000100) {
c = 0x000000FF;
}
a = colorA & 0xFF00;
b = colorB & 0xFF00;
c |= ((a * weightA + b * weightB) / 16) & 0x1FF00;
if (c & 0x00010000) {
c = (c & 0x000000FF) | 0x0000FF00;
}
a = colorA & 0xFF0000;
b = colorB & 0xFF0000;
c |= ((a * weightA + b * weightB) / 16) & 0x1FF0000;
if (c & 0x01000000) {
c = (c & 0x0000FFFF) | 0x00FF0000;
}
#endif
return c;
}
#endif
struct blip_t;
enum mColorFormat {
mCOLOR_XBGR8 = 0x00001,
mCOLOR_XRGB8 = 0x00002,
mCOLOR_BGRX8 = 0x00004,
mCOLOR_RGBX8 = 0x00008,
mCOLOR_ABGR8 = 0x00010,
mCOLOR_ARGB8 = 0x00020,
mCOLOR_BGRA8 = 0x00040,
mCOLOR_RGBA8 = 0x00080,
mCOLOR_RGB5 = 0x00100,
mCOLOR_BGR5 = 0x00200,
mCOLOR_RGB565 = 0x00400,
mCOLOR_BGR565 = 0x00800,
mCOLOR_ARGB5 = 0x01000,
mCOLOR_ABGR5 = 0x02000,
mCOLOR_RGBA5 = 0x04000,
mCOLOR_BGRA5 = 0x08000,
mCOLOR_RGB8 = 0x10000,
mCOLOR_BGR8 = 0x20000,
mCOLOR_L8 = 0x40000,
mCOLOR_ANY = -1
};
enum mCoreFeature {
mCORE_FEATURE_OPENGL = 1,
};

View File

@ -87,7 +87,7 @@ struct mCore* mCoreCreate(enum mPlatform platform) {
}
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#ifdef PSP2
#include <psp2/photoexport.h>

View File

@ -13,7 +13,7 @@
#include <mgba-util/vfs.h>
#ifdef USE_PNG
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <png.h>
#include <zlib.h>
#endif

View File

@ -15,8 +15,8 @@
#include <mgba-util/gui/file-select.h>
#include <mgba-util/gui/font.h>
#include <mgba-util/gui/menu.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/memory.h>
#include <mgba-util/png-io.h>
#include <mgba-util/vfs.h>
#ifdef PSP2

View File

@ -13,7 +13,7 @@
#ifdef USE_FFMPEG
#include <mgba-util/convolve.h>
#ifdef USE_PNG
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/vfs.h>
#endif

View File

@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba-util/gui/font.h>
#include <mgba-util/gui/font-metrics.h>
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/vfs.h>
#include "icons.h"

View File

@ -52,7 +52,7 @@ void free(void*);
#undef PYEXPORT
#ifdef USE_PNG
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#endif
#ifdef M_CORE_GBA
#include <mgba/gba/interface.h>

View File

@ -40,7 +40,7 @@ ffi.set_source("mgba._pylib", """
#include <mgba/internal/sm83/sm83.h>
#include <mgba/internal/gb/gb.h>
#include <mgba/internal/gb/renderers/cache-set.h>
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/vfs.h>
#define PYEXPORT

View File

@ -9,7 +9,7 @@
#include "GBAApp.h"
#include "LogController.h"
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/vfs.h>
#ifdef M_CORE_GBA
#include <mgba/internal/gba/gba.h>

View File

@ -19,7 +19,7 @@
#ifdef M_CORE_GB
#include <mgba/internal/gb/gb.h>
#endif
#include <mgba-util/export.h>
#include <mgba-util/image/export.h>
#include <mgba-util/vfs.h>
using namespace QGBA;
@ -145,9 +145,9 @@ void PaletteView::exportPalette(int start, int length) {
return;
}
if (filename.endsWith(".pal", Qt::CaseInsensitive)) {
exportPaletteRIFF(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
mPaletteExportRIFF(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
} else if (filename.endsWith(".act", Qt::CaseInsensitive)) {
exportPaletteACT(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
mPaletteExportACT(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
}
vf->close(vf);
}

View File

@ -15,7 +15,7 @@
#include <mgba/core/cheats.h>
#include <mgba/core/serialize.h>
#include <mgba/core/version.h>
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/vfs.h>
#include "CoreController.h"

View File

@ -10,7 +10,7 @@
#include <mgba/core/version.h>
#ifdef USE_PNG
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/vfs.h>
#endif

View File

@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba-util/gui/font.h>
#include <mgba-util/gui/font-metrics.h>
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/string.h>
#include <mgba-util/vfs.h>

View File

@ -10,7 +10,7 @@
#include <mgba/feature/commandline.h>
#include <mgba/feature/video-logger.h>
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#include <mgba-util/string.h>
#include <mgba-util/table.h>
#include <mgba-util/threading.h>

View File

@ -15,13 +15,13 @@ set(SOURCE_FILES
${BASE_SOURCE_FILES}
convolve.c
elf-read.c
export.c
geometry.c
image/export.c
image/png-io.c
patch.c
patch-fast.c
patch-ips.c
patch-ups.c
png-io.c
ring-fifo.c
sfo.c
text-codec.c)

View File

@ -1,14 +1,14 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau
/* Copyright (c) 2013-2023 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba-util/export.h>
#include <mgba-util/image/export.h>
#include <mgba/core/interface.h>
#include <mgba-util/vfs.h>
bool exportPaletteRIFF(struct VFile* vf, size_t entries, const uint16_t* colors) {
bool mPaletteExportRIFF(struct VFile* vf, size_t entries, const uint16_t* colors) {
if (entries > 0xFFFF) {
return false;
}
@ -56,7 +56,7 @@ bool exportPaletteRIFF(struct VFile* vf, size_t entries, const uint16_t* colors)
return true;
}
bool exportPaletteACT(struct VFile* vf, size_t entries, const uint16_t* colors) {
bool mPaletteExportACT(struct VFile* vf, size_t entries, const uint16_t* colors) {
if (entries > 256) {
return false;
}

View File

@ -3,7 +3,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba-util/png-io.h>
#include <mgba-util/image/png-io.h>
#ifdef USE_PNG