aboutsummaryrefslogtreecommitdiffstats
path: root/lib/psibitmap.cpp
blob: 3dfd125706f4a2e350d6213170e3bb610e089e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <plp_inttypes.h>
#include "psibitmap.h"

void
encodeBitmap(int width, int height, getPixelFunction_t getPixel, bool /*rle*/,
	     bufferStore &out) {
    bufferStore ib;

    ib.addDWord(0x00000028);   // hdrlen
    ib.addDWord(width);        // xPixels
    ib.addDWord(height);       // yPixels
    ib.addDWord(0);            // xTwips (unspecified)
    ib.addDWord(0);            // yTwips (unspecified)
    ib.addDWord(2);            // bitsPerPixel
    ib.addDWord(0);            // unknown1
    ib.addDWord(0);            // unknown2
    ib.addDWord(0);            // RLEflag

    bufferStore rawBuf;
    for (int y = 0; y < height; y++) {
	int ov = 0;
	int shift = 0;
	int bc = 0;

	for (int x = 0; x < width; x++) {
	    int v = getPixel(x, y) / 85;
	    ov |= (v << shift);
	    if (shift == 6) {
		rawBuf.addByte(ov);
		bc++;
		shift = 0;
		ov = 0;
	    } else
		shift += 2;
	}
	if (shift != 0) {
	    rawBuf.addByte(ov);
	    shift = 0;
	    ov = 0;
	    bc++;
	}
	while (bc % 4) {
	    rawBuf.addByte(0);
	    bc++;
	}
    }

#if 1
    ib.addBuff(rawBuf);
#else
    //TODO: RLE encoding

    int rawLen = rawBuf.getLen();
    int eqCount = 1;
    int lastByte = rawBuf.getByte(0);
    bufferStore diBuf;

    for (int i = 1; i <= rawLen; i++) {
	int v;
	if (i < rawLen)
	    v = rawBuf.getByte(i);
	else
	    v = lastByte + 1;
	if (v == lastByte) {
	    if (diBuf.getLen()) {
		ib.addByte(0x100 - diBuf.getLen());
		ib.addBuff(diBuf);
		diBuf.init();
	    }
	    eqCount++;
	    if (eqCount > 0x7f) {
		ib.addByte(0x7f);
		ib.addByte(v);
		eqCount = 1;
	    }
	} else {
	    if (eqCount > 1) {
		ib.addByte(eqCount);
		ib.addByte(lastByte);
		eqCount = 1;
	    } else {
		diBuf.addByte(lastByte);
		if ((diBuf.getLen() > 0x7f) || (i == rawLen)) {
		    ib.addByte(0x100 - diBuf.getLen());
		    ib.addBuff(diBuf);
		    diBuf.init();
		}
	    }
	}
	lastByte = v;
    }
#endif

    out.addDWord(ib.getLen() + 4);
    out.addBuff(ib);
}

#define splitByte(v)                                \
do {                                                \
    int j;                                          \
                                                    \
    if (x < bytesPerLine)                           \
	for (j = 0; j < pixelsPerByte; j++) {       \
	    if (j && ((oidx % xPixels) == 0))       \
		break;                              \
	    else                                    \
              if (oidx >= picsize)                  \
		return false;                       \
	    else {                                  \
		out.addByte((v & mask) * grayVal);  \
                v >>= bitsPerPixel;                 \
		oidx++;                             \
	    }                                       \
	}                                           \
    if (++x >= linelen)                             \
	x = 0;                                      \
} while (0)

bool
decodeBitmap(const unsigned char *p, int &width, int &height, bufferStore &out)
{
    u_int32_t totlen = *((const u_int32_t*)p); p += 4;
    u_int32_t hdrlen = *((const u_int32_t*)p); p += 4;
    u_int32_t datlen = totlen - hdrlen;
    u_int32_t xPixels = *((const u_int32_t*)p); p += 4;
    u_int32_t yPixels = *((const u_int32_t*)p); p += 4;
    u_int32_t xTwips = *((const u_int32_t*)p); p += 4;
    u_int32_t yTwips = *((const u_int32_t*)p); p += 4;
    u_int32_t bitsPerPixel = *((const u_int32_t*)p); p += 4;
    u_int32_t unknown1 = *((const u_int32_t*)p); p += 4;
    u_int32_t unknown2 = *((const u_int32_t*)p); p += 4;
    u_int32_t RLEflag = *((const u_int32_t*)p); p += 4;

    width = xPixels;
    height = yPixels;

    u_int32_t picsize = xPixels * yPixels;
    u_int32_t linelen;
    int pixelsPerByte = (8 / bitsPerPixel);
    int nColors = 1 << bitsPerPixel;
    int grayVal = 255 / (nColors - 1);
    int bytesPerLine = (xPixels + pixelsPerByte - 1) / pixelsPerByte;
    int mask = (bitsPerPixel << 1) - 1;

    int oidx = 0;
    int x = 0;
    int y = 0;
    int offset = 0;

    if (RLEflag) {
	int i = 0;
	while (offset < datlen) {
	    unsigned char b = *(p + offset);
	    if (b >= 0x80) {
		offset += 0x100 - b + 1;
		i += 0x100 - b;
	    } else {
		offset += 2;
		i += b + 1;
	    }
	}
	linelen = i / yPixels;
	offset = 0;
	while (offset < datlen) {
	    unsigned char b = *(p + offset++);
	    if (b >= 0x80) {
		for (i = 0; i < 0x100 - b; i++, offset++) {
		    if (offset >= datlen)
			return false; // data corrupted
		    unsigned char b2 = *(p + offset);
		    splitByte(b2);
		}
	    } else {
		if (offset >= datlen)
		    return false;
		else {
		    unsigned char b2 = *(p + offset);
		    unsigned char bs = b2;
		    for (i = 0; i <= b; i++) {
			splitByte(b2);
			b2 = bs;
		    }
		}
		offset++;
	    }
	}
    } else {
	linelen = datlen / yPixels;
	while (offset < datlen) {
	    unsigned char b = *(p + offset++);
	    splitByte(b);
	}
    }
    return true;
}

/*
 * Local variables:
 * c-basic-offset: 4
 * End:
 */