Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name(s):
- // rotateMatrixV00
- // Initial Time (simu) :
- // Initial Time (plateform) :
- //==========================================
- // rotateMatrixV01
- // Time (simu) :
- // Time (plateform) :
- // Optimisation description:
- //==========================================
- // rotateMatrixV02
- // Time (simu) :
- // Time (plateform) :
- // Optimisation description:
- package midletselector;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.lcdui.*;
- import com.sun.midp.lcdui.Resource;
- import com.sun.midp.midlet.*;
- import java.lang.*;
- import borneo.graphik.*;
- import borneo.system.Borneo;
- import borneo.system.BorneoFS;
- public class Displayable1 extends Canvas implements CommandListener,Runnable {
- // image
- Matrix outputMatrix;
- // global variable used in the process example
- int globalPos;
- // signaling paint() is working
- int repainting;
- // processing time in (milliseconds ms)
- int processTime, iteration;
- // DEBUG=1 : use of debug methods; DEBUG=0 : run on hardware platform
- final static int DEBUG=0;
- //**********************************
- // static class initialiser
- //**********************************
- static {
- try {
- } catch(Exception e) { e.printStackTrace(); }
- }
- //**********************************
- /** Constructor */
- //**********************************
- public Displayable1(AppliSelector pparent) {
- try {
- setCommandListener(this);
- } catch(Exception e) { e.printStackTrace(); }
- // start a Thread (with interface (implements keyword))
- new Thread(this).start();
- }
- // *******************************
- // Thread starts here
- // *******************************
- public void run() {
- try {
- double dtmp;
- int desc, size,val;
- byte fileBuf[];
- //==========================================================================================================================
- // Load MATRIX (image)
- // this code part shows how to load an PNG image from Borneo File System and how to convert it into a 1 dimensional matrix
- // "image file name from the root project".png directory space/Tab "name file name in the Borneo system".png
- // in this case we have :
- // images/java.png images/java.png
- BImage imageMatrix=new BImage();
- // open file : file name must match with the name in BorneoU.list
- desc=BorneoFS.open("images/java.png");
- // set file read index at the end of the file (get the file size)
- size=BorneoFS.seek(desc, 0, BorneoFS.SEEK_END);
- Borneo.DebugPrint("fileSize="+size+"\n");
- // set file read index at the begining of the file
- BorneoFS.seek(desc, 0, BorneoFS.SEEK_SET);
- fileBuf=new byte[size];
- // read file and put data in fileBuf buffer
- BorneoFS.read(desc, fileBuf, 0, fileBuf.length);
- BorneoFS.close(desc);
- // decode data (PNG encoded) and put data in ARGB buffer
- val=imageMatrix.getARGB(fileBuf);
- if(val<=0) Borneo.DebugPrint("Unable to load file\n");
- else Borneo.DebugPrint("image size width="+imageMatrix.width+" height="+imageMatrix.height+" ARGBsize="+imageMatrix.ARGB.length+"\n");
- //==========================================================================================================================
- //==========================================================================================================================
- // Example of time evaluation
- // store current time (in milliseconds from the last system boot) in startTime (Time before sinus)
- long startTime=System.currentTimeMillis();
- dtmp=Math.sin(3.14/3, 10)*10000;
- // get the current time (Time after sinus)
- long stopTime=System.currentTimeMillis();
- // use constant DEBUG to activate/desactivate debug information
- if(DEBUG!=0) {
- // display time used to compute Math.sin : (time before sin()) - (time after sin())
- Borneo.DebugPrint("Sin Time="+(int)(System.currentTimeMillis()-startTime)+"\n");
- Borneo.DebugPrint("sin="+(int)(dtmp)+"\n");
- }
- dtmp=Math.cos(3.14/3, 10)*10000;
- if(DEBUG!=0) Borneo.DebugPrint("cos="+(int)(dtmp)+"\n");
- //==========================================================================================================================
- outputMatrix=new Matrix((int)( 1.42 *imageMatrix.width) , (int)( 1.42 * imageMatrix.height));
- //==========================================================================================================================
- // Main loop
- //==================
- Borneo.DebugPrint("SAAAAAD");
- while(true){
- Borneo.DebugPrint("HAPPPYYYYY");
- // wait the end of the painting
- while(repainting==1) Thread.yield();
- iteration++;
- // Time before processing
- startTime=System.currentTimeMillis();
- //**********************************************
- // Rotation matrix is here
- //processMatrix(imageMatrix, outputMatrix);
- rotateMatrix(imageMatrix, outputMatrix);
- //**********************************************
- // time after processing
- stopTime=System.currentTimeMillis();
- processTime=(int)(stopTime-startTime);
- // lock paint
- repainting=1;
- // launch paint
- repaint();
- while(true);
- }
- //==========================================================================================================================
- } catch(Exception e) { e.printStackTrace(); }
- }
- //********************************************************
- // method : perform a specific operation on the Matrix
- // fill this method with the matrix rotation algorithm
- //********************************************************
- void processMatrix(BImage inputMatrix, Matrix outputMatrix) {
- /**
- Matrix rotation:
- initial image -> rotated image
- X'= X*cos(angle) + Y*sin(angle)
- Y'= -X*sin(angle) + Y*cos(angle)
- rotated image -> initial image
- X= X'*cos(angle) - Y'*sin(angle)
- Y= X'*sin(angle) - Y'*cos(angle)
- */
- // copy inputMatrix in outputMatrix
- int i;
- for(i=0; i<inputMatrix.ARGB.length; i++) outputMatrix.mat[i]=inputMatrix.ARGB[i];
- for(i=0; i<globalPos; i++) outputMatrix.mat[i]=0xff0000;
- globalPos++;
- }
- //******************************************
- // Draw items on screen
- //******************************************
- protected void paint(Graphics g) {
- g.setColor(0xffffff);
- g.fillRect(0,0,480,272);
- g.drawRGB(outputMatrix.mat, 0, outputMatrix.width, 50, 50, outputMatrix.width, outputMatrix.height, false);
- g.setColor(0);
- g.drawString("process time="+processTime+" ms ite="+iteration, 10, 200, 0);
- repainting=0;
- }
- public void commandAction(Command command, Displayable displayable) {
- }
- protected void showNotify() {
- }
- protected void hideNotify() {
- }
- // method launched when the touch pad is pressed
- public void pointerPressed(int x, int y) {
- }
- // method launched when the touch pad is dragged (the pointer stay pressed andslides on the touchpad)
- public void pointerDragged(int x, int y) {
- }
- // method launched when the touch pad is released
- public void pointerReleased(int x, int y) {
- }
- // method launched when a key is released
- public void keyReleased(int keyCode) {
- }
- // method launched when a key is pressed
- public void keyPressed(int keyCode) {
- }
- /** MATRIX ROTATION
- * author: Thibaut Vercueil, groupe 10
- * @param inputMatrix
- * @param outputMatrix
- */
- void rotateMatrix(BImage inputMatrix, Matrix outputMatrix) {
- int width = inputMatrix.width;
- int height = outputMatrix.width;
- int newWidth = outputMatrix.width;
- int newLength = newWidth*newWidth;
- int length = width * height -1;
- double angle = 3.14/3;
- angle %= 2*3.14;
- double cos = Math.cos( angle, 15);
- double sin = Math.sin( angle, 15);
- double minus_sin = -sin;
- int offset_x = 0;
- int offset_y = 0;
- //Computings extreme points
- int top_r_x = (int) ( (width-1) * cos + (0) * sin );
- int top_r_y = (int) ( -(width-1) * sin + (0) * cos );
- int bot_r_x = (int) ( (width-1) * cos + (width-1) * sin );
- int bot_r_y = (int) ( -(width-1) * sin + (width-1) * cos );
- int bot_l_x = (int) ( (0) * cos + (width-1) * sin );
- int bot_l_y = (int) ( -(0) * sin + (width-1) * cos );
- if(angle >= 0 && angle < 3.14/2){
- offset_y = -top_r_y;
- offset_x = 0;
- }
- if(angle >= 3.14/2 && angle < 3.14){
- offset_y = -bot_r_y;
- offset_x = -top_r_x;
- }
- if(angle >= 3.14 && angle < (3*3.14)/2 ){
- offset_y = -bot_l_y;
- offset_x = -bot_r_x;
- }
- if(angle >= (3*3.14)/2 && angle < 2*3.14 ){
- offset_y = 0;
- offset_x = -bot_l_x;
- }
- int i, i_out, x_in, y_in, x_out, y_out, i_out_minus, i_out_plus;
- for(i=0; i<length; i++){
- x_in = i % width;
- y_in = i / width;
- //Main code here
- x_out = (int) ( x_in * cos + y_in * sin + offset_x ) ;
- y_out = (int) ( x_in * minus_sin + y_in * cos + offset_y ) ;
- i_out = (int) (x_out + newWidth*y_out);
- i_out_minus = i_out -1;
- i_out_plus = i_out +1;
- if(i_out_minus>0 && i_out_plus<newLength){
- outputMatrix.mat[i_out_minus]=inputMatrix.ARGB[i];
- outputMatrix.mat[i_out]=inputMatrix.ARGB[i];
- outputMatrix.mat[i_out_plus]=inputMatrix.ARGB[i];
- }
- if(i%200==0)
- repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement