/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002-2005 The ScummVM project
 *
 * 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.
 *
 * $Header: /home9/user/studenten/dahms/c-source/cvs_root/tinsel/bitmap.cpp,v 1.2 2005/05/03 09:21:22 dahms Exp $
 */

#include <assert.h>

#include "common/stdafx.h"
#include "common/util.h"
#include "common/system.h"

#include "tinsel/bitmap.h"

namespace Tinsel {

Bitmap::Bitmap(uint16 width, uint16 height, uint16 *blocklist,
	const SCNPalette *pal, SCNPixelBlocks *blocks) {

	uint16 bx, xo, yo, xi, yi, b;
	uint32 bi;

	_width = width;
	_height = height;
	_palette = pal;

	assert(blocks);
	assert(blocks->blocks);
	assert(blocklist);

	debug("trying to build %ux%u bitmap", width, height);

	_bitmap = (uint8 *)calloc(width * height, sizeof(uint8));

	bx = width / 4;
	if(width % 4) bx ++;

	for(yo = 0; yo < height; yo += 4) {
		for(xo = 0; xo < width; xo += 4) {
			for(yi = 0; yi < 4; yi ++) {
				if((yo + yi) >= height) break;

				for(xi = 0; xi < 4; xi ++) {
					if((xo + xi) >= width) break;

					bi = ((yo / 4) * bx + (xo / 4));
					b = blocklist[bi];

					assert(b < blocks->num);

					_bitmap[(yo + yi) * width + (xo + xi)] =
						blocks->blocks[b][yi * 4 + xi];
				} // xi
			} // yi
		} // xo
	} // yo
}

Bitmap::Bitmap(uint16 width, uint16 height, uint8 *pixels,
	const SCNPalette *pal) {

	_width = width;
	_height = height;
	_palette = pal;

	assert(pixels);

	_bitmap = (uint8 *)calloc(width * height, sizeof(uint8));

	memcpy(_bitmap, pixels, (width * height));
}

Bitmap::~Bitmap() {

	free(_bitmap);
}

} // namespace Tinsel
