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 /* This code is based on Nine Patch code by Matthew Leverton
23    taken from https://github.com/konforce/Allegro-Nine-Patch
24 
25    Copyright (C) 2011 Matthew Leverton
26 
27    Permission is hereby granted, free of charge, to any person obtaining a copy of
28    this software and associated documentation files (the "Software"), to deal in
29    the Software without restriction, including without limitation the rights to
30    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
31    of the Software, and to permit persons to whom the Software is furnished to do
32    so, subject to the following conditions:
33 
34    The above copyright notice and this permission notice shall be included in all
35    copies or substantial portions of the Software.
36 
37    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43    SOFTWARE.
44  */
45 
46 #ifndef GRAPHICS_NINE_PATCH_H
47 #define GRAPHICS_NINE_PATCH_H
48 
49 #include "common/array.h"
50 #include "common/rect.h"
51 #include "common/hashmap.h"
52 
53 namespace Graphics {
54 
55 struct TransparentSurface;
56 struct Surface;
57 
58 struct NinePatchMark {
59 	int offset;
60 	int length;
61 	int dest_offset;
62 	int dest_length;
63 	float ratio;
64 };
65 
66 class NinePatchSide {
67 public:
68 	Common::Array<NinePatchMark *> _m;
69 	int _fix;
70 
NinePatchSide()71 	NinePatchSide() : _fix(0) {}
72 	~NinePatchSide();
73 
74 	bool init(Graphics::TransparentSurface *bmp, bool vertical);
75 	void calcOffsets(int len);
76 };
77 
78 class NinePatchBitmap {
79 	Graphics::TransparentSurface *_bmp;
80 	NinePatchSide _h, _v;
81 	Common::Rect _padding;
82 	bool _destroy_bmp;
83 	int _width, _height;
84 	int _cached_dw, _cached_dh;
85 	Common::HashMap<uint32, int> _cached_colors;
86 
87 public:
88 	NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bitmap);
89 	~NinePatchBitmap();
90 
91 	void blit(Graphics::Surface &target, int dx, int dy, int dw, int dh, byte *palette = NULL, byte numColors = 0);
92 	void blitClip(Graphics::Surface &target, Common::Rect clip, int dx, int dy, int dw, int dh);
93 
getWidth()94 	int getWidth() { return _width; }
getHeight()95 	int getHeight() { return _height; }
getMinWidth()96 	int getMinWidth() { return _h._fix; }
getMinHeight()97 	int getMinHeight() { return _v._fix; }
getSource()98 	Graphics::TransparentSurface *getSource() { return _bmp; }
getPadding()99 	Common::Rect &getPadding() { return _padding; }
100 
101 private:
102 
103 	void drawRegions(Graphics::Surface &target, int dx, int dy, int dw, int dh);
104 
105 	// Assumes color is in the palette
106 	byte getColorIndex(uint32 target, byte *palette);
107 	uint32 grayscale(uint32 color);
108 	uint32 grayscale(byte r, byte g, byte b);
109 	byte closestGrayscale(uint32 color, byte* palette, byte paletteLength);
110 };
111 
112 } // end of namespace Graphics
113 
114 #endif // GRAPHICS_NINE_PATCH_H
115