-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
308 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* | ||
* Copyright (c) 2008 Andreas Weis (http://www.ghulbus-inc.de/) | ||
* | ||
* 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. | ||
*/ | ||
|
||
//================================================================================================ | ||
// Typedefs and Defines | ||
//================================================================================================ | ||
|
||
typedef struct | ||
{ | ||
char magic[4]; | ||
uint16_t padding1; // 0000 | ||
uint16_t secondLineOffset; | ||
uint32_t padding2; // 00000000 | ||
uint32_t transparencyVal; // 0x00 (clear) to 0x80 (opaque) | ||
uint8_t bgColourUpperLeft[16]; | ||
uint8_t bgColourUpperRight[16]; | ||
uint8_t bgColourLowerLeft[16]; | ||
uint8_t bgColourLowerRight[16]; | ||
uint8_t light1Direction[16]; | ||
uint8_t light2Direction[16]; | ||
uint8_t light3Direction[16]; | ||
uint8_t light1RGB[16]; | ||
uint8_t light2RGB[16]; | ||
uint8_t light3RGB[16]; | ||
uint8_t ambientLightRGB[16]; | ||
char title[68]; // null terminated, S-JIS | ||
char IconName[64]; // null terminated | ||
char copyIconName[64]; // null terminated | ||
char deleteIconName[64]; // null terminated | ||
uint8_t padding3[512]; | ||
} ps2_IconSys_t; | ||
|
||
/** File header | ||
*/ | ||
typedef struct Icon_Header_t { | ||
unsigned int file_id; ///< reserved; should be: 0x010000 (but does not have to ;) ) | ||
unsigned int animation_shapes; ///< number of animation shapes per vertex | ||
unsigned int texture_type; ///< texture type - 0x07: uncompressed, 0x06: uncompresses, 0x0f: RLE compression | ||
unsigned int reserved; ///< reserved; should be: 0x3F800000 (but does not have to ;) ) | ||
unsigned int n_vertices; ///< number of vertices; must be a multiple of 3 | ||
} Icon_Header; | ||
/** Set of vertex coordinates | ||
* @note The f16_* fields indicate float16 data; divide by 4096.0f to convert to float32; | ||
*/ | ||
typedef struct Vertex_Coord_t { | ||
short f16_x; ///< vertex x coordinate in float16 | ||
short f16_y; ///< vertex y coordinate in float16 | ||
short f16_z; ///< vertex z coordinate in float16 | ||
short f16_unknown; ///< unknown; seems to influence lightning? | ||
} Vertex_Coord; | ||
/** Set of texture coordinates | ||
* @note The f16_* fields indicate float16 data; divide by 4096.0f to convert to float32; | ||
*/ | ||
typedef struct Texture_Data_t { | ||
short f16_u; ///< vertex u texture coordinate in float16 | ||
short f16_v; ///< vertex v texture coordinate in float16 | ||
unsigned int color; ///< vertex color (32 bit RGBA) | ||
} Texture_Data; | ||
/** Animation header | ||
*/ | ||
typedef struct Animation_Header_t { | ||
unsigned int id_tag; ///< ??? | ||
unsigned int frame_length; ///< ??? | ||
float anim_speed; ///< ??? | ||
unsigned int play_offset; ///< ??? | ||
unsigned int n_frames; ///< number of frames in the animation | ||
} Animation_Header; | ||
/** Per-frame animation data | ||
*/ | ||
typedef struct Frame_Data_t { | ||
unsigned int shape_id; ///< shape used for this frame | ||
unsigned int n_keys; ///< number of keys corresponding to this frame | ||
} Frame_Data; | ||
/** Per-key animation data | ||
*/ | ||
typedef struct Frame_Key_t { | ||
float time; ///< ??? | ||
float value; ///< ??? | ||
} Frame_Key; | ||
|
||
//Get icon data as bytes | ||
uint8_t* getIconPS2(const char* folder, const char* iconfile); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/types.h> | ||
#include <string.h> | ||
|
||
#include "ps2icon.h" | ||
#include "mcio.h" | ||
|
||
|
||
static uint32_t TIM2RGBA(const uint8_t *buf) | ||
{ | ||
uint8_t RGBA[4]; | ||
uint16_t lRGB = (int16_t) (buf[1] << 8) | buf[0]; | ||
|
||
RGBA[0] = 8 * (lRGB & 0x1F); | ||
RGBA[1] = 8 * ((lRGB >> 5) & 0x1F); | ||
RGBA[2] = 8 * (lRGB >> 10); | ||
RGBA[3] = 0xFF; | ||
|
||
return *((uint32_t *) &RGBA); | ||
} | ||
|
||
static void* ps2IconTexture(const uint8_t* iData) | ||
{ | ||
uint32_t i; | ||
uint16_t j; | ||
Icon_Header header; | ||
Animation_Header anim_header; | ||
Frame_Data animation; | ||
uint32_t *lTexturePtr, *lRGBA; | ||
|
||
lTexturePtr = (uint32_t *) calloc(128 * 128, sizeof(uint32_t)); | ||
|
||
//read header: | ||
memcpy(&header, iData, sizeof(Icon_Header)); | ||
iData += sizeof(Icon_Header); | ||
|
||
//n_vertices has to be divisible by three, that's for sure: | ||
if(header.file_id != 0x010000 || header.n_vertices % 3 != 0) | ||
return lTexturePtr; | ||
|
||
//read icon data from file: https://ghulbus-inc.de/projects/ps2iconsys/ | ||
///Vertex data | ||
// each vertex consists of animation_shapes tuples for vertex coordinates, | ||
// followed by one vertex coordinate tuple for normal coordinates | ||
// followed by one texture data tuple for texture coordinates and color | ||
for(i=0; i<header.n_vertices; i++) { | ||
iData += sizeof(Vertex_Coord) * header.animation_shapes; | ||
iData += sizeof(Vertex_Coord); | ||
iData += sizeof(Texture_Data); | ||
} | ||
|
||
//animation data | ||
// preceeded by an animation header, there is a frame data/key set for every frame: | ||
memcpy(&anim_header, iData, sizeof(Animation_Header)); | ||
iData += sizeof(Animation_Header); | ||
|
||
//read animation data: | ||
for(i=0; i<anim_header.n_frames; i++) { | ||
memcpy(&animation, iData, sizeof(Frame_Data)); | ||
iData += sizeof(Frame_Data); | ||
|
||
if(animation.n_keys > 0) | ||
iData += sizeof(Frame_Key) * animation.n_keys; | ||
} | ||
|
||
lRGBA = lTexturePtr; | ||
|
||
if (header.texture_type <= 7) | ||
{ // Uncompressed texture | ||
for (i = 0; i < (128 * 128); i++) | ||
{ | ||
*lRGBA = TIM2RGBA(iData); | ||
lRGBA++; | ||
iData += 2; | ||
} | ||
} | ||
else | ||
{ //Compressed texture | ||
iData += 4; | ||
do | ||
{ | ||
j = (int16_t) (iData[1] << 8) | iData[0]; | ||
if (0xFF00 == (j & 0xFF00)) | ||
{ | ||
for (j = (0x0000 - j) & 0xFFFF; j > 0; j--) | ||
{ | ||
iData += 2; | ||
*lRGBA = TIM2RGBA(iData); | ||
lRGBA++; | ||
} | ||
} | ||
else | ||
{ | ||
iData += 2; | ||
for (; j > 0; j--) | ||
{ | ||
*lRGBA = TIM2RGBA(iData); | ||
lRGBA++; | ||
} | ||
} | ||
iData += 2; | ||
} while ((lRGBA - lTexturePtr) < 0x4000); | ||
} | ||
|
||
return (lTexturePtr); | ||
} | ||
|
||
//Get icon data as bytes | ||
uint8_t* getIconPS2(const char* folder, const char* iconfile) | ||
{ | ||
int fd; | ||
uint8_t *buf, *out; | ||
char filePath[256]; | ||
struct io_dirent st; | ||
|
||
snprintf(filePath, sizeof(filePath), "%s/%s", folder, iconfile); | ||
mcio_mcStat(filePath, &st); | ||
|
||
fd = mcio_mcOpen(filePath, sceMcFileAttrReadable | sceMcFileAttrFile); | ||
if (fd < 0) | ||
return calloc(128 * 128, sizeof(uint32_t)); | ||
|
||
buf = malloc(st.stat.size); | ||
mcio_mcRead(fd, buf, st.stat.size); | ||
mcio_mcClose(fd); | ||
|
||
out = ps2IconTexture(buf); | ||
free(buf); | ||
|
||
return out; | ||
} |