Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);
  2. final Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
  3. ByteBuffer pixels = pixmap.getPixels();
  4. byte[] bytes = new byte[pixels.remaining()];
  5. gl.glReadPixels(0, 0, width, height, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixels);
  6. pixels.get(bytes);
  7.  
  8. int bytesSize = bytes.length;
  9. int[] arrayWitoutAlpha = new int[bytesSize/4];
  10. int arrayPos = 0;
  11. for(int i = 0; i < bytesSize; i+=4) {
  12. arrayWitoutAlpha[arrayPos] = ((bytes[i]&0x0ff) << 16) | ((bytes[i+1]&0x0ff) << 8) | (bytes[i+2]&0x0ff);
  13. arrayPos++;
  14. }
  15. return arrayWitoutAlpha;
  16.  
  17. GifEncoder encoder = new GifEncoder(Gdx.files.external("testgif.gif").write(false),
  18. gifWidth, gifHeight, 1);
  19. ImageOptions options = new ImageOptions();
  20. for (int i = 0; i < framesArray.length; i++) { //framesArray - two-dimentional int array with frames
  21. encoder.addImage(framesArray[i], gifWidth, options);
  22. }
  23. encoder.finishEncoding();
  24.  
  25. Pixmap pixmap = ...
  26.  
  27. int width = pixmap.getWidth(), height = pixmap.getHeight();
  28. Pixmap flipped = new Pixmap(width, height, pixmap.getFormat());
  29.  
  30. for (int x = 0; x < width; ++x)
  31. for (int y = 0; y < height; ++y)
  32. flipped.drawPixel(x, y, pixmap.getPixel(x, height - y - 1));
  33.  
  34. pixmap.dispose();
  35. pixmap = flipped;
  36.  
  37. int pixel = pixmap.getPixel(x, y); // RGBA
  38.  
  39. int r = (pixel >> 24) & 0xFF;
  40. int g = (pixel >> 16) & 0xFF;
  41. int b = (pixel >> 8) & 0xFF;
  42. int a = pixel & 0xFF;
  43.  
  44. pixel = (a << 24) + (r << 16) + (g << 8) + b; // ARGB
  45.  
  46. Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
  47. ByteBuffer pixels = pixmap.getPixels();
  48. gl.glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  49. frames.add(pixmap);
  50.  
  51. // Note that if loopCount is less than 1, then the gif will loop indefinitely
  52. GifEncoder encoder = new GifEncoder(..., width, height, 0);
  53.  
  54. ImageOptions options = new ImageOptions();
  55.  
  56. // Frame rate / Time between each frame
  57. options.setDelay(50, TimeUnit.MILLISECONDS);
  58.  
  59. for (Pixmap pixmap : frames) {
  60. int width = pixmap.getWidth(), height = pixmap.getHeight();
  61. int[][] pixels = new int[height][width];
  62.  
  63. ... flip pixmap on the y-axis ...
  64.  
  65. for (int x = 0; x < width; ++x) {
  66. for (int y = 0; y < height; ++y) {
  67. int pixel = pixmap.getPixel(x, y);
  68.  
  69. ... change pixel format ...
  70.  
  71. pixels[y][x] = pixel;
  72. }
  73. }
  74.  
  75. encoder.addImage(pixels, options);
  76. }
  77.  
  78. encoder.finishEncoding();
  79.  
  80. new Thread(new Runnable() {
  81. @Override
  82. public void run() {
  83. ... save gif ...
  84. }
  85. }).start();
  86.  
  87. for (Pixmap pixmap : frames)
  88. pixmap.dispose();
  89. frames.clear();
  90.  
  91. PixmapIO.writePNG(fileHandle, pixmap);
  92.  
  93. import static com.badlogic.gdx.graphics.GL20.*;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement