Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueOther.java @ 9351ad55

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
import android.graphics.Bitmap;
25
import android.opengl.GLES20;
26

    
27
import java.io.BufferedOutputStream;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.nio.ByteBuffer;
31
import java.nio.ByteOrder;
32

    
33
class EffectQueueOther extends EffectQueue
34
  {
35
  private static final int NUM_UNIFORMS = 4;
36
  private static final int INDEX = EffectTypes.OTHER.ordinal();
37
  private String[] mFilename;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
  public EffectQueueOther(DistortedObject obj)
42
    {
43
    super(obj,NUM_UNIFORMS, INDEX );
44

    
45
    if( mMax[INDEX]>0 )
46
      {
47
      mFilename= new String[mMax[INDEX]];
48
      }
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  protected void moveEffect(int index)
54
    {
55
    mFilename[index] = mFilename[index+1];
56

    
57
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
58
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
59
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
60
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
// runs on the graphics thread
65

    
66
  synchronized void send()
67
    {
68
    for(int i=0; i<mNumEffects; i++)
69
      {
70
      if (mType[i] == EffectNames.SAVE_PNG.ordinal() )
71
        {
72
        int left  = (int)mUniforms[NUM_UNIFORMS*i  ];
73
        int top   = (int)mUniforms[NUM_UNIFORMS*i+1];
74
        int width = (int)mUniforms[NUM_UNIFORMS*i+2];
75
        int height= (int)mUniforms[NUM_UNIFORMS*i+3];
76

    
77
        ByteBuffer buf = ByteBuffer.allocateDirect( width*height*4 );
78
        buf.order(ByteOrder.LITTLE_ENDIAN);
79
        GLES20.glReadPixels( left, top, width, height , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
80

    
81
        flipUpsideDown(buf,width,height); // GL uses a coordinate system from mathematics; i.e.
82
                                          // (0,0) is in the lower-left corner. 2D stuff has
83
                                          // the origin on the upper-left corner; we have to flip
84
                                          // out bitmap upside down!
85
        buf.rewind();
86
        BufferedOutputStream bos = null;
87

    
88
        try
89
          {
90
          bos = new BufferedOutputStream(new FileOutputStream(mFilename[i]));
91
          Bitmap bmp = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);
92
          bmp.copyPixelsFromBuffer(buf);
93
          bmp.compress(Bitmap.CompressFormat.PNG, 90, bos);
94
          bmp.recycle();
95

    
96
          for(int j=0; j<mNumListeners; j++)
97
            EffectMessageSender.newMessage( mListeners.elementAt(j),
98
                                            EffectMessage.EFFECT_FINISHED,
99
                                           (mID[i]<<EffectTypes.LENGTH)+EffectTypes.OTHER.type,
100
                                            mType[i],
101
                                            mBitmapID,
102
                                            null);
103
          }
104
        catch(Exception e)
105
          {
106
          for(int j=0; j<mNumListeners; j++)
107
            EffectMessageSender.newMessage( mListeners.elementAt(j),
108
                                            EffectMessage.EFFECT_FAILED,
109
                                           (mID[i]<<EffectTypes.LENGTH)+EffectTypes.OTHER.type,
110
                                            mType[i],
111
                                            mBitmapID,
112
                                            e.getMessage());
113
          }
114
        finally
115
          {
116
          if (bos != null)
117
            {
118
            try {bos.close();}
119
            catch(IOException io ) {}
120
            }
121
          }
122

    
123
        remove(i);
124
        i--;
125
        continue;
126
        }
127
      else if (mType[i] == EffectNames.SAVE_MP4.ordinal() )
128
        {
129
        // TODO: Implement SAVE_MP4 HERE
130
        }
131
      }
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  synchronized long add(EffectNames eln, String filename, int left, int top, int width, int height)
137
    {
138
    if( mMax[INDEX]>mNumEffects )
139
      {
140
      mFilename[mNumEffects] = filename;
141
      mInterI[mNumEffects] = null;
142
      mInterP[mNumEffects] = null;
143

    
144
      mUniforms[NUM_UNIFORMS*mNumEffects  ] =  left;
145
      mUniforms[NUM_UNIFORMS*mNumEffects+1] =   top;
146
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = width;
147
      mUniforms[NUM_UNIFORMS*mNumEffects+3] =height;
148

    
149
      return addBase(eln);
150
      }
151

    
152
    return -1;
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  private void flipUpsideDown(ByteBuffer buf, int width, int height)
158
    {
159
    byte[] tmp1 = new byte[width*4];
160
    byte[] tmp2 = new byte[width*4];
161

    
162
    for(int i=0; i<height/2; i++)
163
      {
164
      buf.position((         i)*width*4);
165
      buf.get(tmp1);
166
      buf.position((height-1-i)*width*4);
167
      buf.get(tmp2);
168

    
169
      buf.position((         i)*width*4);
170
      buf.put(tmp2);
171
      buf.position((height-1-i)*width*4);
172
      buf.put(tmp1);
173
      }
174
    }
175
  }
(15-15/30)