Project

General

Profile

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

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

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 org.distorted.library.message.EffectMessage;
28

    
29
import java.io.BufferedOutputStream;
30
import java.io.FileOutputStream;
31
import java.io.IOException;
32
import java.nio.ByteBuffer;
33
import java.nio.ByteOrder;
34

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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
  public EffectQueueOther(DistortedObject obj)
44
    {
45
    super(obj,NUM_UNIFORMS, INDEX );
46

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
// runs on the graphics thread
67

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

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

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

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

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

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

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

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

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

    
151
      return addBase(eln);
152
      }
153

    
154
    return -1;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

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

    
171
      buf.position((         i)*width*4);
172
      buf.put(tmp2);
173
      buf.position((height-1-i)*width*4);
174
      buf.put(tmp1);
175
      }
176
    }
177
  }
(16-16/18)