1 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Distorted. //
|
5 |
|
|
// //
|
6 |
|
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
|
|
// it under the terms of the GNU General Public License as published by //
|
8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
9 |
|
|
// (at your option) any later version. //
|
10 |
|
|
// //
|
11 |
|
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14 |
|
|
// GNU General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// You should have received a copy of the GNU General Public License //
|
17 |
|
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
6a06a912
|
Leszek Koltunski
|
package org.distorted.library;
|
21 |
|
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
|
24 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25 |
|
|
/**
|
26 |
|
|
* Wrapper around the standard Android Bitmap class to which one can apply graphical effects.
|
27 |
|
|
* <p>
|
28 |
|
|
* General idea is as follows:
|
29 |
|
|
* <ul>
|
30 |
|
|
* <li> Create an instance of DistortedBitmap
|
31 |
|
|
* <li> Paint something onto the Bitmap that's backing it up
|
32 |
|
|
* <li> Apply some effects
|
33 |
|
|
* <li> Draw it!
|
34 |
|
|
* </ul>
|
35 |
|
|
* <p>
|
36 |
|
|
* The effects we can apply fall into three general categories:
|
37 |
|
|
* <ul>
|
38 |
|
|
* <li> Matrix Effects, i.e. ones that change the Bitmap's ModelView Matrix (moves, scales, rotations)
|
39 |
|
|
* <li> Vertex Effects, i.e. effects that are implemented in the Vertex Shader. Those typically change
|
40 |
|
|
* the shape of (some sub-Region of) the Bitmap in some way (deforms, distortions, sinks)
|
41 |
|
|
* <li> Fragment Effects, i.e. effects that change (some of) the pixels of the Bitmap (transparency, macroblock)
|
42 |
|
|
* </ul>
|
43 |
|
|
* <p>
|
44 |
|
|
*
|
45 |
|
|
*/
|
46 |
|
|
public class DistortedBitmap extends DistortedObject
|
47 |
|
|
{
|
48 |
|
|
|
49 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50 |
|
|
// PUBLIC API
|
51 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
/**
|
53 |
|
|
* Default constructor: creates a DistortedBitmap (width,height) pixels in size, with the distortion
|
54 |
ffbf279e
|
Leszek Koltunski
|
* grid of 'cols' and does not fill it up with any Bitmap data just yet.
|
55 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
56 |
|
|
* Distortion grid is a grid of rectangles the Bitmap is split to. The vertices of this grid are then
|
57 |
|
|
* moved around by the Vertex Shader to create various Vertex Effects.
|
58 |
|
|
* <p>
|
59 |
|
|
* Size parameter describes the horizontal size, i.e. the number of rectangles the top (or bottom) edge
|
60 |
|
|
* is split to. So when size=1, the whole Bitmap is just one giant rectangle. When size=10, the Bitmap
|
61 |
|
|
* is split into a grid of 10x10 rectangles.
|
62 |
|
|
* <p>
|
63 |
|
|
* The higher the size, the better Vertex Effects look; on the other hand too high size will slow things
|
64 |
|
|
* down.
|
65 |
|
|
*
|
66 |
|
|
* @param width width of the DistortedBitmap, in pixels.
|
67 |
|
|
* @param height height of the DistortedBitmap, in pixels.
|
68 |
ffbf279e
|
Leszek Koltunski
|
* @param cols Number of columns in the distortion grid. 2<=size<256.
|
69 |
|
|
* Number of rows gets calculated with 'rows = cols*height/width'.
|
70 |
6a06a912
|
Leszek Koltunski
|
*/
|
71 |
ffbf279e
|
Leszek Koltunski
|
public DistortedBitmap(int width, int height, int cols)
|
72 |
6a06a912
|
Leszek Koltunski
|
{
|
73 |
adb2661c
|
Leszek Koltunski
|
int xsize = cols;
|
74 |
|
|
int ysize = cols*height/width;
|
75 |
|
|
|
76 |
79f172ab
|
Leszek Koltunski
|
if( xsize<1 ) xsize= 1;
|
77 |
6a06a912
|
Leszek Koltunski
|
if( xsize>256 ) xsize=256;
|
78 |
79f172ab
|
Leszek Koltunski
|
if( ysize<1 ) ysize= 1;
|
79 |
6a06a912
|
Leszek Koltunski
|
if( ysize>256 ) ysize=256;
|
80 |
|
|
|
81 |
|
|
mSizeX= width;
|
82 |
|
|
mSizeY= height;
|
83 |
ffbf279e
|
Leszek Koltunski
|
mSizeZ= 1;
|
84 |
0729bc41
|
Leszek Koltunski
|
mGrid = DistortedGridFactory.getGrid(xsize,ysize);
|
85 |
ffbf279e
|
Leszek Koltunski
|
initializeData();
|
86 |
6a06a912
|
Leszek Koltunski
|
}
|
87 |
|
|
|
88 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89 |
|
|
/**
|
90 |
|
|
* Creates a DistortedBitmap and immediately fills it up with Bitmap data.
|
91 |
|
|
* The dimensions of the created DistortedBitmap object are the same like that of the passed Bitmap.
|
92 |
|
|
*
|
93 |
|
|
* @param bmp The android.graphics.Bitmap object to apply effects to and display.
|
94 |
da7ce0d8
|
LeszekKoltunski
|
* @param gridSize Horizontal size of the distortion grid. 1<=size<256.
|
95 |
6a06a912
|
Leszek Koltunski
|
*/
|
96 |
|
|
public DistortedBitmap(Bitmap bmp, int gridSize)
|
97 |
|
|
{
|
98 |
|
|
this(bmp.getWidth(), bmp.getHeight(), gridSize);
|
99 |
|
|
setBitmap(bmp);
|
100 |
|
|
}
|
101 |
9361b337
|
Leszek Koltunski
|
|
102 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103 |
|
|
/**
|
104 |
8298e6af
|
Leszek Koltunski
|
* Copy constructor.
|
105 |
|
|
*
|
106 |
|
|
* @param db Object to copy
|
107 |
350cc2f5
|
Leszek Koltunski
|
* @param flags see {@see DistortedObject#DistortedObject(DistortedObject,int)}
|
108 |
6a06a912
|
Leszek Koltunski
|
*/
|
109 |
|
|
public DistortedBitmap(DistortedBitmap db, int flags)
|
110 |
|
|
{
|
111 |
9361b337
|
Leszek Koltunski
|
super(db,flags);
|
112 |
6a06a912
|
Leszek Koltunski
|
}
|
113 |
|
|
|
114 |
ada90d33
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115 |
|
|
|
116 |
|
|
protected DistortedObject deepCopy(int flags)
|
117 |
|
|
{
|
118 |
|
|
return new DistortedBitmap(this,flags);
|
119 |
|
|
}
|
120 |
|
|
|
121 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122 |
|
|
}
|
123 |
|
|
|