Copy over libretro-common changes

This commit is contained in:
twinaphex 2016-03-20 04:49:59 +01:00
parent 97981c7a9d
commit b40b169098
7 changed files with 86 additions and 57 deletions

View File

@ -23,6 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <retro_common.h>
#include <file/file_list.h>
#include <compat/strcasestr.h>

View File

@ -23,6 +23,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <retro_common.h>
#if defined(_WIN32)
# ifdef _MSC_VER
# define setmode _setmode

View File

@ -26,9 +26,9 @@
#include <retro_file.h>
#include <formats/rbmp.h>
static bool write_header_bmp(RFILE *file, unsigned width, unsigned height)
static bool write_header_bmp(RFILE *file, unsigned width, unsigned height, bool is32bpp)
{
unsigned line_size = (width * 3 + 3) & ~3;
unsigned line_size = (width * (is32bpp?4:3) + 3) & ~3;
unsigned size = line_size * height + 54;
unsigned size_array = line_size * height;
uint8_t header[54];
@ -72,7 +72,7 @@ static bool write_header_bmp(RFILE *file, unsigned width, unsigned height)
header[26] = 1;
header[27] = 0;
/* Bits per pixel */
header[28] = 24;
header[28] = is32bpp?32:24;
header[29] = 0;
/* Compression method */
header[30] = 0;
@ -108,21 +108,7 @@ static bool write_header_bmp(RFILE *file, unsigned width, unsigned height)
return retro_fwrite(file, header, sizeof(header)) == sizeof(header);
}
static void dump_lines_file(RFILE *file, uint8_t **lines,
size_t line_size, unsigned height)
{
unsigned i;
for (i = 0; i < height; i++)
retro_fwrite(file, lines[i], line_size);
}
static void dump_line_bgr(uint8_t *line, const uint8_t *src, unsigned width)
{
memcpy(line, src, width * 3);
}
static void dump_line_16(uint8_t *line, const uint16_t *src, unsigned width)
static void dump_line_565_to_24(uint8_t *line, const uint16_t *src, unsigned width)
{
unsigned i;
@ -138,7 +124,7 @@ static void dump_line_16(uint8_t *line, const uint16_t *src, unsigned width)
}
}
static void dump_line_32(uint8_t *line, const uint32_t *src, unsigned width)
static void dump_line_32_to_24(uint8_t *line, const uint32_t *src, unsigned width)
{
unsigned i;
@ -152,68 +138,81 @@ static void dump_line_32(uint8_t *line, const uint32_t *src, unsigned width)
}
static void dump_content(RFILE *file, const void *frame,
int width, int height, int pitch, bool bgr24, bool xrgb8888)
int width, int height, int pitch, rbmp_source_type type)
{
uint8_t *line;
size_t line_size;
int i, j;
int bytes_per_pixel = (type==RBMP_SOURCE_TYPE_ARGB8888?4:3);
union
{
const uint8_t *u8;
const uint16_t *u16;
const uint32_t *u32;
} u;
uint8_t **lines = (uint8_t**)calloc(height, sizeof(uint8_t*));
if (!lines)
u.u8 = (const uint8_t*)frame + (height-1) * pitch;
line_size = (width * bytes_per_pixel + 3) & ~3;
if (type == RBMP_SOURCE_TYPE_BGR24)
{
/* BGR24 byte order input matches output. Can directly copy, but... need to make sure we pad it. */
uint32_t zeros = 0;
int pad = line_size-pitch;
for (j = height-1; j >= 0; j--, u.u8 -= pitch)
{
retro_fwrite(file, u.u8, pitch);
if(pad != 0) retro_fwrite(file, &zeros, pad);
}
return;
u.u8 = (const uint8_t*)frame;
line_size = (width * 3 + 3) & ~3;
for (i = 0; i < height; i++)
}
else if(type == RBMP_SOURCE_TYPE_ARGB8888)
{
lines[i] = (uint8_t*)calloc(1, line_size);
if (!lines[i])
goto end;
/* ARGB8888 byte order input matches output. Can directly copy. */
for (j = height-1; j >= 0; j--, u.u8 -= pitch)
{
retro_fwrite(file, u.u8, line_size);
}
return;
}
if (bgr24) /* BGR24 byte order. Can directly copy. */
/* allocate line buffer, and initialize the final four bytes to zero, for deterministic padding */
line = (uint8_t*)malloc(line_size);
if (!line) return;
*(uint32_t*)(line + line_size - 4) = 0;
if (type == RBMP_SOURCE_TYPE_XRGB888)
{
for (j = 0; j < height; j++, u.u8 += pitch)
dump_line_bgr(lines[j], u.u8, width);
for (j = height-1; j >= 0; j--, u.u8 -= pitch)
{
dump_line_32_to_24(line, u.u32, width);
retro_fwrite(file, line, line_size);
}
}
else if (xrgb8888)
else /* type == RBMP_SOURCE_TYPE_RGB565 */
{
for (j = 0; j < height; j++, u.u8 += pitch)
dump_line_32(lines[j], u.u32, width);
}
else /* RGB565 */
{
for (j = 0; j < height; j++, u.u8 += pitch)
dump_line_16(lines[j], u.u16, width);
for (j = height-1; j >= 0; j--, u.u8 -= pitch)
{
dump_line_565_to_24(line, u.u16, width);
retro_fwrite(file, line, line_size);
}
}
dump_lines_file(file, lines, line_size, height);
end:
for (i = 0; i < height; i++)
free(lines[i]);
free(lines);
}
bool rbmp_save_image(const char *filename, const void *frame,
unsigned width, unsigned height,
unsigned pitch, bool bgr24, bool xrgb8888)
unsigned pitch, rbmp_source_type type)
{
bool ret;
RFILE *file = retro_fopen(filename, RFILE_MODE_WRITE, -1);
if (!file)
return false;
ret = write_header_bmp(file, width, height);
ret = write_header_bmp(file, width, height, type==RBMP_SOURCE_TYPE_ARGB8888);
if (ret)
dump_content(file, frame, width, height, pitch, bgr24, xrgb8888);
dump_content(file, frame, width, height, pitch, type);
retro_fclose(file);

View File

@ -25,14 +25,32 @@
#ifdef _MSC_VER
#ifdef __cplusplus
extern "C" {
#endif
/* Pre-MSVC 2015 compilers don't implement snprintf in a cross-platform manner. */
#if _MSC_VER < 1900
#ifndef snprintf
#define snprintf _snprintf
#include <stdlib.h>
#ifndef snprintf
#define snprintf c99_snprintf_retro__
#endif
int c99_snprintf_retro__(char *outBuf, size_t size, const char *format, ...);
#endif
#ifndef vsnprintf
#define vsnprintf _vsnprintf
/* Pre-MSVC 2010 compilers don't implement vsnprintf in a cross-platform manner? Not sure about this one. */
#if _MSC_VER < 1600
#include <stdarg.h>
#include <stdlib.h>
#ifndef vsnprintf
#define vsnprintf c99_vsnprintf_retro__
#endif
int c99_vsnprintf_retro__(char *outBuf, size_t size, const char *format, va_list ap);
#endif
#ifdef __cplusplus
}
#endif
#undef UNICODE /* Do not bother with UNICODE at this time. */

View File

@ -29,9 +29,17 @@
extern "C" {
#endif
typedef enum
{
RBMP_SOURCE_TYPE_BGR24,
RBMP_SOURCE_TYPE_XRGB888,
RBMP_SOURCE_TYPE_RGB565,
RBMP_SOURCE_TYPE_ARGB8888,
} rbmp_source_type;
bool rbmp_save_image(const char *filename, const void *frame,
unsigned width, unsigned height,
unsigned pitch, bool bgr24, bool xrgb8888);
unsigned pitch, rbmp_source_type type);
#ifdef __cplusplus
}

View File

@ -28,6 +28,7 @@
#include <sys/types.h>
#include <retro_common.h>
#include <boolean.h>
#ifdef __cplusplus

View File

@ -28,7 +28,7 @@
#include <retro_inline.h>
#include <retro_miscellaneous.h>
#if defined(__cplusplus) && !defined(_MSC_VER)
#if defined(__cplusplus)
extern "C" {
#endif
@ -170,7 +170,7 @@ int scond_broadcast(scond_t *cond);
**/
void scond_signal(scond_t *cond);
#if defined(__cplusplus) && !defined(_MSC_VER)
#if defined(__cplusplus)
}
#endif