1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 /*
24  * This code is based on Labyrinth of Time code with assistance of
25  *
26  * Copyright (c) 1993 Terra Nova Development
27  * Copyright (c) 2004 The Wyrmkeep Entertainment Co.
28  *
29  */
30 
31 #include "common/file.h"
32 
33 #include "lab/lab.h"
34 
35 #include "lab/dispman.h"
36 #include "lab/image.h"
37 
38 namespace Lab {
39 
Image(Common::File * s,LabEngine * vm)40 Image::Image(Common::File *s, LabEngine *vm) : _vm(vm) {
41 	_width = s->readUint16LE();
42 	_height = s->readUint16LE();
43 	s->skip(4);
44 
45 	uint32 size = _width * _height;
46 	if (size & 1)
47 		size++;
48 
49 	_imageData = new byte[size];
50 	s->read(_imageData, size);
51 	_autoFree = true;
52 }
53 
~Image()54 Image::~Image() {
55 	if (_autoFree)
56 		delete[] _imageData;
57 }
58 
setData(byte * d,bool autoFree)59 void Image::setData(byte *d, bool autoFree) {
60 	if (_autoFree)
61 		delete[] _imageData;
62 	_imageData = d;
63 	_autoFree = autoFree;
64 }
65 
blitBitmap(uint16 srcX,uint16 srcY,Image * imgDest,uint16 destX,uint16 destY,uint16 width,uint16 height,byte masked)66 void Image::blitBitmap(uint16 srcX, uint16 srcY, Image *imgDest,
67 	uint16 destX, uint16 destY, uint16 width, uint16 height, byte masked) {
68 	int clipWidth = width;
69 	int clipHeight = height;
70 	int destWidth = (imgDest) ? imgDest->_width : _vm->_graphics->_screenWidth;
71 	int destHeight = (imgDest) ? imgDest->_height : _vm->_graphics->_screenHeight;
72 	byte *destBuffer = (imgDest) ? imgDest->_imageData : _vm->_graphics->getCurrentDrawingBuffer();
73 
74 	if (destX + clipWidth > destWidth)
75 		clipWidth = destWidth - destX;
76 
77 	if (destY + clipHeight > destHeight)
78 		clipHeight = destHeight - destY;
79 
80 	if ((clipWidth > 0) && (clipHeight > 0)) {
81 		byte *img = _imageData + srcY * _width + srcX;
82 		byte *dest = destBuffer + destY * destWidth + destX;
83 
84 		if (!masked) {
85 			for (int i = 0; i < clipHeight; i++) {
86 				memcpy(dest, img, clipWidth);
87 				img += _width;
88 				dest += destWidth;
89 			}
90 		} else {
91 			for (int i = 0; i < clipHeight; i++) {
92 				for (int j = 0; j < clipWidth; j++) {
93 					byte c = img[j];
94 
95 					if (c)
96 						dest[j] = c - 1;
97 				}
98 
99 				img += _width;
100 				dest += destWidth;
101 			}
102 		}
103 	}
104 }
105 
drawImage(uint16 x,uint16 y)106 void Image::drawImage(uint16 x, uint16 y) {
107 	blitBitmap(0, 0, nullptr, x, y, _width, _height, false);
108 }
109 
drawMaskImage(uint16 x,uint16 y)110 void Image::drawMaskImage(uint16 x, uint16 y) {
111 	blitBitmap(0, 0, nullptr, x, y, _width, _height, true);
112 }
113 
readScreenImage(uint16 x,uint16 y)114 void Image::readScreenImage(uint16 x, uint16 y) {
115 	int clipWidth = _width;
116 	int clipHeight = _height;
117 
118 	if (x + clipWidth > _vm->_graphics->_screenWidth)
119 		clipWidth = _vm->_graphics->_screenWidth - x;
120 
121 	if (y + clipHeight > _vm->_graphics->_screenHeight)
122 		clipHeight = _vm->_graphics->_screenHeight - y;
123 
124 	if ((clipWidth > 0) && (clipHeight > 0)) {
125 		byte *img = _imageData;
126 		byte *screen = _vm->_graphics->getCurrentDrawingBuffer() + y * _vm->_graphics->_screenWidth + x;
127 
128 		while (clipHeight-- > 0) {
129 			memcpy(img, screen, clipWidth);
130 			img += _width;
131 			screen += _vm->_graphics->_screenWidth;
132 		}
133 	}
134 }
135 
136 } // End of namespace Lab
137