[Java] Déformer une image...

Déformer une image... [Java] - Java - Programmation

Marsh Posté le 02-05-2008 à 15:11:57    

Salut tout le monde.  :hello:  
 
Je viens vous demander une petite aide  :ange: , voila je bosse sur un projet en J2me, et je cherche a faire une fonction addTexture qui permettrait de déformer une image rectangulaire en :  
-allongeant juste le coin supérieur gauche et/ou le coin supérieur droit,
-en la ratatinant en hauteur comme en largeur ou en l'agrandissant  
 
Pour le moment j'ai fais sa, mais sa me parait brouillon, vous n'auriez pas une idée pour que sa soit plus rapide? parce que sa rame un peu la ... et comme sa doit tenir sur un téléphone portable  :whistle:  
 
 

Code :
  1. public void addTexture(Image _texture, Point _pGHaut, Point _pDHaut, int _yBas, boolean _transform)
  2.     {
  3.  
  4.         if(_pGHaut.getX()>_pDHaut.getX())
  5.         {
  6.             Point BufPoint = _pDHaut;
  7.             _pDHaut = _pGHaut;
  8.             _pGHaut = BufPoint;
  9.             _texture = Image.createImage(_texture, 0, 0, _texture.getWidth(), _texture.getHeight(), Sprite.TRANS_MIRROR);
  10.         }
  11.         
  12.         if(_pGHaut.getY()>=_yBas || _pDHaut.getY()>_yBas || _yBas > this.__height)
  13.         {
  14.             return;
  15.         }
  16.         else
  17.         {
  18.             if(_transform==true)
  19.                 _texture = Image.createImage(_texture, 0, 0, _texture.getWidth(), _texture.getHeight(), Sprite.TRANS_MIRROR);
  20.             
  21.             Image textDeforme = deformerImage(_texture, _pDHaut.getX()-_pGHaut.getX(), _yBas-_pGHaut.getY(), _yBas-_pDHaut.getY());
  22.             int x = _pGHaut.getX();
  23.             int y = 0;
  24.             
  25.             if(_pGHaut.getY()>_pDHaut.getY())
  26.                 y = _pDHaut.getY();
  27.             else
  28.                 y = _pGHaut.getY();
  29.             
  30.             afficherImage(textDeforme, x, y);
  31.         }
  32.     }
  33.     
  34.     public Image deformerImage(Image _image, int _largeur, int _hauteur1, int _hauteur2)
  35.     {
  36.         
  37.         int oldWidth = _image.getWidth();
  38.         int oldHeight = _image.getHeight();    
  39.         int[] oldImage = readImage(_image);
  40.         
  41.         int cas = 0;
  42.         double dy = 0.0;    
  43.         int air = 0;
  44.         int larg = 0;
  45.         
  46.         if(_hauteur1>_hauteur2)
  47.         {
  48.             cas = 1;
  49.             dy = ((double)_hauteur1-(double)_hauteur2)/(double)oldWidth;    
  50.             air = oldWidth*_hauteur1;
  51.             larg = (oldWidth*(_hauteur1-1));                
  52.         }
  53.         else
  54.         {
  55.             dy = ((double)_hauteur2-(double)_hauteur1)/(double)oldWidth;    
  56.             air = oldWidth*_hauteur2;    
  57.             larg = (oldWidth*(_hauteur2-1));
  58.         }
  59.         
  60.         int lon = (oldWidth*(oldHeight-1));
  61.         double newHauteur = (double)_hauteur1;
  62.         int[] newImage = new int[air];
  63.         int[] bufImTab = null;
  64.         Image newIMAGE = null;
  65.         
  66.         for(int i=0; i<oldWidth; i++)
  67.         {
  68.             bufImTab = new int[oldHeight];
  69.             
  70.             //recupe tout une bande de hauteur
  71.             for(int j=0;j<oldHeight;j++)
  72.                 bufImTab[j] = oldImage[i+lon -(j*oldWidth)];
  73.             
  74.             //On la redimensionne
  75.             bufImTab = readImage(redimImage(Image.createRGBImage(bufImTab, 1, oldHeight, true), 1, (int)newHauteur));
  76.             
  77.             //On la place dans la nouvelle image
  78.             for(int k=0;k<(int)newHauteur;k++)
  79.             {
  80.                 if(newImage[k]==0)
  81.                     newImage[k] = defineColor(0,0,0,0);
  82.                     
  83.                 newImage[i+larg-(k*oldWidth)] = bufImTab[k];
  84.             }
  85.             
  86.             if(cas == 0)
  87.                 newHauteur +=dy;
  88.             else
  89.                 newHauteur -=dy;
  90.         
  91.         }
  92.         
  93.         if(cas==1)
  94.         {
  95.             return redimImage(Image.createRGBImage(newImage, oldWidth, _hauteur1, true),_largeur, _hauteur1);
  96.         }
  97.         else
  98.         {
  99.             return redimImage(Image.createRGBImage(newImage, oldWidth, _hauteur2, true),_largeur, _hauteur2);    
  100.         }
  101.             
  102.     }
  103.     
  104.     public Image redimImage(Image _image, int _longueur, int _hauteur)
  105.    {
  106.        int sourceWidth = _image.getWidth();
  107.        int sourceHeight = _image.getHeight();
  108.        int redimWidth = _longueur;
  109.        int redimHeight = _hauteur;
  110.         
  111.         int[] oldImage = readImage(_image);
  112.         int[] newImage = new int[redimWidth*redimHeight];
  113.         int compteur =0;
  114.         Image redimImage = Image.createImage(redimWidth, redimHeight);
  115.         
  116.         if(redimWidth > __width)
  117.             redimWidth = __width;
  118.             
  119.         if(redimHeight > __height)
  120.             redimHeight = __height;
  121.         
  122.        if (redimHeight < 0)
  123.             redimHeight = redimWidth * sourceHeight / sourceWidth;
  124.         
  125.        for (int y = 0; y < redimHeight; y++)
  126.        {
  127.            for (int x = 0; x < redimWidth; x++)
  128.            {
  129.  
  130.                int dx = x * sourceWidth / redimWidth;
  131.                int dy = y * sourceHeight / redimHeight;
  132.                newImage[compteur] = oldImage[dx+dy*sourceWidth];
  133.                 compteur++;
  134.            }
  135.        }
  136.      
  137.         redimImage = Image.createRGBImage(newImage, redimWidth, redimHeight, true);
  138.       
  139.        return redimImage;
  140.    }
  141.     
  142.     public void afficherImage(Image _image, int _x, int _y)
  143.     {
  144.     
  145.         int[] imData = readImage(_image);
  146.         int width = _image.getWidth();
  147.         int height = _image.getHeight();
  148.         int compteurX = 0;
  149.         int compteurY = 0;
  150.         
  151.         if(_x+_image.getWidth() > __width || _y+_image.getHeight() > __height )
  152.         {
  153.             System.out.println("PROBLEME AFFICHER IMAGE" );
  154.             return;
  155.         }
  156.         
  157.         for(int i=_y;i<(height+_y);i++)
  158.         {
  159.             
  160.             for(int j=_x;j<(width+_x);j++)
  161.             {
  162.                 if(0 <= _x && _x <= this.__width && 0 <= _y && _y <= this.__height)
  163.                     this.__tabJeux[j+(i*this.__width)] = imData[compteurX+compteurY*width];
  164.                 compteurX++;
  165.             }
  166.             
  167.             compteurX = 0;
  168.             compteurY++;
  169.         }
  170.     }
  171.     
  172.     public int[] readImage(Image _image)
  173.     {
  174.         int[] argbdata = new int[_image.getWidth()*_image.getHeight()];
  175.         
  176.         _image.getRGB( argbdata, 0,  _image.getWidth(),  0,  0,  _image.getWidth(),  _image.getHeight());
  177.         
  178.         return argbdata;
  179.     }


 
 
Merci a vous  :sol:  
 
Twub


Message édité par Twub le 02-05-2008 à 17:24:55
Reply

Marsh Posté le 02-05-2008 à 15:11:57   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed