Project

General

Profile

« Previous | Next » 

Revision cdd6333d

Added by Leszek Koltunski almost 8 years ago

Move the 'Save' effect to the Application and remove the 'OTHER' effect queue

View differences:

src/main/java/org/distorted/library/Distorted.java
448 448
    return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal());
449 449
    }
450 450

  
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452
/**
453
 * Returns the maximum number of Other effects.
454
 *
455
 * @return The maximum number of Other effects
456
 */
457
  public static int getMaxOther()
458
    {
459
    return EffectQueue.getMax(EffectTypes.OTHER.ordinal());
460
    }
461

  
462 451
///////////////////////////////////////////////////////////////////////////////////////////////////
463 452
/**
464 453
 * Sets the maximum number of Matrix effects that can be applied to a single DistortedObject at one time.
......
513 502
    return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max);
514 503
    }
515 504

  
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518
 * Sets the maximum number of Other effects that can be applied to a single DistortedObject at one time.
519
 * This can fail if the value of 'max' is outside permitted range.
520
 *
521
 * @param max new maximum number of simultaneous Other Effects. Has to be a non-negative number not greater
522
 *            than Byte.MAX_VALUE
523
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
524
 */
525
  public static boolean setMaxOther(int max)
526
    {
527
    return EffectQueue.setMax(EffectTypes.OTHER.ordinal(),max);
528
    }
529

  
530 505
///////////////////////////////////////////////////////////////////////////////////////////////////
531 506
//end of file  
532 507
}
src/main/java/org/distorted/library/DistortedObject.java
49 49
    protected EffectQueueMatrix    mM;
50 50
    protected EffectQueueFragment  mF;
51 51
    protected EffectQueueVertex    mV;
52
    protected EffectQueueOther mO;
53 52

  
54 53
    protected boolean matrixCloned, vertexCloned, fragmentCloned;
55 54
 
......
119 118
        mF = new EffectQueueFragment(d);
120 119
        fragmentCloned = false;   
121 120
        }
122

  
123
      mO= new EffectQueueOther(d); // Other effects are never cloned.
124 121
      }
125 122
    
126 123
///////////////////////////////////////////////////////////////////////////////////////////////////
......
165 162
      mF.send();
166 163
       
167 164
      mGrid.draw();
168

  
169
      mO.send();
170 165
      }
171 166

  
172 167
///////////////////////////////////////////////////////////////////////////////////////////////////
......
188 183
      if( vertexCloned  ==false) mV.abortAll();
189 184
      if( fragmentCloned==false) mF.abortAll();
190 185

  
191
      mO.abortAll();
192

  
193 186
      mBmp          = null;
194 187
      mGrid         = null;
195 188
      mM            = null;
196 189
      mV            = null;
197 190
      mF            = null;
198
      mO            = null;
199 191
      mTextureDataH = null;
200 192
      }
201 193
 
......
325 317
     mV.addListener(el);
326 318
     mF.addListener(el);
327 319
     mM.addListener(el);
328
     mO.addListener(el);
329 320
     }
330 321

  
331 322
///////////////////////////////////////////////////////////////////////////////////////////////////
......
339 330
     mV.removeListener(el);
340 331
     mF.removeListener(el);
341 332
     mM.removeListener(el);
342
     mO.removeListener(el);
343 333
     }
344 334
   
345 335
///////////////////////////////////////////////////////////////////////////////////////////////////
......
393 383
 */
394 384
    public int abortAllEffects()
395 385
      {
396
      return mM.abortAll() + mV.abortAll() + mF.abortAll() + mO.abortAll();
386
      return mM.abortAll() + mV.abortAll() + mF.abortAll();
397 387
      }
398 388

  
399 389
///////////////////////////////////////////////////////////////////////////////////////////////////
......
410 400
        case MATRIX  : return mM.abortAll();
411 401
        case VERTEX  : return mV.abortAll();
412 402
        case FRAGMENT: return mF.abortAll();
413
        case OTHER   : return mO.abortAll();
414 403
        default      : return 0;
415 404
        }
416 405
      }
......
429 418
      if( type==EffectTypes.MATRIX.type   ) return mM.removeByID(id>>EffectTypes.LENGTH);
430 419
      if( type==EffectTypes.VERTEX.type   ) return mV.removeByID(id>>EffectTypes.LENGTH);
431 420
      if( type==EffectTypes.FRAGMENT.type ) return mF.removeByID(id>>EffectTypes.LENGTH);
432
      if( type==EffectTypes.OTHER.type    ) return mO.removeByID(id>>EffectTypes.LENGTH);
433 421

  
434 422
      return 0;
435 423
      }
......
448 436
        case MATRIX  : return mM.removeByType(name);
449 437
        case VERTEX  : return mV.removeByType(name);
450 438
        case FRAGMENT: return mF.removeByType(name);
451
        case OTHER   : return mO.removeByType(name);
452 439
        default      : return 0;
453 440
        }
454 441
      }
......
468 455
      if( type==EffectTypes.MATRIX.type   )  return mM.printByID(id>>EffectTypes.LENGTH);
469 456
      if( type==EffectTypes.VERTEX.type   )  return mV.printByID(id>>EffectTypes.LENGTH);
470 457
      if( type==EffectTypes.FRAGMENT.type )  return mF.printByID(id>>EffectTypes.LENGTH);
471
      if( type==EffectTypes.OTHER.type    )  return mO.printByID(id>>EffectTypes.LENGTH);
472 458

  
473 459
      return false;
474 460
      }
......
2552 2538
///////////////////////////////////////////////////////////////////////////////////////////////////
2553 2539
// WAVE
2554 2540

  
2555
///////////////////////////////////////////////////////////////////////////////////////////////////   
2556
///////////////////////////////////////////////////////////////////////////////////////////////////
2557
// Other-based effects
2558
///////////////////////////////////////////////////////////////////////////////////////////////////
2559
// SAVE_PNG
2560
/**
2561
 * Save the current state of the Object that's backing up our DistortedObject to a PNG file.
2562
 *
2563
 * @param filename Full path to the file.
2564
 * @return         ID of the effect added, or -1 if we failed to add one.
2565
 */
2566
 public long savePNG(String filename, int left, int top, int width, int height)
2567
   {
2568
   return mO.add(EffectNames.SAVE_PNG, filename, left, top, width, height);
2569
   }
2570 2541
}
src/main/java/org/distorted/library/EffectNames.java
53 53
  CONTRAST         ( EffectTypes.FRAGMENT,   new float[] {1.0f}           ),
54 54
  SMOOTH_CONTRAST  ( EffectTypes.FRAGMENT,   new float[] {1.0f}           ),
55 55
  HUE              ( EffectTypes.FRAGMENT,   new float[] {0.0f}           ),
56
  SMOOTH_HUE       ( EffectTypes.FRAGMENT,   new float[] {0.0f}           ),
56
  SMOOTH_HUE       ( EffectTypes.FRAGMENT,   new float[] {0.0f}           );
57 57
  // add new Fragment effects here...
58 58

  
59
  SAVE_PNG         ( EffectTypes.OTHER   ,   null                         ),      // OTHER Effects don't have Unities.
60
  SAVE_MP4         ( EffectTypes.OTHER   ,   null                         );      //
61
  // add new Other effects here...
62

  
63 59
///////////////////////////////////////////////////////////////////////////////////////////////////
64 60
  
65 61
  private static final int MAXDIM = 4;  // maximum supported dimension of an effect  
......
110 106
    {
111 107
    if( ordinal<DISTORT.ordinal()     ) return EffectTypes.MATRIX;
112 108
    if( ordinal<MACROBLOCK.ordinal()  ) return EffectTypes.VERTEX;
113
    if( ordinal<SAVE_PNG.ordinal()    ) return EffectTypes.FRAGMENT;
114 109

  
115
    return EffectTypes.OTHER;
110
    return EffectTypes.FRAGMENT;
116 111
    }
117 112

  
118 113
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectQueueOther.java
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
  }
src/main/java/org/distorted/library/EffectTypes.java
39 39
  /**
40 40
   * Effects executed in the Fragment shader: changes of color, hue, transparency levels, etc.
41 41
   */
42
  FRAGMENT ( 0x4 ),
43
  /**
44
   * Effects that did not belong to anywhere else - currently only saving the contents of underlying
45
   * Surface to a PNG or a MP4 file.
46
   */
47
  OTHER    ( 0x8 );
42
  FRAGMENT ( 0x4 );
48 43

  
49 44
///////////////////////////////////////////////////////////////////////////////////////////////////
50 45

  
......
69 64

  
70 65
    maxtable[1] = 5;  // Max 5 VERTEX Effects
71 66
    maxtable[2] = 5;  // Max 5 FRAGMENT Effects
72
    maxtable[3] = 5;  // Max 5 OTHER Effects
73 67
    }
74 68
///////////////////////////////////////////////////////////////////////////////////////////////////
75 69
  }

Also available in: Unified diff