Project

General

Profile

Download (5.15 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / EffectQueueOther.java @ c6e1c219

1
package org.distorted.library;
2

    
3
///////////////////////////////////////////////////////////////////////////////////////////////////
4

    
5
import android.graphics.Bitmap;
6
import android.opengl.GLES20;
7

    
8
import java.io.BufferedOutputStream;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.nio.ByteBuffer;
12
import java.nio.ByteOrder;
13

    
14
class EffectQueueOther extends EffectQueue
15
  {
16
  private static final int NUM_UNIFORMS = 4;
17
  private static final int INDEX = EffectTypes.OTHER.ordinal();
18
  private String[] mFilename;
19

    
20
///////////////////////////////////////////////////////////////////////////////////////////////////
21

    
22
  public EffectQueueOther(DistortedObject obj)
23
    {
24
    super(obj,NUM_UNIFORMS, INDEX );
25

    
26
    if( mMax[INDEX]>0 )
27
      {
28
      mFilename= new String[mMax[INDEX]];
29
      }
30
    }
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
  protected void moveEffect(int index)
35
    {
36
    mFilename[index] = mFilename[index+1];
37

    
38
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
39
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
40
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
41
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
42
    }
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
// runs on the graphics thread
46

    
47
  synchronized void send()
48
    {
49
    for(int i=0; i<mNumEffects; i++)
50
      {
51
      if (mType[i] == EffectNames.SAVE_PNG.ordinal() )
52
        {
53
        int left  = (int)mUniforms[NUM_UNIFORMS*i  ];
54
        int top   = (int)mUniforms[NUM_UNIFORMS*i+1];
55
        int width = (int)mUniforms[NUM_UNIFORMS*i+2];
56
        int height= (int)mUniforms[NUM_UNIFORMS*i+3];
57

    
58
        ByteBuffer buf = ByteBuffer.allocateDirect( width*height*4 );
59
        buf.order(ByteOrder.LITTLE_ENDIAN);
60
        GLES20.glReadPixels( left, top, width, height , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
61

    
62
        flipUpsideDown(buf,width,height); // GL uses a coordinate system from mathematics; i.e.
63
                                          // (0,0) is in the lower-left corner. 2D stuff has
64
                                          // the origin on the upper-left corner; we have to flip
65
                                          // out bitmap upside down!
66
        buf.rewind();
67
        BufferedOutputStream bos = null;
68

    
69
        try
70
          {
71
          bos = new BufferedOutputStream(new FileOutputStream(mFilename[i]));
72
          Bitmap bmp = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);
73
          bmp.copyPixelsFromBuffer(buf);
74
          bmp.compress(Bitmap.CompressFormat.PNG, 90, bos);
75
          bmp.recycle();
76

    
77
          for(int j=0; j<mNumListeners; j++)
78
            EffectMessageSender.newMessage( mListeners.elementAt(j),
79
                                            EffectMessage.EFFECT_FINISHED,
80
                                           (mID[i]<<EffectTypes.LENGTH)+EffectTypes.OTHER.type,
81
                                            mType[i],
82
                                            mBitmapID,
83
                                            null);
84
          }
85
        catch(Exception e)
86
          {
87
          for(int j=0; j<mNumListeners; j++)
88
            EffectMessageSender.newMessage( mListeners.elementAt(j),
89
                                            EffectMessage.EFFECT_FAILED,
90
                                           (mID[i]<<EffectTypes.LENGTH)+EffectTypes.OTHER.type,
91
                                            mType[i],
92
                                            mBitmapID,
93
                                            e.getMessage());
94
          }
95
        finally
96
          {
97
          if (bos != null)
98
            {
99
            try {bos.close();}
100
            catch(IOException io ) {}
101
            }
102
          }
103

    
104
        remove(i);
105
        i--;
106
        continue;
107
        }
108
      else if (mType[i] == EffectNames.SAVE_MP4.ordinal() )
109
        {
110
        // TODO: Implement SAVE_MP4 HERE
111
        }
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  synchronized long add(EffectNames eln, String filename, int left, int top, int width, int height)
118
    {
119
    if( mMax[INDEX]>mNumEffects )
120
      {
121
      mFilename[mNumEffects] = filename;
122
      mInterI[mNumEffects] = null;
123
      mInterP[mNumEffects] = null;
124

    
125
      mUniforms[NUM_UNIFORMS*mNumEffects  ] =  left;
126
      mUniforms[NUM_UNIFORMS*mNumEffects+1] =   top;
127
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = width;
128
      mUniforms[NUM_UNIFORMS*mNumEffects+3] =height;
129

    
130
      return addBase(eln);
131
      }
132

    
133
    return -1;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private void flipUpsideDown(ByteBuffer buf, int width, int height)
139
    {
140
    byte[] tmp1 = new byte[width*4];
141
    byte[] tmp2 = new byte[width*4];
142

    
143
    for(int i=0; i<height/2; i++)
144
      {
145
      buf.position((         i)*width*4);
146
      buf.get(tmp1);
147
      buf.position((height-1-i)*width*4);
148
      buf.get(tmp2);
149

    
150
      buf.position((         i)*width*4);
151
      buf.put(tmp2);
152
      buf.position((height-1-i)*width*4);
153
      buf.put(tmp1);
154
      }
155
    }
156
  }
(15-15/30)