1
|
package org.distorted.library;
|
2
|
|
3
|
import android.graphics.Bitmap;
|
4
|
|
5
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
6
|
/**
|
7
|
* Wrapper around the standard Android Bitmap class to which one can apply graphical effects.
|
8
|
* <p>
|
9
|
* General idea is as follows:
|
10
|
* <ul>
|
11
|
* <li> Create an instance of DistortedBitmap
|
12
|
* <li> Paint something onto the Bitmap that's backing it up
|
13
|
* <li> Apply some effects
|
14
|
* <li> Draw it!
|
15
|
* </ul>
|
16
|
* <p>
|
17
|
* The effects we can apply fall into three general categories:
|
18
|
* <ul>
|
19
|
* <li> Matrix Effects, i.e. ones that change the Bitmap's ModelView Matrix (moves, scales, rotations)
|
20
|
* <li> Vertex Effects, i.e. effects that are implemented in the Vertex Shader. Those typically change
|
21
|
* the shape of (some sub-Region of) the Bitmap in some way (deforms, distortions, sinks)
|
22
|
* <li> Fragment Effects, i.e. effects that change (some of) the pixels of the Bitmap (transparency, macroblock)
|
23
|
* </ul>
|
24
|
* <p>
|
25
|
*
|
26
|
*/
|
27
|
public class DistortedBitmap extends DistortedObject
|
28
|
{
|
29
|
|
30
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31
|
// PUBLIC API
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33
|
/**
|
34
|
* Default constructor: creates a DistortedBitmap (width,height) pixels in size, with the distortion
|
35
|
* grid of size 'size' and does not fill it up with any Bitmap data just yet.
|
36
|
* <p>
|
37
|
* Distortion grid is a grid of rectangles the Bitmap is split to. The vertices of this grid are then
|
38
|
* moved around by the Vertex Shader to create various Vertex Effects.
|
39
|
* <p>
|
40
|
* Size parameter describes the horizontal size, i.e. the number of rectangles the top (or bottom) edge
|
41
|
* is split to. So when size=1, the whole Bitmap is just one giant rectangle. When size=10, the Bitmap
|
42
|
* is split into a grid of 10x10 rectangles.
|
43
|
* <p>
|
44
|
* The higher the size, the better Vertex Effects look; on the other hand too high size will slow things
|
45
|
* down.
|
46
|
*
|
47
|
* @param width width of the DistortedBitmap, in pixels.
|
48
|
* @param height height of the DistortedBitmap, in pixels.
|
49
|
* @param gridSize Horizontal size of the distortion grid. 2<=size<256.
|
50
|
*/
|
51
|
public DistortedBitmap(int width, int height, int gridSize)
|
52
|
{
|
53
|
int xsize = gridSize;
|
54
|
int ysize = xsize*height/width;
|
55
|
|
56
|
if( xsize<2 ) xsize= 2;
|
57
|
if( xsize>256 ) xsize=256;
|
58
|
if( ysize<2 ) ysize= 2;
|
59
|
if( ysize>256 ) ysize=256;
|
60
|
|
61
|
mSizeX= width;
|
62
|
mSizeY= height;
|
63
|
mSizeZ= 1;
|
64
|
mGrid = new GridBitmap(xsize,ysize);
|
65
|
initializeData(gridSize);
|
66
|
}
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
/**
|
70
|
* Creates a DistortedBitmap and immediately fills it up with Bitmap data.
|
71
|
* The dimensions of the created DistortedBitmap object are the same like that of the passed Bitmap.
|
72
|
*
|
73
|
* @param bmp The android.graphics.Bitmap object to apply effects to and display.
|
74
|
* @param gridSize Horizontal size of the distortion grid. 1<=size<256.
|
75
|
*/
|
76
|
public DistortedBitmap(Bitmap bmp, int gridSize)
|
77
|
{
|
78
|
this(bmp.getWidth(), bmp.getHeight(), gridSize);
|
79
|
setBitmap(bmp);
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
/**
|
84
|
* {@see DistortedObject#DistortedObject(DistortedObject,flags)}
|
85
|
*/
|
86
|
public DistortedBitmap(DistortedBitmap db, int flags)
|
87
|
{
|
88
|
super(db,flags);
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
protected DistortedObject deepCopy(int flags)
|
94
|
{
|
95
|
return new DistortedBitmap(this,flags);
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
}
|
100
|
|