Advertisement
Guest User

Untitled

a guest
Aug 18th, 2012
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1.  
  2.  
  3. /*
  4.  * To change this template, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. /**
  8.  *
  9.  * @author dosse
  10.  */
  11. public class Renderer {
  12.  
  13.     private static int nThread = 4;
  14.  
  15.     public static int[] renderInt(Randomatic r, int offx, int offy, int w, int h, float amp) {
  16.         System.out.println("from " + offy + " to " + (offy + h - 1));
  17.         int[] pixels = new int[w * h];
  18.         for (int y = 0; y < h; y++) {
  19.          for (int x = 0; x < w; x++) {
  20.          pixels[y * w + x] = (int) (r.getValue(x + offx, y + offy, amp)*255);
  21.          }
  22.          }
  23.         return pixels;
  24.     }
  25.  
  26.     private static class RenderThread extends Thread {
  27.  
  28.         private Randomatic r;
  29.         private int offx, offy, w, h;
  30.         private float amp;
  31.         private boolean done=false;
  32.         private int[] data;
  33.  
  34.         public RenderThread(Randomatic r, int offx, int offy, int w, int h, float amp) {
  35.             this.r = r;
  36.             this.offx = offx;
  37.             this.offy = offy;
  38.             this.w = w;
  39.             this.h = h;
  40.             this.amp = amp;
  41.         }
  42.        
  43.         @Override
  44.         public void run() {
  45.             data=renderInt(r, offx, offy, w, h, amp);
  46.             done=true;
  47.         }
  48.         public boolean isDone(){return done;}
  49.         public int[] getData(){return data;}
  50.     }
  51.  
  52.     public static int[] renderIntMT(Randomatic r, int offx, int offy, int w, int h, float amp) {
  53.         if (h <= 256) {
  54.             return renderInt(r, offx, offy, w, h, amp);
  55.         }
  56.         int[] pixels = new int[w * h];
  57.         int portion = (int) Math.floor(h / nThread);
  58.         int c = 0;
  59.         RenderThread[] threads=new RenderThread[nThread+1];
  60.         int[][] rendered = new int[nThread+1][];
  61.         for (int i = 0; i < h; i += portion) {
  62.             int ty = i, th = (i + portion) > h ? h - i : portion;
  63.                 threads[c]=new RenderThread(r, offx, i, w, th, amp);
  64.                 threads[c].start();
  65.                 c++;
  66.         }
  67.         boolean completed=false;
  68.         System.out.println("waiting");
  69.         while(!completed){
  70.             try {Thread.sleep(100);} catch (InterruptedException ex) {}
  71.             completed=true;
  72.             for(int i=0;i<threads.length;i++){
  73.                 if(threads[i]==null) continue;
  74.                 if(threads[i].isDone()) rendered[i]=threads[i].getData(); else completed=false;
  75.             }
  76.         }
  77.         int k=0; long timestamp=System.nanoTime();
  78.         for(int i=0;i<rendered.length;i++){
  79.             if(rendered[i]==null) continue;
  80.             for(int j=0;j<rendered[i].length;j++){
  81.                 pixels[k]=rendered[i][j];
  82.                 k++;
  83.             }
  84.         }
  85.         System.out.println("Reassembled in "+(System.nanoTime()-timestamp)/1000000+" ms");
  86.         return pixels;
  87.     }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement