Advertisement
thibthibaut

MatrixRotationJavaBorneo

Oct 14th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.27 KB | None | 0 0
  1. // Name(s):
  2. // rotateMatrixV00
  3. // Initial Time (simu)      :
  4. // Initial Time (plateform) :
  5. //==========================================
  6. // rotateMatrixV01
  7. // Time (simu)      :
  8. // Time (plateform) :
  9. // Optimisation description:
  10. //==========================================
  11. // rotateMatrixV02
  12. // Time (simu)      :
  13. // Time (plateform) :
  14. // Optimisation description:
  15.  
  16.  
  17.  
  18.  
  19. package midletselector;
  20.  
  21. import javax.microedition.midlet.MIDlet;
  22. import javax.microedition.lcdui.*;
  23. import com.sun.midp.lcdui.Resource;
  24. import com.sun.midp.midlet.*;
  25. import java.lang.*;
  26. import borneo.graphik.*;
  27. import borneo.system.Borneo;
  28. import borneo.system.BorneoFS;
  29.  
  30. public class Displayable1 extends Canvas implements CommandListener,Runnable {
  31.  
  32.     // image
  33.     Matrix  outputMatrix;
  34.    
  35.     // global variable used in the process example
  36.     int     globalPos;
  37.    
  38.     // signaling paint() is working
  39.     int     repainting;
  40.    
  41.     // processing time in (milliseconds ms)
  42.     int     processTime, iteration;
  43.    
  44.     // DEBUG=1 : use of debug methods; DEBUG=0 : run on hardware platform
  45.     final static int DEBUG=0;
  46.    
  47.    
  48.     //**********************************
  49.     // static class initialiser
  50.     //**********************************
  51.     static  {
  52.         try {
  53.            
  54.         } catch(Exception e) { e.printStackTrace(); }
  55.     }
  56.  
  57.     //**********************************
  58.     /** Constructor */
  59.     //**********************************
  60.     public Displayable1(AppliSelector pparent) {
  61.         try {
  62.             setCommandListener(this);
  63.         } catch(Exception e) { e.printStackTrace(); }
  64.  
  65.         // start a Thread (with interface (implements keyword))
  66.         new Thread(this).start();
  67.     }
  68.  
  69.     // *******************************
  70.     // Thread starts here
  71.     // *******************************
  72.     public void run() {
  73.         try {
  74.             double  dtmp;
  75.             int     desc, size,val;
  76.             byte    fileBuf[];
  77.        
  78.             //==========================================================================================================================
  79.             // Load MATRIX (image)
  80.             // this code part shows how to load an PNG image from Borneo File System and how to convert it into a 1 dimensional matrix
  81.             // "image file name from the root project".png directory space/Tab "name file name in the Borneo system".png
  82.             // in this case we have :
  83.             // images/java.png  images/java.png    
  84.             BImage imageMatrix=new BImage();
  85.             // open file : file name must match with the name in BorneoU.list
  86.             desc=BorneoFS.open("images/java.png");
  87.             // set file read index at the end of the file (get the file size)
  88.             size=BorneoFS.seek(desc, 0, BorneoFS.SEEK_END);        
  89.             Borneo.DebugPrint("fileSize="+size+"\n");
  90.             // set file read index at the begining of the file
  91.             BorneoFS.seek(desc, 0, BorneoFS.SEEK_SET);
  92.             fileBuf=new byte[size];
  93.             // read file and put data in fileBuf buffer
  94.             BorneoFS.read(desc, fileBuf, 0, fileBuf.length);
  95.             BorneoFS.close(desc);
  96.             // decode data (PNG encoded) and put data in ARGB buffer
  97.             val=imageMatrix.getARGB(fileBuf);
  98.             if(val<=0) Borneo.DebugPrint("Unable to load file\n");
  99.             else Borneo.DebugPrint("image size width="+imageMatrix.width+" height="+imageMatrix.height+" ARGBsize="+imageMatrix.ARGB.length+"\n");
  100.             //==========================================================================================================================
  101.            
  102.            
  103.             //==========================================================================================================================
  104.             // Example of time evaluation
  105.             // store current time (in milliseconds from the last system boot) in startTime (Time before sinus)
  106.             long startTime=System.currentTimeMillis();
  107.             dtmp=Math.sin(3.14/3, 10)*10000;
  108.             // get the current time (Time after sinus)
  109.             long stopTime=System.currentTimeMillis();
  110.            
  111.             // use constant DEBUG to activate/desactivate debug information
  112.             if(DEBUG!=0) {
  113.                 // display time used to compute Math.sin : (time before sin()) - (time after sin())
  114.                 Borneo.DebugPrint("Sin Time="+(int)(System.currentTimeMillis()-startTime)+"\n");
  115.                 Borneo.DebugPrint("sin="+(int)(dtmp)+"\n");
  116.             }
  117.             dtmp=Math.cos(3.14/3, 10)*10000;
  118.             if(DEBUG!=0) Borneo.DebugPrint("cos="+(int)(dtmp)+"\n");
  119.             //==========================================================================================================================
  120.            
  121.             outputMatrix=new Matrix((int)( 1.42 *imageMatrix.width) , (int)( 1.42 * imageMatrix.height));
  122.              
  123.             //==========================================================================================================================
  124.             // Main loop
  125.             //==================
  126.             Borneo.DebugPrint("SAAAAAD");
  127.             while(true){
  128.                 Borneo.DebugPrint("HAPPPYYYYY");
  129.                 // wait the end of the painting
  130.                 while(repainting==1) Thread.yield();
  131.                 iteration++;
  132.                 // Time before processing
  133.                 startTime=System.currentTimeMillis();
  134.                
  135.                 //**********************************************
  136.                 // Rotation matrix is here
  137.                 //processMatrix(imageMatrix, outputMatrix);
  138.                 rotateMatrix(imageMatrix, outputMatrix);
  139.            
  140.                 //**********************************************
  141.                
  142.                 // time after processing
  143.                 stopTime=System.currentTimeMillis();
  144.                 processTime=(int)(stopTime-startTime);
  145.                 // lock paint
  146.            
  147.                
  148.                 repainting=1;
  149.                 // launch paint
  150.                 repaint();
  151.                 while(true);
  152.                
  153.                
  154.             }
  155.            
  156.            
  157.            
  158.             //==========================================================================================================================
  159.         } catch(Exception e) { e.printStackTrace(); }
  160.     }
  161.  
  162.     //********************************************************
  163.     // method : perform a specific operation on the Matrix
  164.     // fill this method with the matrix rotation algorithm
  165.     //********************************************************
  166.     void processMatrix(BImage inputMatrix, Matrix outputMatrix) {
  167.        
  168.        
  169.         /**
  170.         Matrix rotation:
  171.         initial image -> rotated image
  172.         X'=  X*cos(angle) + Y*sin(angle)
  173.         Y'= -X*sin(angle) + Y*cos(angle)
  174.         rotated image -> initial image
  175.         X= X'*cos(angle) - Y'*sin(angle)
  176.         Y= X'*sin(angle) - Y'*cos(angle)
  177.          */
  178.        
  179.        
  180.         // copy inputMatrix in outputMatrix
  181.         int i;
  182.         for(i=0; i<inputMatrix.ARGB.length; i++) outputMatrix.mat[i]=inputMatrix.ARGB[i];
  183.         for(i=0; i<globalPos; i++) outputMatrix.mat[i]=0xff0000;
  184.         globalPos++;
  185.     }
  186.    
  187.     //******************************************
  188.     // Draw items on screen
  189.     //******************************************
  190.     protected void paint(Graphics g) {
  191.         g.setColor(0xffffff);
  192.         g.fillRect(0,0,480,272);
  193.         g.drawRGB(outputMatrix.mat, 0, outputMatrix.width, 50, 50, outputMatrix.width, outputMatrix.height, false);
  194.         g.setColor(0);
  195.         g.drawString("process time="+processTime+" ms ite="+iteration, 10, 200, 0);
  196.         repainting=0;
  197.     }
  198.  
  199.     public void commandAction(Command command, Displayable displayable) {
  200.     }
  201.  
  202.     protected void showNotify() {
  203.     }
  204.  
  205.     protected void hideNotify() {
  206.     }
  207.  
  208.     // method launched when the touch pad is pressed
  209.     public void pointerPressed(int x, int y)  {
  210.     }
  211.  
  212.     // method launched when the touch pad is dragged (the pointer stay pressed andslides on the touchpad)
  213.     public void pointerDragged(int x, int y)  {
  214.     }
  215.  
  216.     // method launched when the touch pad is released
  217.     public void pointerReleased(int x, int y)  {
  218.     }
  219.  
  220.     // method launched when a key is released
  221.     public void keyReleased(int keyCode)  {
  222.     }
  223.  
  224.     // method launched when a key is pressed
  225.     public void keyPressed(int keyCode) {
  226.     }
  227.    
  228.    
  229.    
  230.    
  231.     /** MATRIX ROTATION
  232.      * author: Thibaut Vercueil, groupe 10
  233.      * @param inputMatrix
  234.      * @param outputMatrix
  235.      */
  236.     void rotateMatrix(BImage inputMatrix, Matrix outputMatrix) {
  237.        
  238.         int width = inputMatrix.width;
  239.         int height = outputMatrix.width;
  240.        
  241.         int newWidth = outputMatrix.width;
  242.         int newLength = newWidth*newWidth;
  243.        
  244.         int length = width * height -1;
  245.        
  246.         double angle = 3.14/3;
  247.        
  248.         angle %= 2*3.14;
  249.        
  250.         double cos = Math.cos( angle, 15);
  251.         double sin = Math.sin( angle, 15);
  252.         double minus_sin = -sin;
  253.        
  254.         int offset_x = 0;
  255.         int offset_y = 0;
  256.        
  257.         //Computings extreme points
  258.         int top_r_x = (int) (  (width-1) * cos + (0) * sin );
  259.         int top_r_y = (int) ( -(width-1) * sin + (0) * cos );
  260.        
  261.         int bot_r_x = (int) (  (width-1) * cos + (width-1) * sin );
  262.         int bot_r_y = (int) ( -(width-1) * sin + (width-1) * cos );
  263.        
  264.         int bot_l_x = (int) (  (0) * cos + (width-1) * sin );
  265.         int bot_l_y = (int) ( -(0) * sin + (width-1) * cos );
  266.        
  267.         if(angle >= 0 && angle < 3.14/2){
  268.             offset_y = -top_r_y;
  269.             offset_x = 0;
  270.         }
  271.        
  272.         if(angle >= 3.14/2 && angle < 3.14){
  273.             offset_y = -bot_r_y;
  274.             offset_x = -top_r_x;
  275.         }
  276.        
  277.         if(angle >= 3.14 && angle < (3*3.14)/2 ){
  278.             offset_y = -bot_l_y;
  279.             offset_x = -bot_r_x;
  280.         }
  281.        
  282.         if(angle >= (3*3.14)/2 && angle < 2*3.14 ){
  283.             offset_y = 0;
  284.             offset_x = -bot_l_x;
  285.         }
  286.        
  287.         int i, i_out, x_in, y_in, x_out, y_out, i_out_minus, i_out_plus;
  288.    
  289.        
  290.         for(i=0; i<length; i++){
  291.            
  292.             x_in = i % width;
  293.             y_in = i / width;
  294.            
  295.             //Main code here
  296.             x_out = (int) ( x_in * cos + y_in * sin + offset_x  ) ;
  297.             y_out = (int) ( x_in * minus_sin + y_in * cos + offset_y ) ;
  298.            
  299.             i_out = (int) (x_out + newWidth*y_out);
  300.             i_out_minus = i_out -1;
  301.             i_out_plus = i_out +1;
  302.                
  303.             if(i_out_minus>0 && i_out_plus<newLength){
  304.             outputMatrix.mat[i_out_minus]=inputMatrix.ARGB[i];
  305.             outputMatrix.mat[i_out]=inputMatrix.ARGB[i];
  306.             outputMatrix.mat[i_out_plus]=inputMatrix.ARGB[i];
  307.             }
  308.             if(i%200==0)
  309.             repaint();
  310.        
  311.         }
  312.            
  313.     }
  314.    
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement