Project

General

Profile

« Previous | Next » 

Revision c6e1c219

Added by Leszek Koltunski almost 8 years ago

Save PNG effect almost finished. Supporting App (hopefully!) completely finished.

What remains to be done: put actual saving of the Bitmap in a separate thread, away from the Graphics thread!!

View differences:

src/main/java/org/distorted/library/EffectQueueOther.java
13 13

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

  
......
34 34
  protected void moveEffect(int index)
35 35
    {
36 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];
37 42
    }
38 43

  
39 44
///////////////////////////////////////////////////////////////////////////////////////////////////
......
45 50
      {
46 51
      if (mType[i] == EffectNames.SAVE_PNG.ordinal() )
47 52
        {
48
        ByteBuffer buf = ByteBuffer.allocateDirect( (int)(mObjHalfX * mObjHalfY * 16) );
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 );
49 59
        buf.order(ByteOrder.LITTLE_ENDIAN);
50
        GLES20.glReadPixels(0, 0, (int)(2*mObjHalfX), (int)(2*mObjHalfY) , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
60
        GLES20.glReadPixels( left, top, width, height , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
51 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!
52 66
        buf.rewind();
53

  
54 67
        BufferedOutputStream bos = null;
55 68

  
56 69
        try
57 70
          {
58 71
          bos = new BufferedOutputStream(new FileOutputStream(mFilename[i]));
59
          Bitmap bmp = Bitmap.createBitmap( (int)(2*mObjHalfX), (int)(2*mObjHalfY), Bitmap.Config.ARGB_8888);
72
          Bitmap bmp = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);
60 73
          bmp.copyPixelsFromBuffer(buf);
61 74
          bmp.compress(Bitmap.CompressFormat.PNG, 90, bos);
62 75
          bmp.recycle();
......
66 79
                                            EffectMessage.EFFECT_FINISHED,
67 80
                                           (mID[i]<<EffectTypes.LENGTH)+EffectTypes.OTHER.type,
68 81
                                            mType[i],
69
                                            mBitmapID);
82
                                            mBitmapID,
83
                                            null);
70 84
          }
71 85
        catch(Exception e)
72 86
          {
......
75 89
                                            EffectMessage.EFFECT_FAILED,
76 90
                                           (mID[i]<<EffectTypes.LENGTH)+EffectTypes.OTHER.type,
77 91
                                            mType[i],
78
                                            mBitmapID);
92
                                            mBitmapID,
93
                                            e.getMessage());
79 94
          }
80 95
        finally
81 96
          {
......
99 114

  
100 115
///////////////////////////////////////////////////////////////////////////////////////////////////
101 116

  
102
  synchronized long add(EffectNames eln, String filename)
117
  synchronized long add(EffectNames eln, String filename, int left, int top, int width, int height)
103 118
    {
104 119
    if( mMax[INDEX]>mNumEffects )
105 120
      {
......
107 122
      mInterI[mNumEffects] = null;
108 123
      mInterP[mNumEffects] = null;
109 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

  
110 130
      return addBase(eln);
111 131
      }
112 132

  
113 133
    return -1;
114 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
    }
115 156
  }

Also available in: Unified diff