2017-02-25 08:15:36 +00:00
|
|
|
#include "compat.h"
|
2016-06-21 00:33:19 +00:00
|
|
|
#include "build.h"
|
|
|
|
#include "editor.h"
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
#include "pngwrite.h"
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
uint16_t capturecount = 0;
|
2016-06-21 00:33:19 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// screencapture
|
|
|
|
//
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
static int screencapture_begin(char *fn, const char *ext, BFILE** filptr)
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
2016-09-16 21:55:36 +00:00
|
|
|
bssize_t i;
|
2016-06-21 00:33:19 +00:00
|
|
|
|
|
|
|
do // JBF 2004022: So we don't overwrite existing screenshots
|
|
|
|
{
|
|
|
|
if (capturecount > 9999) return -1;
|
|
|
|
|
|
|
|
i = Bstrrchr(fn, '.')-fn-4;
|
|
|
|
fn[i++] = ((capturecount/1000)%10)+48;
|
|
|
|
fn[i++] = ((capturecount/100)%10)+48;
|
|
|
|
fn[i++] = ((capturecount/10)%10)+48;
|
|
|
|
fn[i++] = (capturecount%10)+48;
|
|
|
|
i++;
|
|
|
|
Bstrcpy(&fn[i], ext);
|
|
|
|
|
|
|
|
if ((*filptr = Bfopen(fn, "rb")) == NULL) break;
|
|
|
|
Bfclose(*filptr);
|
|
|
|
capturecount++;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
*filptr = Bfopen(fn, "wb");
|
|
|
|
if (*filptr == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
static void screencapture_end(char *fn, BFILE** filptr)
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
Bfclose(*filptr);
|
|
|
|
OSD_Printf("Saved screenshot to %s\n", fn);
|
|
|
|
Bfree(fn);
|
|
|
|
capturecount++;
|
|
|
|
}
|
|
|
|
|
2016-06-21 00:33:19 +00:00
|
|
|
# ifdef USE_OPENGL
|
|
|
|
# define HICOLOR (getrendermode() >= REND_POLYMOST && in3dmode())
|
|
|
|
# else
|
|
|
|
# define HICOLOR 0
|
|
|
|
# endif
|
2016-09-16 21:55:36 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
int screencapture(const char *filename, char inverseit)
|
|
|
|
{
|
|
|
|
char *fn = Xstrdup(filename);
|
|
|
|
BFILE *fp;
|
|
|
|
int const retval = screencapture_begin(fn, "png", &fp);
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2016-09-16 21:55:36 +00:00
|
|
|
if (retval)
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
Bfree(fn);
|
|
|
|
return retval;
|
2016-06-21 00:33:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
uint8_t * const imgBuf = (uint8_t *) Xmalloc(xdim * ydim * (HICOLOR ? 3 : 1));
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
begindrawing(); //{{{
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
#ifdef USE_OPENGL
|
|
|
|
if (HICOLOR)
|
|
|
|
{
|
|
|
|
bglReadPixels(0, 0, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, imgBuf);
|
|
|
|
int const bytesPerLine = xdim * 3;
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
if (inverseit)
|
|
|
|
{
|
|
|
|
for (bsize_t i=0, j = ydim * bytesPerLine; i<j; i+=3)
|
|
|
|
swapchar(&imgBuf[i], &imgBuf[i+2]);
|
|
|
|
}
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
// flip rows
|
|
|
|
uint8_t* rowBuf = (uint8_t *) Xmalloc(bytesPerLine);
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
for (int i = 0, numRows = ydim >> 1; i < numRows; ++i)
|
|
|
|
{
|
|
|
|
Bmemcpy(rowBuf, imgBuf + i * bytesPerLine, bytesPerLine);
|
|
|
|
Bmemcpy(imgBuf + i * bytesPerLine, imgBuf + (ydim - i - 1) * bytesPerLine, bytesPerLine);
|
|
|
|
Bmemcpy(imgBuf + (ydim - i - 1) * bytesPerLine, rowBuf, bytesPerLine);
|
|
|
|
}
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
Bfree(rowBuf);
|
|
|
|
}
|
|
|
|
else
|
2016-06-21 00:33:19 +00:00
|
|
|
#endif
|
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
struct {
|
|
|
|
uint8_t r, g, b;
|
|
|
|
} palette[256];
|
|
|
|
|
2016-09-16 21:55:36 +00:00
|
|
|
if (inverseit)
|
|
|
|
{
|
|
|
|
for (bssize_t i = 0; i < 256; ++i)
|
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
palette[i].r = 255 - curpalettefaded[i].r;
|
|
|
|
palette[i].g = 255 - curpalettefaded[i].g;
|
|
|
|
palette[i].b = 255 - curpalettefaded[i].b;
|
2016-09-16 21:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
2016-09-16 21:55:36 +00:00
|
|
|
for (bssize_t i = 0; i < 256; ++i)
|
2017-07-18 20:53:00 +00:00
|
|
|
Bmemcpy(&palette[i], &curpalettefaded[i], sizeof(palette[0]));
|
2016-06-21 00:33:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
png_set_pal((uint8_t *)palette, 256);
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
for (int i = 0; i < ydim; ++i)
|
|
|
|
Bmemcpy(imgBuf + i * xdim, (uint8_t *)frameplace + ylookup[i], xdim);
|
2016-06-21 00:33:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
enddrawing(); //}}}
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
png_set_text("Software", osd->version.buf);
|
|
|
|
png_write(fp, xdim, ydim, HICOLOR ? PNG_TRUECOLOR : PNG_INDEXED, imgBuf);
|
|
|
|
Bfree(imgBuf);
|
|
|
|
screencapture_end(fn, &fp);
|
2016-06-21 00:33:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
int screencapture_tga(const char *filename, char inverseit)
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
|
|
|
int32_t i;
|
2017-07-18 20:53:00 +00:00
|
|
|
char head[18] = { 0,1,1,0,0,0,1,24,0,0,0,0,0/*wlo*/,0/*whi*/,0/*hlo*/,0/*hhi*/,8,0 };
|
2016-06-21 00:33:19 +00:00
|
|
|
//char palette[4*256];
|
|
|
|
char *fn = Xstrdup(filename);
|
|
|
|
BFILE *fil;
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
i = screencapture_begin(fn, "tga", &fil);
|
2016-06-21 00:33:19 +00:00
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
Bfree(fn);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
#ifdef USE_OPENGL
|
|
|
|
if (HICOLOR)
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
|
|
|
head[1] = 0; // no colourmap
|
|
|
|
head[2] = 2; // uncompressed truecolour
|
|
|
|
head[3] = 0; // (low) first colourmap index
|
|
|
|
head[4] = 0; // (high) first colourmap index
|
|
|
|
head[5] = 0; // (low) number colourmap entries
|
|
|
|
head[6] = 0; // (high) number colourmap entries
|
|
|
|
head[7] = 0; // colourmap entry size
|
|
|
|
head[16] = 24; // 24 bits per pixel
|
|
|
|
}
|
2017-07-18 20:53:00 +00:00
|
|
|
#endif
|
2016-06-21 00:33:19 +00:00
|
|
|
|
|
|
|
head[12] = xdim & 0xff;
|
|
|
|
head[13] = (xdim >> 8) & 0xff;
|
|
|
|
head[14] = ydim & 0xff;
|
|
|
|
head[15] = (ydim >> 8) & 0xff;
|
|
|
|
|
|
|
|
Bfwrite(head, 18, 1, fil);
|
|
|
|
|
2017-07-29 20:39:46 +00:00
|
|
|
// palette first
|
2017-07-18 20:53:00 +00:00
|
|
|
#ifdef USE_OPENGL
|
|
|
|
if (!HICOLOR)
|
|
|
|
#endif
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
if (inverseit)
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
for (i=0; i<256; i++)
|
|
|
|
{
|
|
|
|
Bfputc(255 - curpalettefaded[i].b, fil);
|
|
|
|
Bfputc(255 - curpalettefaded[i].g, fil);
|
|
|
|
Bfputc(255 - curpalettefaded[i].r, fil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i=0; i<256; i++)
|
|
|
|
{
|
|
|
|
Bfputc(curpalettefaded[i].b, fil);
|
|
|
|
Bfputc(curpalettefaded[i].g, fil);
|
|
|
|
Bfputc(curpalettefaded[i].r, fil);
|
|
|
|
}
|
2016-06-21 00:33:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
begindrawing(); //{{{
|
|
|
|
|
2016-06-21 00:33:19 +00:00
|
|
|
# ifdef USE_OPENGL
|
2017-07-18 20:53:00 +00:00
|
|
|
if (HICOLOR)
|
2016-06-21 00:33:19 +00:00
|
|
|
{
|
|
|
|
// 24bit
|
2017-07-18 20:53:00 +00:00
|
|
|
int const size = xdim * ydim * 3;
|
|
|
|
uint8_t *inversebuf = (uint8_t *) Xmalloc(size);
|
2016-06-21 00:33:19 +00:00
|
|
|
|
|
|
|
bglReadPixels(0, 0, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, inversebuf);
|
2017-07-29 20:39:46 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
for (i = 0; i < size; i += 3)
|
|
|
|
swapchar(&inversebuf[i], &inversebuf[i + 2]);
|
2016-06-21 00:33:19 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
Bfwrite(inversebuf, xdim*ydim, 3, fil);
|
2016-06-21 00:33:19 +00:00
|
|
|
Bfree(inversebuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
# endif
|
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
char * const ptr = (char *) frameplace;
|
|
|
|
|
|
|
|
for (i = ydim-1; i >= 0; i--)
|
|
|
|
Bfwrite(ptr + i * bytesperline, xdim, 1, fil);
|
2016-06-21 00:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enddrawing(); //}}}
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
screencapture_end(fn, &fil);
|
|
|
|
|
2016-06-21 00:34:41 +00:00
|
|
|
return 0;
|
2016-06-21 00:33:19 +00:00
|
|
|
}
|
2017-07-18 20:53:00 +00:00
|
|
|
#undef HICOLOR
|
2016-06-21 00:33:19 +00:00
|
|
|
|