Munashedov

FrameBuffer class

Jun 29th, 2021
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. class FrameBuffer{
  2.   ArrayList<PImage> frames = new ArrayList<PImage>();
  3.   boolean shouldCaptureFrames = true;
  4.   boolean hasSaved = false;
  5.   int capacity;
  6.   int chunkSize = 10;
  7.   boolean logBufferInfo;
  8.  
  9.   FrameBuffer(int _maxCapacity){
  10.     capacity = _maxCapacity;
  11.   }
  12.  
  13.   void saveBuffer(){
  14.     int chunkTotal = ((frames.size() - (frames.size() % chunkSize)) / chunkSize) + 1;
  15.     int currentChunk = 0;
  16.     printChunkTotal(chunkTotal);
  17.     ArrayList<PImage> chunk = new ArrayList<PImage>(chunkSize);
  18.     for(int i = 0; i < frames.size(); i++){
  19.         chunk.add(frames.get(i));
  20.         if((i+1) % chunkSize == 0 || (i+1) % frames.size() == 0){
  21.           Thread thread = new Thread( new SaveImageChunks(chunk, currentChunk, chunkSize) );
  22.           thread.start();
  23.           chunk.clear();
  24.           currentChunk += 1;
  25.         }
  26.     }
  27.     frames.clear();
  28.     hasSaved = true;
  29.   }
  30.  
  31.   String queryState(){
  32.     if(hasSaved){
  33.       return "Saved";
  34.     } else {
  35.       return str(frames.size());
  36.     }
  37.   }
  38.  
  39.   void addFrame(PImage image){
  40.     if(shouldCaptureFrames){
  41.       hasSaved = false;
  42.       if(frames.size() < capacity){
  43.         frames.add(image);
  44.       }
  45.     }
  46.   }
  47.  
  48.   void printChunkTotal(int chunkTotal){
  49.     if(logBufferInfo){
  50.       println("Chunk Total: "+ chunkTotal);
  51.     }
  52.   }
  53. }
  54.  
  55.  
  56.  
  57.  
  58. class SaveImageChunks implements Runnable{
  59.   PImage[] images;
  60.   int chunkIndex;
  61.   int chunkSize;
  62.   boolean logThreadInfo;
  63.  
  64.   SaveImageChunks(ArrayList<PImage> imageList, int _chunkIndex, int _chunkSize){
  65.     chunkIndex = _chunkIndex;
  66.     chunkSize = _chunkSize;
  67.     images = new PImage[imageList.size()];
  68.     for(int n = 0; n < imageList.size(); n++){
  69.       images[n] = imageList.get(n);
  70.     }
  71.   }
  72.  
  73.   void run(){
  74.     startInfo();
  75.     saveImages();
  76.     endInfo();
  77.   }
  78.  
  79.   void saveImages(){
  80.     if(images.length == 0){
  81.       println("Attempted to save buffer, but it was empty.");
  82.     } else {
  83.       for(int n = 0; n < images.length; n++){
  84.         images[n].save("/frames/f-"+ (n+(chunkSize*(chunkIndex))) +".png");
  85.       }
  86.      
  87.     }
  88.   }
  89.   void startInfo(){
  90.     if(logThreadInfo){
  91.       println("Image saving thread started. (Indices: "+ (chunkSize*chunkIndex)+","+ (chunkSize*chunkIndex + (images.length-1))+")");
  92.     }
  93.   }
  94.   void endInfo(){
  95.     if(logThreadInfo){
  96.       println("Saved "+images.length +" images.");
  97.       println("Thread finished.");
  98.     }
  99.   }
  100. }
Add Comment
Please, Sign In to add comment