/* 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/scnfile.cpp,v 1.4 2006/09/07 10:39:55 dahms Exp $
 */

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

#include "tinsel/features.h"
#include "tinsel/tinsel.h"
#include "tinsel/scnfile.h"

namespace Tinsel {

SCNFile::SCNFile(const char *filename, uint32 features) {
	if(_file.open(filename) == false)
	{
		warning("failed to open %s", filename);
		return;
	}

	_features = features;
	_background = NULL;

	getSections();
	if(_features & GF_DW1) getBlocks();
	getPalettes();
	getBitmapList();
}

SCNFile::~SCNFile() {
	if(_file.isOpen()) _file.close();

	// TODO: free sections, blocks
}

uint8 SCNFile::read_uint8() {
	uint8 b;

	_file.read(&b, 1);
	return b;
}

uint16 SCNFile::read_uint16() {
	uint8 b2[2];
	uint16 val;

	_file.read(b2, 2);
	val = b2[0] + (b2[1] << 8);
	return val;
}

uint32 SCNFile::read_uint32() {
	uint8 b4[4];
	uint32 val;

	_file.read(b4, 4);
	val = b4[0] + (b4[1] << 8) + (b4[2] << 16) + (b4[3] << 24);
	return val;
}

bool SCNFile::getSections() {
	struct SCNSection s;
	uint32 offset = 0, last;
	uint16 type, magic;

	do {
		_file.seek(offset);

		last = offset;

		type = read_uint16();
		magic = read_uint16();
		offset = read_uint32();

		if(magic != 0x3334)
		{
			error("wrong magic");
			return false;
		}

		s.type = type;
		s.offset = last;
		s.size = (offset != 0) ?
			(offset - last - 8) :
			(_file.size() - last - 8);

		_sections.push_back(s);

		debug("0x%08x: id 0x%04x (next: 0x%08x) %d bytes",
			s.offset, s.type, offset, s.size);
	}
	while(offset > 0);

	return true;
}

bool SCNFile::seekSection(uint16 section_id)
{
	for(Common::Array<SCNSection>::const_iterator s = _sections.begin();
		s != _sections.end();
		s ++) {
		if(s->type == section_id) {
			// seek & skip section header
			_file.seek(s->offset + 8);
			return true;
		}
	}

	return false;
}

uint32 SCNFile::getSectionSize(uint16 section_id)
{
	for(Common::Array<SCNSection>::const_iterator s = _sections.begin();
		s != _sections.end();
		s ++) {
		if(s->type == section_id) {
			return s->size;
		}
	}
	return 0;
}

bool SCNFile::getBlocks() {
	uint32 i, j;

	_blocks.num = getSectionSize(0x0004) / 16;
	if(!_blocks.num) {
		warning("no blocks in file %s", _file.name());
		return false;
	}

	_blocks.blocks = (uint8 **)calloc(_blocks.num, sizeof(uint8 *));

	seekSection(0x0004);

	for(i = 0; i < _blocks.num; i ++) {
		_blocks.blocks[i] = (uint8 *)calloc(16, sizeof(uint8));

		for(j = 0; j < 16; j ++) {
			_blocks.blocks[i][j] = read_uint8();
		}
	}
	return true;
}

bool SCNFile::getPalettes() {
	uint32 num, i, j;
	SCNPalette pal;

	num = getSectionSize(0x0005) / 1024;

	if(!seekSection(0x0005)) return false;

	for(i = 0; i < num; i ++) {
		pal.offset = _file.pos();

		for(j = 0; j < 1024; j += 4) {
			pal.palette[j + 0] = read_uint8();
			pal.palette[j + 1] = read_uint8();
			pal.palette[j + 2] = read_uint8();
			pal.palette[j + 3] = read_uint8();
		}

		_palettes.push_back(pal);

		debug("read palette #%u", i);
	}
	return true;
}

const SCNPalette *SCNFile::getPalette(uint32 offset) {
	for(Common::Array<SCNPalette>::const_iterator p = _palettes.begin();
		p != _palettes.end();
		p ++) {

		if(p->offset == offset) return p;
	}
	return NULL;
}

bool SCNFile::getBitmapList() {
	SCNBitmap b = { 0, NULL };
	const SCNPalette *pal;
	uint16 num, i, j;
	uint16 width, height, bx, by;
	uint16 *blist;
	uint32 unkn, offset, off_pal, off_save;
	uint32 offset_mask = 0x7FFFFF;

	if(_features & GF_DW2) offset_mask = 0xFFFFFF;

	num = getSectionSize(0x0006) / 16;
	if(!seekSection(0x0006)) return false;

	for(i = 0; i < num; i ++) {
		b.offset = _file.pos();

		width = read_uint16() & 0x7FFF;
		height = read_uint16() & 0x7FFF;

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

		unkn = read_uint32();
		offset = read_uint32();
		off_pal = read_uint32() & offset_mask;

		if(off_pal != 0) {
			// DW2: something w/o palette?
			pal = getPalette(off_pal);
			if(!pal) {
				error("could not get palette from address 0x%08x\n", off_pal);
			}
		}
		else pal = NULL;

		// save file position
		off_save = _file.pos();

		// get block list
		_file.seek(offset & offset_mask);

		if(_features & GF_DW1) {
			blist = (uint16 *)calloc(bx * by, sizeof(uint16));
			for(j = 0; j < (bx * by); j ++) {
				blist[j] = read_uint16() & 0x7FFF; // 15 bit ?!
			}

			b.bitmap = new Bitmap(width, height, blist, pal, &_blocks);

			if((width == 320) && (height == 200)) {
				_background = b.bitmap;
			}
		}
		else if(_features & GF_DW2) {
			uint8 *pixels = (uint8 *)calloc(width * height, sizeof(uint8));
			_file.read(pixels, (width * height));

			b.bitmap = new Bitmap(width, height, pixels, pal);

			if((width == 640) && (height >= 432)) {
				debug("(possible) background found");
				if(!_background) _background = b.bitmap;
			}
		}

		// restore file position
		_file.seek(off_save);
	}

	return true;
}

Bitmap *SCNFile::getBackgroundBitmap() {
	return _background;
}

} // namespace Tinsel

