(VITA) Slimming Vita2D

This commit is contained in:
frangarcj 2016-09-06 12:03:18 +02:00
parent 434d70ce13
commit d21938a015
6 changed files with 1 additions and 453 deletions

View File

@ -216,7 +216,7 @@ else ifeq ($(platform), vita)
LIBS += -lSceKernel_stub -lSceDisplay_stub -lSceGxm_stub -lSceNet_stub -lSceNetCtl_stub\
-lSceSysmodule_stub -lSceCtrl_stub -lSceAudio_stub -lSceFiber_stub\
-lScePower_stub -lSceRtc_stub -lSceCommonDialog_stub -lScePgf_stub \
-lSceMotion_stub -lfreetype -lm -lc -lpng -ljpeg
-lSceMotion_stub -lfreetype -lm -lc
PLATEXTRA := deps/libvita2d/shader/compiled/clear_v_gxp.o \
deps/libvita2d/shader/compiled/clear_f_gxp.o \

View File

@ -101,15 +101,6 @@ void vita2d_draw_texture_tint_part_scale(const vita2d_texture *texture, float x,
void vita2d_draw_texture_tint_scale_rotate_hotspot(const vita2d_texture *texture, float x, float y, float x_scale, float y_scale, float rad, float center_x, float center_y, unsigned int color);
void vita2d_draw_texture_tint_scale_rotate(const vita2d_texture *texture, float x, float y, float x_scale, float y_scale, float rad, unsigned int color);
vita2d_texture *vita2d_load_PNG_file(const char *filename);
vita2d_texture *vita2d_load_PNG_buffer(const void *buffer);
vita2d_texture *vita2d_load_JPEG_file(const char *filename);
vita2d_texture *vita2d_load_JPEG_buffer(const void *buffer, unsigned long buffer_size);
vita2d_texture *vita2d_load_BMP_file(const char *filename);
vita2d_texture *vita2d_load_BMP_buffer(const void *buffer);
vita2d_font *vita2d_load_font_file(const char *filename);
vita2d_font *vita2d_load_font_mem(const void *buffer, unsigned int size);
void vita2d_free_font(vita2d_font *font);

View File

@ -1,175 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <psp2/io/fcntl.h>
#include <psp2/gxm.h>
#include "vita2d.h"
#define BMP_SIGNATURE (0x4D42)
typedef struct {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} __attribute__((packed)) BITMAPFILEHEADER;
typedef struct {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} __attribute__((packed)) BITMAPINFOHEADER;
static vita2d_texture *_vita2d_load_BMP_generic(
BITMAPFILEHEADER *bmp_fh,
BITMAPINFOHEADER *bmp_ih,
void *user_data,
void (*seek_fn)(void *user_data, unsigned int offset),
void (*read_fn)(void *user_data, void *buffer, unsigned int length))
{
unsigned int row_stride = bmp_ih->biWidth * (bmp_ih->biBitCount/8);
if (row_stride%4 != 0) {
row_stride += 4-(row_stride%4);
}
void *buffer = malloc(row_stride);
if (!buffer)
return NULL;
vita2d_texture *texture = vita2d_create_empty_texture(
bmp_ih->biWidth,
bmp_ih->biHeight);
if (!texture) {
free(buffer);
return NULL;
}
void *texture_data = vita2d_texture_get_datap(texture);
unsigned int tex_stride = vita2d_texture_get_stride(texture);
int i, x, y;
seek_fn(user_data, bmp_fh->bfOffBits);
for (i = 0; i < bmp_ih->biHeight; i++) {
read_fn(user_data, buffer, row_stride);
y = bmp_ih->biHeight - 1 - i;
unsigned int *tex_ptr = (unsigned int *)(texture_data + y*tex_stride);
for (x = 0; x < bmp_ih->biWidth; x++) {
if (bmp_ih->biBitCount == 32) { //ABGR8888
unsigned int color = *(unsigned int *)(buffer + x*4);
*tex_ptr = (color&0xFF)<<24 | ((color>>8)&0xFF)<<16 |
((color>>16)&0xFF)<<8 | (color>>24);
} else if (bmp_ih->biBitCount == 24) { //BGR888
unsigned char *address = buffer + x*3;
*tex_ptr = (*address)<<16 | (*(address+1))<<8 |
(*(address+2)) | (0xFF<<24);
} else if (bmp_ih->biBitCount == 16) { //BGR565
unsigned int color = *(unsigned short *)(buffer + x*2);
unsigned char r = (color & 0x1F) *((float)255/31);
unsigned char g = ((color>>5) & 0x3F) *((float)255/63);
unsigned char b = ((color>>11) & 0x1F) *((float)255/31);
*tex_ptr = ((r<<16) | (g<<8) | b | (0xFF<<24));
}
tex_ptr++;
}
}
free(buffer);
return texture;
}
static void _vita2d_read_bmp_file_seek_fn(void *user_data, unsigned int offset)
{
sceIoLseek(*(SceUID*)user_data, offset, SEEK_SET);
}
static void _vita2d_read_bmp_file_read_fn(void *user_data, void *buffer, unsigned int length)
{
sceIoRead(*(SceUID*)user_data, buffer, length);
}
static void _vita2d_read_bmp_buffer_seek_fn(void *user_data, unsigned int offset)
{
*(unsigned int *)user_data += offset;
}
static void _vita2d_read_bmp_buffer_read_fn(void *user_data, void *buffer, unsigned int length)
{
memcpy(buffer, (void *)*(unsigned int *)user_data, length);
*(unsigned int *)user_data += length;
}
vita2d_texture *vita2d_load_BMP_file(const char *filename)
{
SceUID fd;
if ((fd = sceIoOpen(filename, SCE_O_RDONLY, 0777)) < 0) {
goto exit_error;
}
BITMAPFILEHEADER bmp_fh;
sceIoRead(fd, (void *)&bmp_fh, sizeof(BITMAPFILEHEADER));
if (bmp_fh.bfType != BMP_SIGNATURE) {
goto exit_close;
}
BITMAPINFOHEADER bmp_ih;
sceIoRead(fd, (void *)&bmp_ih, sizeof(BITMAPINFOHEADER));
vita2d_texture *texture = _vita2d_load_BMP_generic(&bmp_fh,
&bmp_ih,
(void *)&fd,
_vita2d_read_bmp_file_seek_fn,
_vita2d_read_bmp_file_read_fn);
sceIoClose(fd);
return texture;
exit_close:
sceIoClose(fd);
exit_error:
return NULL;
}
vita2d_texture *vita2d_load_BMP_buffer(const void *buffer)
{
BITMAPFILEHEADER bmp_fh;
memcpy(&bmp_fh, buffer, sizeof(BITMAPFILEHEADER));
if (bmp_fh.bfType != BMP_SIGNATURE) {
goto exit_error;
}
BITMAPINFOHEADER bmp_ih;
memcpy(&bmp_ih, buffer + sizeof(BITMAPFILEHEADER), sizeof(BITMAPINFOHEADER));
unsigned int buffer_address = (unsigned int)buffer;
vita2d_texture *texture = _vita2d_load_BMP_generic(&bmp_fh,
&bmp_ih,
(void *)&buffer_address,
_vita2d_read_bmp_buffer_seek_fn,
_vita2d_read_bmp_buffer_read_fn);
return texture;
exit_error:
return NULL;
}

View File

@ -1,108 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <psp2/io/fcntl.h>
#include <psp2/gxm.h>
#include <jpeglib.h>
#include "vita2d.h"
static vita2d_texture *_vita2d_load_JPEG_generic(struct jpeg_decompress_struct *jinfo, struct jpeg_error_mgr *jerr)
{
float downScaleWidth = (float)jinfo->image_width / 4096;
float downScaleHeight = (float)jinfo->image_height / 4096;
float downScale = (downScaleWidth >= downScaleHeight) ? downScaleWidth : downScaleHeight;
if (downScale <= 1.f) {
jinfo->scale_denom = 1;
} else if (downScale <= 2.f) {
jinfo->scale_denom = 2;
} else if (downScale <= 4.f) {
jinfo->scale_denom = 4;
} else if (downScale <= 8.f) {
jinfo->scale_denom = 8;
} else {
return NULL;
}
jpeg_start_decompress(jinfo);
vita2d_texture *texture = vita2d_create_empty_texture_format(
jinfo->output_width,
jinfo->output_height,
SCE_GXM_TEXTURE_FORMAT_U8U8U8_BGR);
if (!texture) {
jpeg_abort_decompress(jinfo);
return NULL;
}
void *texture_data = vita2d_texture_get_datap(texture);
unsigned int row_stride = vita2d_texture_get_stride(texture);
unsigned char *row_pointer = texture_data;
while (jinfo->output_scanline < jinfo->output_height) {
jpeg_read_scanlines(jinfo, (JSAMPARRAY)&row_pointer, 1);
row_pointer += row_stride;
}
jpeg_finish_decompress(jinfo);
return texture;
}
vita2d_texture *vita2d_load_JPEG_file(const char *filename)
{
FILE *fp;
if ((fp = fopen(filename, "rb")) < 0) {
return NULL;
}
unsigned int magic = 0;
fread(&magic, 1, sizeof(unsigned int), fp);
fseek(fp, 0, SEEK_SET);
if (magic != 0xE0FFD8FF && magic != 0xE1FFD8FF) {
fclose(fp);
return NULL;
}
struct jpeg_decompress_struct jinfo;
struct jpeg_error_mgr jerr;
jinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&jinfo);
jpeg_stdio_src(&jinfo, fp);
jpeg_read_header(&jinfo, 1);
vita2d_texture *texture = _vita2d_load_JPEG_generic(&jinfo, &jerr);
jpeg_destroy_decompress(&jinfo);
fclose(fp);
return texture;
}
vita2d_texture *vita2d_load_JPEG_buffer(const void *buffer, unsigned long buffer_size)
{
unsigned int magic = *(unsigned int *)buffer;
if (magic != 0xE0FFD8FF && magic != 0xE1FFD8FF) {
return NULL;
}
struct jpeg_decompress_struct jinfo;
struct jpeg_error_mgr jerr;
jinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&jinfo);
jpeg_mem_src(&jinfo, (void *)buffer, buffer_size);
jpeg_read_header(&jinfo, 1);
vita2d_texture *texture = _vita2d_load_JPEG_generic(&jinfo, &jerr);
jpeg_destroy_decompress(&jinfo);
return texture;
}

View File

@ -1,157 +0,0 @@
#include <string.h>
#include <stdlib.h>
#include <psp2/io/fcntl.h>
#include <psp2/gxm.h>
#include <png.h>
#include "vita2d.h"
#define PNG_SIGSIZE (8)
static void _vita2d_read_png_file_fn(png_structp png_ptr, png_bytep data, png_size_t length)
{
SceUID fd = *(SceUID*) png_get_io_ptr(png_ptr);
sceIoRead(fd, data, length);
}
static void _vita2d_read_png_buffer_fn(png_structp png_ptr, png_bytep data, png_size_t length)
{
unsigned int *address = png_get_io_ptr(png_ptr);
memcpy(data, (void *)*address, length);
*address += length;
}
static vita2d_texture *_vita2d_load_PNG_generic(const void *io_ptr, png_rw_ptr read_data_fn)
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
goto error_create_read;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
goto error_create_info;
}
png_bytep *row_ptrs = NULL;
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0);
if (row_ptrs != NULL)
free(row_ptrs);
return NULL;
}
png_set_read_fn(png_ptr, (png_voidp)io_ptr, read_data_fn);
png_set_sig_bytes(png_ptr, PNG_SIGSIZE);
png_read_info(png_ptr, info_ptr);
unsigned int width, height;
int bit_depth, color_type;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
if ((color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
|| (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
|| png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)
|| (bit_depth == 16)) {
png_set_expand(png_ptr);
}
if (bit_depth == 16)
png_set_scale_16(png_ptr);
if (bit_depth == 8 && color_type == PNG_COLOR_TYPE_RGB)
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
}
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
png_read_update_info(png_ptr, info_ptr);
row_ptrs = (png_bytep *)malloc(sizeof(png_bytep) * height);
if (!row_ptrs)
goto error_alloc_rows;
vita2d_texture *texture = vita2d_create_empty_texture(width, height);
if (!texture)
goto error_create_tex;
void *texture_data = vita2d_texture_get_datap(texture);
unsigned int stride = vita2d_texture_get_stride(texture);
int i;
for (i = 0; i < height; i++) {
row_ptrs[i] = (png_bytep)(texture_data + i*stride);
}
png_read_image(png_ptr, row_ptrs);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0);
free(row_ptrs);
return texture;
error_create_tex:
free(row_ptrs);
error_alloc_rows:
png_destroy_info_struct(png_ptr, &info_ptr);
error_create_info:
png_destroy_read_struct(&png_ptr, (png_infopp)0, (png_infopp)0);
error_create_read:
return NULL;
}
vita2d_texture *vita2d_load_PNG_file(const char *filename)
{
png_byte pngsig[PNG_SIGSIZE];
SceUID fd;
if ((fd = sceIoOpen(filename, SCE_O_RDONLY, 0777)) < 0) {
goto exit_error;
}
if (sceIoRead(fd, pngsig, PNG_SIGSIZE) != PNG_SIGSIZE) {
goto exit_close;
}
if (png_sig_cmp(pngsig, 0, PNG_SIGSIZE) != 0) {
goto exit_close;
}
vita2d_texture *texture = _vita2d_load_PNG_generic((void *)&fd, _vita2d_read_png_file_fn);
sceIoClose(fd);
return texture;
exit_close:
sceIoClose(fd);
exit_error:
return NULL;
}
vita2d_texture *vita2d_load_PNG_buffer(const void *buffer)
{
if (png_sig_cmp((png_byte *) buffer, 0, PNG_SIGSIZE) != 0) {
return NULL;
}
unsigned int buffer_address = (unsigned int)buffer + PNG_SIGSIZE;
return _vita2d_load_PNG_generic((void *)&buffer_address, _vita2d_read_png_buffer_fn);
}

View File

@ -331,9 +331,6 @@ VIDEO DRIVER
#include "../deps/libvita2d/source/vita2d_texture.c"
#include "../deps/libvita2d/source/vita2d_draw.c"
#include "../deps/libvita2d/source/utils.c"
#include "../deps/libvita2d/source/vita2d_image_png.c"
#include "../deps/libvita2d/source/vita2d_image_jpeg.c"
#include "../deps/libvita2d/source/vita2d_image_bmp.c"
#include "../deps/libvita2d/source/vita2d_font.c"
#include "../deps/libvita2d/source/vita2d_pgf.c"
#include "../deps/libvita2d/source/bin_packing_2d.c"