Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FrameBuffer{
- ArrayList<PImage> frames = new ArrayList<PImage>();
- boolean shouldCaptureFrames = true;
- boolean hasSaved = false;
- int capacity;
- int chunkSize = 10;
- boolean logBufferInfo;
- FrameBuffer(int _maxCapacity){
- capacity = _maxCapacity;
- }
- void saveBuffer(){
- int chunkTotal = ((frames.size() - (frames.size() % chunkSize)) / chunkSize) + 1;
- int currentChunk = 0;
- printChunkTotal(chunkTotal);
- ArrayList<PImage> chunk = new ArrayList<PImage>(chunkSize);
- for(int i = 0; i < frames.size(); i++){
- chunk.add(frames.get(i));
- if((i+1) % chunkSize == 0 || (i+1) % frames.size() == 0){
- Thread thread = new Thread( new SaveImageChunks(chunk, currentChunk, chunkSize) );
- thread.start();
- chunk.clear();
- currentChunk += 1;
- }
- }
- frames.clear();
- hasSaved = true;
- }
- String queryState(){
- if(hasSaved){
- return "Saved";
- } else {
- return str(frames.size());
- }
- }
- void addFrame(PImage image){
- if(shouldCaptureFrames){
- hasSaved = false;
- if(frames.size() < capacity){
- frames.add(image);
- }
- }
- }
- void printChunkTotal(int chunkTotal){
- if(logBufferInfo){
- println("Chunk Total: "+ chunkTotal);
- }
- }
- }
- class SaveImageChunks implements Runnable{
- PImage[] images;
- int chunkIndex;
- int chunkSize;
- boolean logThreadInfo;
- SaveImageChunks(ArrayList<PImage> imageList, int _chunkIndex, int _chunkSize){
- chunkIndex = _chunkIndex;
- chunkSize = _chunkSize;
- images = new PImage[imageList.size()];
- for(int n = 0; n < imageList.size(); n++){
- images[n] = imageList.get(n);
- }
- }
- void run(){
- startInfo();
- saveImages();
- endInfo();
- }
- void saveImages(){
- if(images.length == 0){
- println("Attempted to save buffer, but it was empty.");
- } else {
- for(int n = 0; n < images.length; n++){
- images[n].save("/frames/f-"+ (n+(chunkSize*(chunkIndex))) +".png");
- }
- }
- }
- void startInfo(){
- if(logThreadInfo){
- println("Image saving thread started. (Indices: "+ (chunkSize*chunkIndex)+","+ (chunkSize*chunkIndex + (images.length-1))+")");
- }
- }
- void endInfo(){
- if(logThreadInfo){
- println("Saved "+images.length +" images.");
- println("Thread finished.");
- }
- }
- }
Add Comment
Please, Sign In to add comment