merge total remake

This commit is contained in:
Eduardo Bart
2011-08-13 23:09:11 -03:00
parent 0af7856475
commit 55862b07ad
253 changed files with 6777 additions and 8463 deletions

View File

@@ -1,16 +0,0 @@
#ifndef ALGORITHMS_H
#define ALGORITHMS_H
#include <algorithm>
#include <string>
inline std::string getPathDirectory(const std::string& sourcePath)
{
std::string dir;
std::size_t pos = sourcePath.find_last_of('/');
if(pos != std::string::npos)
dir = sourcePath.substr(0, pos);
return dir;
}
#endif // ALGORITHMS_H

View File

@@ -1,871 +0,0 @@
/* based on APNG Disassembler 2.3
*
* Copyright (c) 2009 Max Stepin
* maxst at users.sourceforge.net
*
* GNU LGPL information
* --------------------
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <zlib.h>
#include "apngloader.h"
#include <iostream>
#include <sstream>
#include <fstream>
#if defined(_MSC_VER) && _MSC_VER >= 1300
#define swap16(data) _byteswap_ushort(data)
#define swap32(data) _byteswap_ulong(data)
#elif __linux__
#include <byteswap.h>
#define swap16(data) bswap_16(data)
#define swap32(data) bswap_32(data)
#else
#define swap16(data) (((data >> 8) & 255) | ((data & 255) << 8))
#define swap32(data) ((swap16(data) << 16) | swap16(data >> 16))
#endif
#define PNG_ZBUF_SIZE 32768
#define PNG_DISPOSE_OP_NONE 0x00
#define PNG_DISPOSE_OP_BACKGROUND 0x01
#define PNG_DISPOSE_OP_PREVIOUS 0x02
#define PNG_BLEND_OP_SOURCE 0x00
#define PNG_BLEND_OP_OVER 0x01
#define notabc(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
#define ROWBYTES(pixel_bits, width) \
((pixel_bits) >= 8 ? \
((width) * (((unsigned int)(pixel_bits)) >> 3)) : \
(( ((width) * ((unsigned int)(pixel_bits))) + 7) >> 3) )
unsigned char png_sign[8] = {137, 80, 78, 71, 13, 10, 26, 10};
int mask4[2]={240,15};
int shift4[2]={4,0};
int mask2[4]={192,48,12,3};
int shift2[4]={6,4,2,0};
int mask1[8]={128,64,32,16,8,4,2,1};
int shift1[8]={7,6,5,4,3,2,1,0};
unsigned int keep_original = 1;
unsigned char pal[256][3];
unsigned char trns[256];
unsigned int palsize, trnssize;
unsigned int hasTRNS;
unsigned short trns1, trns2, trns3;
unsigned int read32(std::istream& f1)
{
unsigned char a, b, c, d;
f1.read((char*)&a, 1);
f1.read((char*)&b, 1);
f1.read((char*)&c, 1);
f1.read((char*)&d, 1);
return ((unsigned int)a<<24)+((unsigned int)b<<16)+((unsigned int)c<<8)+(unsigned int)d;
}
unsigned short read16(std::istream& f1)
{
unsigned char a, b;
f1.read((char*)&a, 1);
f1.read((char*)&b, 1);
return ((unsigned short)a<<8)+(unsigned short)b;
}
unsigned short readshort(unsigned char * p)
{
return ((unsigned short)(*p)<<8)+(unsigned short)(*(p+1));
}
void read_sub_row(unsigned char * row, unsigned int rowbytes, unsigned int bpp)
{
unsigned int i;
for (i=bpp; i<rowbytes; i++)
row[i] += row[i-bpp];
}
void read_up_row(unsigned char * row, unsigned char * prev_row, unsigned int rowbytes, unsigned int bpp)
{
unsigned int i;
if (prev_row)
for (i=0; i<rowbytes; i++)
row[i] += prev_row[i];
}
void read_average_row(unsigned char * row, unsigned char * prev_row, unsigned int rowbytes, unsigned int bpp)
{
unsigned int i;
if (prev_row)
{
for (i=0; i<bpp; i++)
row[i] += prev_row[i]>>1;
for (i=bpp; i<rowbytes; i++)
row[i] += (prev_row[i] + row[i-bpp])>>1;
}
else
{
for (i=bpp; i<rowbytes; i++)
row[i] += row[i-bpp]>>1;
}
}
void read_paeth_row(unsigned char * row, unsigned char * prev_row, unsigned int rowbytes, unsigned int bpp)
{
unsigned int i;
int a, b, c, pa, pb, pc, p;
if (prev_row)
{
for (i=0; i<bpp; i++)
row[i] += prev_row[i];
for (i=bpp; i<rowbytes; i++)
{
a = row[i-bpp];
b = prev_row[i];
c = prev_row[i-bpp];
p = b - c;
pc = a - c;
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
row[i] += ((pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c);
}
}
else
{
for (i=bpp; i<rowbytes; i++)
row[i] += row[i-bpp];
}
}
void unpack(z_stream zstream, unsigned char * dst, unsigned int dst_size, unsigned char * src, unsigned int src_size, unsigned int h, unsigned int rowbytes, unsigned char bpp)
{
unsigned int j;
unsigned char * row = dst;
unsigned char * prev_row = NULL;
zstream.next_out = dst;
zstream.avail_out = dst_size;
zstream.next_in = src;
zstream.avail_in = src_size;
inflate(&zstream, Z_FINISH);
inflateReset(&zstream);
for (j=0; j<h; j++)
{
switch (*row++)
{
case 0: break;
case 1: read_sub_row(row, rowbytes, bpp); break;
case 2: read_up_row(row, prev_row, rowbytes, bpp); break;
case 3: read_average_row(row, prev_row, rowbytes, bpp); break;
case 4: read_paeth_row(row, prev_row, rowbytes, bpp); break;
}
prev_row = row;
row += rowbytes;
}
}
void compose0(unsigned char * dst1, unsigned int dstbytes1, unsigned char * dst2, unsigned int dstbytes2, unsigned char * src, unsigned int srcbytes, unsigned int w, unsigned int h, unsigned int bop, unsigned char depth)
{
unsigned int i, j, g, a;
unsigned char * sp;
unsigned char * dp1;
unsigned int * dp2;
for (j=0; j<h; j++)
{
sp = src+1;
dp1 = dst1;
dp2 = (unsigned int*)dst2;
if (bop == PNG_BLEND_OP_SOURCE)
{
switch (depth)
{
case 16: for (i=0; i<w; i++) { a = 0xFF; if (hasTRNS && readshort(sp)==trns1) a = 0; *dp1++ = *sp; *dp2++ = (a << 24) + (*sp << 16) + (*sp << 8) + *sp; sp+=2; } break;
case 8: for (i=0; i<w; i++) { a = 0xFF; if (hasTRNS && *sp==trns1) a = 0; *dp1++ = *sp; *dp2++ = (a << 24) + (*sp << 16) + (*sp << 8) + *sp; sp++; } break;
case 4: for (i=0; i<w; i++) { g = (sp[i>>1] & mask4[i&1]) >> shift4[i&1]; a = 0xFF; if (hasTRNS && g==trns1) a = 0; *dp1++ = g*0x11; *dp2++ = (a<<24) + g*0x111111; } break;
case 2: for (i=0; i<w; i++) { g = (sp[i>>2] & mask2[i&3]) >> shift2[i&3]; a = 0xFF; if (hasTRNS && g==trns1) a = 0; *dp1++ = g*0x55; *dp2++ = (a<<24) + g*0x555555; } break;
case 1: for (i=0; i<w; i++) { g = (sp[i>>3] & mask1[i&7]) >> shift1[i&7]; a = 0xFF; if (hasTRNS && g==trns1) a = 0; *dp1++ = g*0xFF; *dp2++ = (a<<24) + g*0xFFFFFF; } break;
}
}
else /* PNG_BLEND_OP_OVER */
{
switch (depth)
{
case 16: for (i=0; i<w; i++, dp1++, dp2++) { if (readshort(sp) != trns1) { *dp1 = *sp; *dp2 = 0xFF000000 + (*sp << 16) + (*sp << 8) + *sp; } sp+=2; } break;
case 8: for (i=0; i<w; i++, dp1++, dp2++) { if (*sp != trns1) { *dp1 = *sp; *dp2 = 0xFF000000 + (*sp << 16) + (*sp << 8) + *sp; } sp++; } break;
case 4: for (i=0; i<w; i++, dp1++, dp2++) { g = (sp[i>>1] & mask4[i&1]) >> shift4[i&1]; if (g != trns1) { *dp1 = g*0x11; *dp2 = 0xFF000000+g*0x111111; } } break;
case 2: for (i=0; i<w; i++, dp1++, dp2++) { g = (sp[i>>2] & mask2[i&3]) >> shift2[i&3]; if (g != trns1) { *dp1 = g*0x55; *dp2 = 0xFF000000+g*0x555555; } } break;
case 1: for (i=0; i<w; i++, dp1++, dp2++) { g = (sp[i>>3] & mask1[i&7]) >> shift1[i&7]; if (g != trns1) { *dp1 = g*0xFF; *dp2 = 0xFF000000+g*0xFFFFFF; } } break;
}
}
src += srcbytes;
dst1 += dstbytes1;
dst2 += dstbytes2;
}
}
void compose2(unsigned char * dst1, unsigned int dstbytes1, unsigned char * dst2, unsigned int dstbytes2, unsigned char * src, unsigned int srcbytes, unsigned int w, unsigned int h, unsigned int bop, unsigned char depth)
{
unsigned int i, j;
unsigned int r, g, b, a;
unsigned char * sp;
unsigned char * dp1;
unsigned int * dp2;
for (j=0; j<h; j++)
{
sp = src+1;
dp1 = dst1;
dp2 = (unsigned int*)dst2;
if (bop == PNG_BLEND_OP_SOURCE)
{
if (depth == 8)
{
for (i=0; i<w; i++)
{
b = *sp++;
g = *sp++;
r = *sp++;
a = 0xFF;
if (hasTRNS && b==trns1 && g==trns2 && r==trns3)
a = 0;
*dp1++ = b; *dp1++ = g; *dp1++ = r;
*dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
}
}
else
{
for (i=0; i<w; i++, sp+=6)
{
b = *sp;
g = *(sp+2);
r = *(sp+4);
a = 0xFF;
if (hasTRNS && readshort(sp)==trns1 && readshort(sp+2)==trns2 && readshort(sp+4)==trns3)
a = 0;
*dp1++ = b; *dp1++ = g; *dp1++ = r;
*dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
}
}
}
else /* PNG_BLEND_OP_OVER */
{
if (depth == 8)
{
for (i=0; i<w; i++, sp+=3, dp1+=3, dp2++)
if ((*sp != trns1) || (*(sp+1) != trns2) || (*(sp+2) != trns3))
{
*dp1 = *sp; *(dp1+1) = *(sp+1); *(dp1+2) = *(sp+2);
*dp2 = 0xFF000000 + (*(sp+2) << 16) + (*(sp+1) << 8) + *sp;
}
}
else
{
for (i=0; i<w; i++, sp+=6, dp1+=3, dp2++)
if ((readshort(sp) != trns1) || (readshort(sp+2) != trns2) || (readshort(sp+4) != trns3))
{
*dp1 = *sp; *(dp1+1) = *(sp+2); *(dp1+2) = *(sp+4);
*dp2 = 0xFF000000 + (*(sp+4) << 16) + (*(sp+2) << 8) + *sp;
}
}
}
src += srcbytes;
dst1 += dstbytes1;
dst2 += dstbytes2;
}
}
void compose3(unsigned char * dst1, unsigned int dstbytes1, unsigned char * dst2, unsigned int dstbytes2, unsigned char * src, unsigned int srcbytes, unsigned int w, unsigned int h, unsigned int bop, unsigned char depth)
{
unsigned int i, j;
unsigned int r, g, b, a;
unsigned int r2, g2, b2, a2;
int u, v, al;
unsigned char col = 0;
unsigned char * sp;
unsigned char * dp1;
unsigned int * dp2;
for (j=0; j<h; j++)
{
sp = src+1;
dp1 = dst1;
dp2 = (unsigned int*)dst2;
for (i=0; i<w; i++)
{
switch (depth)
{
case 8: col = sp[i]; break;
case 4: col = (sp[i>>1] & mask4[i&1]) >> shift4[i&1]; break;
case 2: col = (sp[i>>2] & mask2[i&3]) >> shift2[i&3]; break;
case 1: col = (sp[i>>3] & mask1[i&7]) >> shift1[i&7]; break;
}
b = pal[col][0];
g = pal[col][1];
r = pal[col][2];
a = trns[col];
if (bop == PNG_BLEND_OP_SOURCE)
{
*dp1++ = col;
*dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
}
else /* PNG_BLEND_OP_OVER */
{
if (a == 255)
{
*dp1++ = col;
*dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
}
else
if (a != 0)
{
if ((a2 = (*dp2)>>24))
{
keep_original = 0;
u = a*255;
v = (255-a)*a2;
al = 255*255-(255-a)*(255-a2);
b2 = ((*dp2)&255);
g2 = (((*dp2)>>8)&255);
r2 = (((*dp2)>>16)&255);
b = (b*u + b2*v)/al;
g = (g*u + g2*v)/al;
r = (r*u + r2*v)/al;
a = al/255;
}
*dp1++ = col;
*dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
}
else
{
dp1++;
dp2++;
}
}
}
src += srcbytes;
dst1 += dstbytes1;
dst2 += dstbytes2;
}
}
void compose4(unsigned char * dst, unsigned int dstbytes, unsigned char * src, unsigned int srcbytes, unsigned int w, unsigned int h, unsigned int bop, unsigned char depth)
{
unsigned int i, j, step;
unsigned int g, a, g2, a2;
int u, v, al;
unsigned char * sp;
unsigned char * dp;
step = (depth+7)/8;
for (j=0; j<h; j++)
{
sp = src+1;
dp = dst;
if (bop == PNG_BLEND_OP_SOURCE)
{
for (i=0; i<w; i++)
{
g = *sp; sp += step;
a = *sp; sp += step;
*dp++ = g;
*dp++ = a;
}
}
else /* PNG_BLEND_OP_OVER */
{
for (i=0; i<w; i++)
{
g = *sp; sp += step;
a = *sp; sp += step;
if (a == 255)
{
*dp++ = g;
*dp++ = a;
}
else
if (a != 0)
{
if ((a2 = *(dp+1)))
{
u = a*255;
v = (255-a)*a2;
al = 255*255-(255-a)*(255-a2);
g2 = ((*dp)&255);
g = (g*u + g2*v)/al;
a = al/255;
}
*dp++ = g;
*dp++ = a;
}
else
dp+=2;
}
}
src += srcbytes;
dst += dstbytes;
}
}
void compose6(unsigned char * dst, unsigned int dstbytes, unsigned char * src, unsigned int srcbytes, unsigned int w, unsigned int h, unsigned int bop, unsigned char depth)
{
unsigned int i, j, step;
unsigned int r, g, b, a;
unsigned int r2, g2, b2, a2;
int u, v, al;
unsigned char * sp;
unsigned int * dp;
step = (depth+7)/8;
for (j=0; j<h; j++)
{
sp = src+1;
dp = (unsigned int*)dst;
if (bop == PNG_BLEND_OP_SOURCE)
{
for (i=0; i<w; i++)
{
b = *sp; sp += step;
g = *sp; sp += step;
r = *sp; sp += step;
a = *sp; sp += step;
*dp++ = (a << 24) + (r << 16) + (g << 8) + b;
}
}
else /* PNG_BLEND_OP_OVER */
{
for (i=0; i<w; i++)
{
b = *sp; sp += step;
g = *sp; sp += step;
r = *sp; sp += step;
a = *sp; sp += step;
if (a == 255)
*dp++ = (a << 24) + (r << 16) + (g << 8) + b;
else
if (a != 0)
{
if ((a2 = (*dp)>>24))
{
u = a*255;
v = (255-a)*a2;
al = 255*255-(255-a)*(255-a2);
b2 = ((*dp)&255);
g2 = (((*dp)>>8)&255);
r2 = (((*dp)>>16)&255);
b = (b*u + b2*v)/al;
g = (g*u + g2*v)/al;
r = (r*u + r2*v)/al;
a = al/255;
}
*dp++ = (a << 24) + (r << 16) + (g << 8) + b;
}
else
dp++;
}
}
src += srcbytes;
dst += dstbytes;
}
}
int load_apng(std::stringstream& file, struct apng_data *apng)
{
unsigned int i, j;
unsigned int rowbytes;
int imagesize, zbuf_size, zsize, trns_idx;
unsigned int len, chunk, crc;
unsigned int w, h, seq, w0, h0, x0, y0;
unsigned int frames, loops, first_frame, cur_frame;
unsigned int outrow1, outrow2, outimg1, outimg2;
unsigned short d1, d2;
unsigned char c, dop, bop;
unsigned char channels, depth, pixeldepth, bpp;
unsigned char coltype, compr, filter, interl;
z_stream zstream;
memset(apng, 0, sizeof(struct apng_data));
for (i=0; i<256; i++)
{
pal[i][0] = i;
pal[i][1] = i;
pal[i][2] = i;
trns[i] = 255;
}
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL;
inflateInit(&zstream);
frames = 1;
first_frame = 0;
cur_frame = 0;
zsize = 0;
hasTRNS = 0;
trns_idx = -1;
x0 = 0;
y0 = 0;
loops = 0;
bop = PNG_BLEND_OP_SOURCE;
unsigned char sig[8];
unsigned char * pOut1;
unsigned char * pOut2;
unsigned char * pTemp;
unsigned char * pData;
unsigned char * pImg1;
unsigned char * pImg2;
unsigned char * pDst1;
unsigned char * pDst2;
unsigned int * frames_delay;
file.read((char*)sig, 8);
if(!file.eof() && memcmp(sig, png_sign, 8) == 0) {
len = read32(file);
chunk = read32(file);
if ((len == 13) && (chunk == 0x49484452)) /* IHDR */
{
w = w0 = read32(file);
h = h0 = read32(file);
file.read((char*)&depth, 1);
file.read((char*)&coltype, 1);
file.read((char*)&compr, 1);
file.read((char*)&filter, 1);
file.read((char*)&interl, 1);
crc = read32(file);
channels = 1;
if (coltype == 2)
channels = 3;
else if (coltype == 4)
channels = 2;
else if (coltype == 6)
channels = 4;
pixeldepth = depth*channels;
bpp = (pixeldepth + 7) >> 3;
rowbytes = ROWBYTES(pixeldepth, w);
imagesize = (rowbytes + 1) * h;
zbuf_size = imagesize + ((imagesize + 7) >> 3) + ((imagesize + 63) >> 6) + 11;
/*
* We'll render into 2 output buffers, first in original coltype,
* second in RGBA.
*
* It's better to try to keep the original coltype, but if dispose/blend
* operations will make it impossible, then we'll save RGBA version instead.
*/
outrow1 = w*channels; /* output coltype = input coltype */
outrow2 = w*4; /* output coltype = RGBA */
outimg1 = h*outrow1;
outimg2 = h*outrow2;
pOut1=(unsigned char *)malloc(outimg1);
pOut2=(unsigned char *)malloc(outimg2);
pTemp=(unsigned char *)malloc(imagesize);
pData=(unsigned char *)malloc(zbuf_size);
pImg1=pOut1;
pImg2=pOut2;
frames_delay = NULL;
/* apng decoding - begin */
memset(pOut1, 0, outimg1);
memset(pOut2, 0, outimg2);
while (!file.eof())
{
len = read32(file);
chunk = read32(file);
if (chunk == 0x504C5445) /* PLTE */
{
unsigned int col;
for (i=0; i<len; i++)
{
file.read((char*)&c, 1);
col = i/3;
if (col<256)
{
pal[col][i%3] = c;
palsize = col+1;
}
}
crc = read32(file);
}
else if (chunk == 0x74524E53) /* tRNS */
{
hasTRNS = 1;
for (i=0; i<len; i++)
{
file.read((char*)&c, 1);
if (i<256)
{
trns[i] = c;
trnssize = i+1;
if (c == 0 && coltype == 3 && trns_idx == -1)
trns_idx = i;
}
}
if (coltype == 0)
{
trns1 = readshort(&trns[0]);
if (depth == 16)
{
trns[1] = trns[0]; trns[0] = 0;
}
}
else
if (coltype == 2)
{
trns1 = readshort(&trns[0]);
trns2 = readshort(&trns[2]);
trns3 = readshort(&trns[4]);
if (depth == 16)
{
trns[1] = trns[0]; trns[0] = 0;
trns[3] = trns[2]; trns[2] = 0;
trns[5] = trns[4]; trns[4] = 0;
}
}
crc = read32(file);
}
else if (chunk == 0x6163544C) /* acTL */
{
frames = read32(file);
if(frames_delay)
free(frames_delay);
frames_delay = (unsigned int*)malloc(frames*sizeof(int));
loops = read32(file);
crc = read32(file);
if (pOut1)
free(pOut1);
if (pOut2)
free(pOut2);
pOut1 = (unsigned char *)malloc((frames+1)*outimg1);
pOut2 = (unsigned char *)malloc((frames+1)*outimg2);
pImg1 = pOut1;
pImg2 = pOut2;
memset(pOut1, 0, outimg1);
memset(pOut2, 0, outimg2);
}
else if (chunk == 0x6663544C) /* fcTL */
{
if (zsize == 0)
first_frame = 1;
else
{
if (dop == PNG_DISPOSE_OP_PREVIOUS)
{
if (coltype != 6)
memcpy(pImg1 + outimg1, pImg1, outimg1);
if (coltype != 4)
memcpy(pImg2 + outimg2, pImg2, outimg2);
}
pDst1 = pImg1 + y0*outrow1 + x0*channels;
pDst2 = pImg2 + y0*outrow2 + x0*4;
unpack(zstream, pTemp, imagesize, pData, zsize, h0, rowbytes, bpp);
switch (coltype)
{
case 0: compose0(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 2: compose2(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 3: compose3(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 4: compose4(pDst1, outrow1, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 6: compose6( pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
}
zsize = 0;
if (dop != PNG_DISPOSE_OP_PREVIOUS)
{
if (coltype != 6)
memcpy(pImg1 + outimg1, pImg1, outimg1);
if (coltype != 4)
memcpy(pImg2 + outimg2, pImg2, outimg2);
if (dop == PNG_DISPOSE_OP_BACKGROUND)
{
pDst1 += outimg1;
pDst2 += outimg2;
for (j=0; j<h0; j++)
{
switch (coltype)
{
case 0: memset(pDst2, 0, w0*4); if (hasTRNS) memset(pDst1, trns[1], w0); else keep_original = 0; break;
case 2: memset(pDst2, 0, w0*4); if (hasTRNS) for (i=0; i<w0; i++) { pDst1[i*3] = trns[1]; pDst1[i*3+1] = trns[3]; pDst1[i*3+2] = trns[5]; } else keep_original = 0; break;
case 3: memset(pDst2, 0, w0*4); if (trns_idx >= 0) memset(pDst1, trns_idx, w0); else keep_original = 0; break;
case 4: memset(pDst1, 0, w0*2); break;
case 6: memset(pDst2, 0, w0*4); break;
}
pDst1 += outrow1;
pDst2 += outrow2;
}
}
}
}
seq = read32(file);
w0 = read32(file);
h0 = read32(file);
x0 = read32(file);
y0 = read32(file);
d1 = read16(file);
d2 = read16(file);
file.read((char*)&dop, 1);
file.read((char*)&bop, 1);
crc = read32(file);
if(d2 == 0)
d2 = 100;
frames_delay[cur_frame] = (d1 * 1000)/d2;
if (cur_frame == 0)
{
bop = PNG_BLEND_OP_SOURCE;
if (dop == PNG_DISPOSE_OP_PREVIOUS)
dop = PNG_DISPOSE_OP_BACKGROUND;
}
if (!(coltype & 4) && !(hasTRNS))
bop = PNG_BLEND_OP_SOURCE;
rowbytes = ROWBYTES(pixeldepth, w0);
cur_frame++;
pImg1 += outimg1;
pImg2 += outimg2;
}
else if (chunk == 0x49444154) /* IDAT */
{
file.read((char*)(pData + zsize), len);
zsize += len;
crc = read32(file);
}
else if (chunk == 0x66644154) /* fdAT */
{
seq = read32(file);
len -= 4;
file.read((char*)(pData + zsize), len);
zsize += len;
crc = read32(file);
}
else if (chunk == 0x49454E44) /* IEND */
{
pDst1 = pImg1 + y0*outrow1 + x0*channels;
pDst2 = pImg2 + y0*outrow2 + x0*4;
unpack(zstream, pTemp, imagesize, pData, zsize, h0, rowbytes, bpp);
switch (coltype)
{
case 0: compose0(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 2: compose2(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 3: compose3(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 4: compose4(pDst1, outrow1, pTemp, rowbytes+1, w0, h0, bop, depth); break;
case 6: compose6( pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth); break;
}
break;
}
else
{
c = (unsigned char)(chunk>>24);
if (notabc(c)) break;
c = (unsigned char)((chunk>>16) & 0xFF);
if (notabc(c)) break;
c = (unsigned char)((chunk>>8) & 0xFF);
if (notabc(c)) break;
c = (unsigned char)(chunk & 0xFF);
if (notabc(c)) break;
file.seekg(len, std::ios_base::cur);
crc = read32(file);
}
}
/* apng decoding - end */
if (coltype == 0)
{
switch (depth)
{
case 4: trns[1] *= 0x11; break;
case 2: trns[1] *= 0x55; break;
case 1: trns[1] *= 0xFF; break;
}
}
inflateEnd(&zstream);
apng->bpp = channels;
apng->coltype = coltype;
apng->last_frame = cur_frame;
apng->first_frame = first_frame;
apng->height = h;
apng->width = w;
apng->num_frames = frames;
apng->num_plays = loops;
apng->frames_delay = frames_delay;
apng->pdata = pOut2;
apng->bpp = 4;
apng->coltype = 6;
if(pData)
free(pData);
if(pTemp)
free(pTemp);
if(pOut1)
free(pOut1);
} else
return -1;
} else
return -1;
return 0;
}
void free_apng(struct apng_data *apng)
{
if(apng->pdata)
free(apng->pdata);
if(apng->frames_delay)
free(apng->frames_delay);
}

View File

@@ -1,47 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* 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.
*/
#ifndef APNGLOADER_H
#define APNGLOADER_H
#include <sstream>
struct apng_data {
unsigned char *pdata;
unsigned int width;
unsigned int height;
unsigned int first_frame;
unsigned int last_frame;
unsigned char bpp;
unsigned char coltype;
unsigned int num_frames;
unsigned int num_plays;
unsigned int *frames_delay; // each frame delay in ms
};
// returns -1 on error, 0 on success
int load_apng(std::stringstream& file, struct apng_data *apng);
void free_apng(struct apng_data *apng);
#endif // APNGLOADER_H

View File

@@ -0,0 +1,194 @@
#ifndef AUXILIARY_H
#define AUXILIARY_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <cxxabi.h>
/// Namespace that contains auxiliary functions and templates
namespace aux {
/// Fill an ostream by concatenating args
/// Usage:
/// aux::fill_ostream(stream, a1, a2, ..., aN);
inline void fill_ostream(std::ostringstream&) { }
template<class T, class... Args>
void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) {
stream << first;
fill_ostream(stream, rest...);
}
/// Makes a std::string by concatenating args
/// Usage:
/// std::string str = aux::make_string(a1, a2, ..., aN);
template<class... T>
std::string make_string(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
return buf.str();
}
// used by dumper
struct dump_util {
~dump_util() { std::cout << std::endl; }
template<class T>
dump_util& operator<<(const T& v) {
std::cout << v << " ";
return *this;
}
};
/// Utility for dumping variables
/// Usage:
/// aux::dump << v1, v2, ..., vN;
static const struct dumper {
dumper() { }
template<class T>
dump_util operator<<(const T& v) const {
dump_util d;
d << v;
return d;
}
} dump;
/// Utility for printing messages into stdout
/// Usage:
/// aux::print(v1, v2, ..., vN);
template<class... T>
void print(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
std::cout << buf.str();
}
/// Same as aux::print but adds a new line at the end
template<class... T>
void println(const T&... args) {
print(args...);
std::cout << std::endl;
}
/// Demangle names for GNU g++ compiler
inline std::string demangle_name(const char* name) {
size_t len;
int status;
std::string ret;
char* demangled = abi::__cxa_demangle(name, 0, &len, &status);
if(demangled) {
ret = demangled;
free(demangled);
}
return ret;
}
/// Returns the name of a type
/// e.g. aux::demangle_type<Foo*>() returns a string containing 'Foo*'
template<typename T>
std::string demangle_type() {
return demangle_name(typeid(T).name());
}
/// Cast a type to another type
/// @return whether the conversion was successful or not
template<typename T, typename R>
bool cast(const T& in, R& out) {
std::stringstream ss;
ss << in;
ss >> out;
return !!ss && ss.eof();
}
// cast a type to string
template<typename T>
bool cast(const T& in, std::string& out) {
std::stringstream ss;
ss << in;
out = ss.str();
return true;
}
// cast string to string
template<>
inline bool cast(const std::string& in, std::string& out) {
out = in;
return true;
}
// special cast from string to boolean
template<>
inline bool cast(const std::string& in, bool& b) {
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
bool ret = false;
for(int i=0;i<4;++i) {
if(in == validNames[0][i]) {
b = true;
ret = true;
break;
} else if(in == validNames[1][i]) {
b = false;
ret = true;
break;
}
}
return ret;
}
// special cast from boolean to string
template<>
inline bool cast(const bool& in, std::string& out) {
out = (in ? "true" : "false");
return true;
}
// used by safe_cast
class bad_cast : public std::bad_cast {
public:
virtual ~bad_cast() throw() { }
template<class T, class R>
void setWhat() {
m_what = make_string("failed to cast value of type '", demangle_type<T>(),
"' to type '", demangle_type<R>(), "'");
}
virtual const char* what() { return m_what.c_str(); }
private:
std::string m_what;
};
/// Cast a type to another type, any error throws a aux::bad_cast_exception
/// Usage:
/// R r = aux::safe_cast<R>(t);
template<typename R, typename T>
R safe_cast(const T& t) {
R r;
if(!cast(t, r)) {
bad_cast e;
e.setWhat<T,R>();
throw e;
}
return r;
}
/// Cast a type to another type, cast errors are ignored
/// Usage:
/// R r = aux::unsafe_cast<R>(t);
template<typename R, typename T>
R unsafe_cast(const T& t) {
R r;
try {
r = safe_cast<R,T>(t);
} catch(bad_cast& e) {
println(e.what());
}
return r;
}
// an empty string to use anywhere needed
const static std::string empty_string;
}
// shortcut for aux::dump
const static aux::dumper& dump = aux::dump;
#endif

View File

@@ -1,4 +1,4 @@
#include <util/color.h>
#include "color.h"
Color Color::white(0xFF, 0xFF, 0xFF, 0xFF);
Color Color::black(0x00, 0x00, 0x00, 0xFF);

View File

@@ -2,35 +2,35 @@
#define COLOR_H
#include "types.h"
#include <ostream>
#include <sstream>
typedef uint32 RGBA;
class Color
{
public:
inline Color() : color(0) { }
inline Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : color(((a & 0xff)<<24) | ((b & 0xff)<<16) | ((g & 0xff)<<8) | (r & 0xff)) { }
inline Color(const Color& other) : color(other.color) { }
inline Color(RGBA rgba) : color(rgba) { }
Color() : color(0) { }
Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : color(((a & 0xff)<<24) | ((b & 0xff)<<16) | ((g & 0xff)<<8) | (r & 0xff)) { }
Color(const Color& other) : color(other.color) { }
Color(RGBA rgba) : color(rgba) { }
inline uint8 a() const { return (color >> 24) & 0xFF; }
inline uint8 b() const { return (color >> 16) & 0xFF; }
inline uint8 g() const { return (color >> 8) & 0xFF; }
inline uint8 r() const { return color & 0xFF; }
inline RGBA rgba() const { return color; }
inline const uint8* rgbaPtr() const { return (const uint8*)&color; }
uint8 a() const { return (color >> 24) & 0xFF; }
uint8 b() const { return (color >> 16) & 0xFF; }
uint8 g() const { return (color >> 8) & 0xFF; }
uint8 r() const { return color & 0xFF; }
RGBA rgba() const { return color; }
const uint8* rgbaPtr() const { return (const uint8*)&color; }
inline void setAlpha(uint8 a) { color = ((a & 0xff)<<24) | (color & 0x00ffffff); }
inline void setBlue(uint8 b) { color = ((b & 0xff)<<16) | (color & 0xff00ffff); }
inline void setGreen(uint8 g) { color = ((g & 0xff)<<8) | (color & 0xffff00ff); }
inline void setRed(uint8 r) { color = (r & 0xff) | (color & 0xffffff00); }
inline void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) { color = ((a & 0xff)<<24) | ((b & 0xff)<<16) | ((g & 0xff)<<8) | (r & 0xff); }
inline void setRGBA(uint32 rgba) { color = rgba; }
void setAlpha(uint8 a) { color = ((a & 0xff)<<24) | (color & 0x00ffffff); }
void setBlue(uint8 b) { color = ((b & 0xff)<<16) | (color & 0xff00ffff); }
void setGreen(uint8 g) { color = ((g & 0xff)<<8) | (color & 0xffff00ff); }
void setRed(uint8 r) { color = (r & 0xff) | (color & 0xffffff00); }
void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) { color = ((a & 0xff)<<24) | ((b & 0xff)<<16) | ((g & 0xff)<<8) | (r & 0xff); }
void setRGBA(uint32 rgba) { color = rgba; }
inline Color& operator=(const Color& other) { color = other.color; return *this; }
inline bool operator==(const Color& other) const { return other.color == color; }
inline bool operator!=(const Color& other) const { return other.color != color; }
Color& operator=(const Color& other) { color = other.color; return *this; }
bool operator==(const Color& other) const { return other.color == color; }
bool operator!=(const Color& other) const { return other.color != color; }
static Color white;
static Color black;
@@ -51,6 +51,14 @@ inline std::ostream& operator<<(std::ostream& out, const Color& color)
<< (int)color.b() << ","
<< (int)color.a() << ")";
return out;
}
inline std::istream& operator>>(std::istream& in, Color& color)
{
int r, g, b, a;
in >> r >> g >> b >> a;
color.setRGBA(r, g, b, a);
return in;
}
#endif // COLOR_H
#endif

View File

@@ -1,30 +0,0 @@
#include "convert.h"
bool safe_convert(const std::string& input, bool& b) {
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
bool ret = false;
for(int i=0;i<4;++i) {
if(input == validNames[0][i]) {
b = true;
ret = true;
break;
} else if(input == validNames[1][i]) {
b = false;
ret = true;
break;
}
}
return ret;
}
bool safe_convert(const std::string& input, std::string& output)
{
output = input;
return true;
}
bool safe_convert(const bool& b, std::string& out)
{
out = (b ? "true" : "false");
return true;
}

View File

@@ -1,16 +0,0 @@
#ifndef CONVERT_H
#define CONVERT_H
#include <string>
#include <sstream>
bool safe_convert(const std::string& input, bool& b);
bool safe_convert(const std::string& input, std::string& output);
bool safe_convert(const bool& b, std::string& out);
template <typename V, typename T>
bool safe_convert(const V& in, T& out) {std::stringstream ss; ss << in; ss >> out; return !!ss; }
template<typename R, typename T>
R convert(const T& t) { R r; safe_convert(t, r); return r; }
#endif // CONVERT_H

View File

@@ -1,7 +0,0 @@
#ifndef FOREACH_H
#define FOREACH_H
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
#endif // FOREACH_H

View File

@@ -1,9 +1,8 @@
#include "logger.h"
#include <iostream>
#include <cstdlib>
void log(LogLevel level, const std::string& message, std::string prettyFunction)
void Logger::log(LogLevel level, const std::string& message, std::string prettyFunction)
{
if(!prettyFunction.empty()) {
prettyFunction = prettyFunction.substr(0, prettyFunction.find_first_of('('));

View File

@@ -1,37 +1,32 @@
#ifndef LOGGER_H
#define LOGGER_H
#include "makestring.h"
#include "auxiliary.h"
enum LogLevel {
LogDebug = 0,
LogInfo,
LogWarning,
LogError,
LogFatal
class Logger
{
public:
enum LogLevel {
LogDebug = 0,
LogInfo,
LogWarning,
LogError,
LogFatal
};
static void log(LogLevel level, const std::string& message, std::string prettyFunction = "");
};
void log(LogLevel level, const std::string& message, std::string prettyFunction = "");
// specialized logging
#define logDebug(...) log(LogDebug, make_string(__VA_ARGS__))
#define logInfo(...) log(LogInfo, make_string(__VA_ARGS__))
#define logWarning(...) log(LogWarning, make_string(__VA_ARGS__))
#define logError(...) log(LogError, make_string(__VA_ARGS__))
#define logFatal(...) log(LogFatal, make_string(__VA_ARGS__))
#define logDebug(...) Logger::log(Logger::LogDebug, aux::make_string(__VA_ARGS__))
#define logInfo(...) Logger::log(Logger::LogInfo, aux::make_string(__VA_ARGS__))
#define logWarning(...) Logger::log(Logger::LogWarning, aux::make_string(__VA_ARGS__))
#define logError(...) Logger::log(Logger::LogError, aux::make_string(__VA_ARGS__))
#define logFatal(...) Logger::log(Logger::LogFatal, aux::make_string(__VA_ARGS__))
#define logTrace() log(LogDebug, "", __PRETTY_FUNCTION__)
#define logTraceDebug(...) log(LogDebug, make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) log(LogInfo, make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) log(LogWarning, make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) log(LogError, make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTrace() Logger::log(Logger::LogDebug, "", __PRETTY_FUNCTION__)
#define logTraceDebug(...) Logger::log(Logger::LogDebug, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) Logger::log(Logger::LogInfo, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) log(Logger::LogWarning, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) Logger::log(Logger::LogError, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
// dump utility
struct Dump {
~Dump() { logDebug(s.str().c_str()); }
template<class T>
Dump& operator<<(const T& v) { s << v << " "; return *this; }
std::ostringstream s;
};
#define dump Dump()
#endif // LOGGER_H
#endif

View File

@@ -1,21 +0,0 @@
#ifndef MAKESTRING_H
#define MAKESTRING_H
#include <sstream>
#include <string>
inline void fill_ostream(std::ostringstream&) { }
template<class T, class... Args>
void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) {
stream << first;
fill_ostream(stream, rest...);
}
template<class... T>
std::string make_string(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
return buf.str();
}
#endif // MAKESTRING_H

View File

@@ -2,50 +2,50 @@
#define POINT_H
#include "types.h"
#include <ostream>
#include <sstream>
#include <cmath>
template <class T>
template<class T>
class TSize;
template <class T>
template<class T>
class TPoint
{
public:
inline TPoint() : x(0), y(0) {};
inline TPoint(T x, T y) : x(x), y(y) { };
inline TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { };
TPoint() : x(0), y(0) {};
TPoint(T x, T y) : x(x), y(y) { };
TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { };
inline bool isNull() const { return x==0 && y==0; }
inline TSize<T> toSize() const { return TSize<T>(x, y); }
bool isNull() const { return x==0 && y==0; }
TSize<T> toSize() const { return TSize<T>(x, y); }
inline TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
inline TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y); }
inline TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
inline TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y); }
inline TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
inline TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y); }
inline TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
inline TPoint<T> operator*(const T v) const { return TPoint<T>(x * v, y * v); }
inline TPoint<T>& operator*=(const T v) { x*=v; y*=v; return *this; }
inline TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y); }
inline TPoint<T>& operator/=(const TPoint<T>& other) { x/=other.x; y/=other.y; return *this; }
inline TPoint<T> operator/(const T v) const { return TPoint<T>(x/v, y/v); }
inline TPoint<T>& operator/=(const T v) { x/=v; y/=v; return *this; }
TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y); }
TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y); }
TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y); }
TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
TPoint<T> operator*(const T v) const { return TPoint<T>(x * v, y * v); }
TPoint<T>& operator*=(const T v) { x*=v; y*=v; return *this; }
TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y); }
TPoint<T>& operator/=(const TPoint<T>& other) { x/=other.x; y/=other.y; return *this; }
TPoint<T> operator/(const T v) const { return TPoint<T>(x/v, y/v); }
TPoint<T>& operator/=(const T v) { x/=v; y/=v; return *this; }
inline bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
inline bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
inline bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
inline bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
inline TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
inline bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
inline bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
inline float length() const { return sqrt((float)(x*x + y*y)); }
inline T manhattanLength() const { return std::abs(x) + std::abs(y); }
float length() const { return sqrt((float)(x*x + y*y)); }
T manhattanLength() const { return std::abs(x) + std::abs(y); }
inline float distanceFrom(const TPoint<T>& other) const {
float distanceFrom(const TPoint<T>& other) const {
return TPoint<T>(x - other.x, y - other.y).getLength();
}
@@ -55,12 +55,20 @@ public:
typedef TPoint<int> Point;
typedef TPoint<float> PointF;
template <class T>
inline std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
template<class T>
std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
{
out << "Point(" << point.x << ","
<< point.y << ")";
return out;
}
template<class T>
std::istream& operator>>(std::istream& in, TPoint<T>& point)
{
in >> point.x;
in >> point.y;
return in;
}
#endif

View File

@@ -2,89 +2,89 @@
#define RECT_H
#include "types.h"
#include <ostream>
#include <sstream>
template <class T>
template<class T>
class TPoint;
template <class T>
template<class T>
class TSize;
template <class T>
template<class T>
class TRect
{
public:
inline TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
inline TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
inline TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
inline TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
inline TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
inline TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x1+size.width()-1), y2(y1+size.height()-1) { }
TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x1+size.width()-1), y2(y1+size.height()-1) { }
inline bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
inline bool isEmpty() const { return x1 > x2 || y1 > y2; }
inline bool isValid() const { return x1 <= x2 && y1 <= y2; }
bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
bool isEmpty() const { return x1 > x2 || y1 > y2; }
bool isValid() const { return x1 <= x2 && y1 <= y2; }
inline T left() const { return x1; }
inline T top() const { return y1; }
inline T right() const { return x2; }
inline T bottom() const { return y2; }
inline T horizontalCenter() const { return x1 + (x2 - x1)/2; }
inline T verticalCenter() const { return y1 + (y2 - y1)/2; }
inline T x() const { return x1; }
inline T y() const { return y1; }
inline TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
inline TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
inline TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
inline TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
inline TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
inline T width() const { return x2 - x1 + 1; }
inline T height() const { return y2 - y1 + 1; }
inline TSize<T> size() const { return TSize<T>(width(), height()); }
inline void reset() { x1 = y1 = 0; x2 = y2 = -1; }
inline void clear() { x2 = x1 - 1; y2 = y1 - 1; }
T left() const { return x1; }
T top() const { return y1; }
T right() const { return x2; }
T bottom() const { return y2; }
T horizontalCenter() const { return x1 + (x2 - x1)/2; }
T verticalCenter() const { return y1 + (y2 - y1)/2; }
T x() const { return x1; }
T y() const { return y1; }
TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
T width() const { return x2 - x1 + 1; }
T height() const { return y2 - y1 + 1; }
TSize<T> size() const { return TSize<T>(width(), height()); }
void reset() { x1 = y1 = 0; x2 = y2 = -1; }
void clear() { x2 = x1 - 1; y2 = y1 - 1; }
inline void setLeft(T pos) { x1 = pos; }
inline void setTop(T pos) { y1 = pos; }
inline void setRight(T pos) { x2 = pos; }
inline void setBottom(T pos) { y2 = pos; }
inline void setX(T x) { x1 = x; }
inline void setY(T y) { y1 = y; }
inline void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
inline void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
inline void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
inline void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
inline void setWidth(T width) { x2 = x1 + width - 1; }
inline void setHeight(T height) { y2 = y1 + height- 1; }
inline void setSize(T width, T height) { x2 = x1 + width - 1; y2 = y1 + height - 1; }
inline void setSize(const TSize<T>& size) { x2 = x1 + size.width() - 1; y2 = y1 + size.height() - 1; }
inline void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
inline void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
void setLeft(T pos) { x1 = pos; }
void setTop(T pos) { y1 = pos; }
void setRight(T pos) { x2 = pos; }
void setBottom(T pos) { y2 = pos; }
void setX(T x) { x1 = x; }
void setY(T y) { y1 = y; }
void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
void setWidth(T width) { x2 = x1 + width - 1; }
void setHeight(T height) { y2 = y1 + height- 1; }
void setSize(T width, T height) { x2 = x1 + width - 1; y2 = y1 + height - 1; }
void setSize(const TSize<T>& size) { x2 = x1 + size.width() - 1; y2 = y1 + size.height() - 1; }
void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
inline void addLeft(T add) { x1 -= add; }
inline void addTop(T add) { y1 -= add; }
inline void addRight(T add) { x2 += add; }
inline void addBottom(T add) { y2 += add; }
void addLeft(T add) { x1 -= add; }
void addTop(T add) { y1 -= add; }
void addRight(T add) { x2 += add; }
void addBottom(T add) { y2 += add; }
inline void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
inline void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
inline void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
inline void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
inline void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
inline void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
inline void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
inline void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
inline void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
inline void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
inline void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
inline void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
inline TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
inline TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x, y1 + p.y), TPoint<T>(x2 + p.x, y2 + p.y)); }
TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x, y1 + p.y), TPoint<T>(x2 + p.x, y2 + p.y)); }
inline TRect<T> expanded(T pixels) const { return TRect<T>(TPoint<T>(x1 - pixels, y1 - pixels), TPoint<T>(x2 + pixels, y2 + pixels)); }
TRect<T> expanded(T pixels) const { return TRect<T>(TPoint<T>(x1 - pixels, y1 - pixels), TPoint<T>(x2 + pixels, y2 + pixels)); }
inline void moveCenter(const TPoint<T> &p) {
void moveCenter(const TPoint<T> &p) {
T w = x2 - x1;
T h = y2 - y1;
x1 = p.x - w/2;
@@ -92,18 +92,18 @@ public:
x2 = x1 + w;
y2 = y1 + h;
}
inline void moveHorizontalCenter(T x) {
void moveHorizontalCenter(T x) {
T w = x2 - x1;
x1 = x - w/2;
x2 = x1 + w;
}
inline void moveVerticalCenter(T y) {
void moveVerticalCenter(T y) {
T h = y2 - y1;
y1 = y - h/2;
y2 = y1 + h;
}
inline bool contains(const TPoint<T> &p, bool insideOnly = false) const {
bool contains(const TPoint<T> &p, bool insideOnly = false) const {
T l, r;
if(x2 < x1 - 1) {
l = x2;
@@ -137,7 +137,7 @@ public:
return true;
}
inline bool intersects(const TRect<T> &r) const {
bool intersects(const TRect<T> &r) const {
if(isNull() || r.isNull())
return false;
@@ -178,7 +178,7 @@ public:
return true;
}
inline TRect<T> united(const TRect<T> &r) const {
TRect<T> united(const TRect<T> &r) const {
TRect<T> tmp;
tmp.x1 = std::min(x1, r.x1);
tmp.x2 = std::max(x2, r.x2);
@@ -187,7 +187,7 @@ public:
return tmp;
}
inline TRect<T> intersection(const TRect<T> &r) const {
TRect<T> intersection(const TRect<T> &r) const {
if(isNull())
return r;
if(r.isNull())
@@ -229,12 +229,12 @@ public:
return tmp;
}
inline TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
inline bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
inline bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
inline TRect<T>& operator|=(const TRect<T>& other) { *this = united(other); return *this; }
inline TRect<T>& operator&=(const TRect<T>& other) { *this = intersection(other); return *this; }
TRect<T>& operator|=(const TRect<T>& other) { *this = united(other); return *this; }
TRect<T>& operator&=(const TRect<T>& other) { *this = intersection(other); return *this; }
private:
T x1, y1, x2, y2;
@@ -243,14 +243,20 @@ private:
typedef TRect<int> Rect;
typedef TRect<float> RectF;
template <class T>
inline std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
template<class T>
std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
{
out << "Rect(" << rect.left() << ","
<< rect.top() << ","
<< rect.width() << ","
<< rect.height() << ")";
out << rect.left() << " " << rect.top() << " " << rect.width() << " " << rect.height();
return out;
}
#endif // RECT_H
template<class T>
std::istream& operator>>(std::istream& in, TRect<T>& rect)
{
T x, y , w, h;
in >> x >> y >> w >> h;
rect.setRect(x,y,w,h);
return in;
}
#endif

View File

@@ -9,50 +9,50 @@ enum ESizeScaleMode {
KEEP_ASPECT_RATIO_BY_EXPANDING
};
template <class T>
template<class T>
class TSize
{
public:
inline TSize() : wd(-1), ht(-1) {};
inline TSize(T width, T height) : wd(width), ht(height) { };
inline TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
TSize() : wd(-1), ht(-1) {};
TSize(T width, T height) : wd(width), ht(height) { };
TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
inline TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
inline bool isNull() const { return wd==0 && ht==0; }
inline bool isEmpty() const { return wd<1 || ht<1; }
inline bool isValid() const { return wd>=0 && ht>=0; }
bool isNull() const { return wd==0 && ht==0; }
bool isEmpty() const { return wd<1 || ht<1; }
bool isValid() const { return wd>=0 && ht>=0; }
inline int width() const { return wd; }
inline int height() const { return ht; }
int width() const { return wd; }
int height() const { return ht; }
inline void setSize(T w, T h) { wd = w; ht = h; }
inline void setWidth(T w) { wd = w; }
inline void setHeight(T h) { ht = h; }
void setSize(T w, T h) { wd = w; ht = h; }
void setWidth(T w) { wd = w; }
void setHeight(T h) { ht = h; }
inline TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
inline TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht); }
inline TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
inline TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht); }
inline TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
inline TSize<T> operator*(const float v) const { return TSize<T>((T)v*wd, (T)ht*v); }
inline TSize<T>& operator*=(const float v) { wd=(T)v*wd; ht=(T)ht*v; return *this; }
inline TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
inline TSize<T>& operator/=(const float v) { (T)wd/=v; (T)ht/=v; return *this; }
TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht); }
TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht); }
TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
TSize<T> operator*(const float v) const { return TSize<T>((T)v*wd, (T)ht*v); }
TSize<T>& operator*=(const float v) { wd=(T)v*wd; ht=(T)ht*v; return *this; }
TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
TSize<T>& operator/=(const float v) { (T)wd/=v; (T)ht/=v; return *this; }
inline bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
inline bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
inline bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
inline bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
inline TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
inline bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
inline bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
inline TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max(wd,other.wd), std::max(ht,other.ht)); }
inline TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min(wd,other.wd), std::min(ht,other.ht)); }
TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max(wd,other.wd), std::max(ht,other.ht)); }
TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min(wd,other.wd), std::min(ht,other.ht)); }
inline void scale(const TSize<T>& s, ESizeScaleMode mode) {
void scale(const TSize<T>& s, ESizeScaleMode mode) {
if(mode == IGNORE_ASPECT_RATIO || wd == 0 || ht == 0) {
wd = s.wd;
ht = s.ht;
@@ -74,10 +74,10 @@ public:
}
}
}
inline void scale(int w, int h, ESizeScaleMode mode) { scale(TSize<T>(w, h)); }
void scale(int w, int h, ESizeScaleMode mode) { scale(TSize<T>(w, h)); }
inline float ratio() const { return (float)wd/ht; }
inline T area() const { return wd*ht; }
float ratio() const { return (float)wd/ht; }
T area() const { return wd*ht; }
private:
T wd, ht;
@@ -86,13 +86,20 @@ private:
typedef TSize<int> Size;
typedef TSize<float> SizeF;
template <class T>
inline std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
template<class T>
std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
{
out << "Size(" << size.width() << ","
<< size.height() << ")";
out << size.width() << " " << size.height();
return out;
}
template<class T>
std::istream& operator>>(std::istream& in, TSize<T>& size)
{
T w, h;
in >> w >> h;
size.setSize(w, h);
return in;
}
#endif

View File

@@ -24,3 +24,20 @@ AlignmentFlag parseAlignment(std::string aligment)
else
return AlignCenter;
}
AnchorPoint parseAnchorPoint(const std::string& anchorPoint)
{
if(anchorPoint == "left")
return AnchorLeft;
else if(anchorPoint == "right")
return AnchorRight;
else if(anchorPoint == "top")
return AnchorTop;
else if(anchorPoint == "bottom")
return AnchorBottom;
else if(anchorPoint == "horizontalCenter")
return AnchorHorizontalCenter;
else if(anchorPoint == "verticalCenter")
return AnchorVerticalCenter;
return AnchorNone;
}

View File

@@ -5,5 +5,5 @@
#include <string>
AlignmentFlag parseAlignment(std::string aligment);
#endif // TRANSLATOR_H
AnchorPoint parseAnchorPoint(const std::string& anchorPoint);
#endif

View File

@@ -2,8 +2,9 @@
#define TYPES_H
#include <stdint.h>
#include <functional>
// easy types
// easy handwriting types
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
@@ -16,4 +17,7 @@ typedef int32_t int32;
typedef int16_t int16;
typedef int8_t int8;
#endif // TYPES_H
typedef std::function<void()> SimpleCallback;
typedef std::function<bool()> BooleanCallback;
#endif