
package source;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Radoslaf
 */
public class ImageRoseProccesing {

    // Obrázky růžice
    private Image rose_0;
    private Image rose_45;
    private Image rose_90;
    private Image rose_135;
    private Image rose_180;
    private Image rose_225;
    private Image rose_270;
    private Image rose_315;

    public Image getRose_0() {
        if (rose_0 == null) {
            try {
                rose_0 = Image.createImage("/img/rose/rose_0.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_0;
    }
    public Image getRose_45() {
        if (rose_45 == null) {
            try {
                rose_45 = Image.createImage("/img/rose/rose_45.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_45;
    }
    public Image getRose_90() {
        if (rose_90 == null) {
            try {
                rose_90 = Image.createImage("/img/rose/rose_90.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_90;
    }
    public Image getRose_135() {
        if (rose_135 == null) {
            try {
                rose_135 = Image.createImage("/img/rose/rose_135.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_135;
    }
    public Image getRose_180() {
        if (rose_180 == null) {
            try {
                rose_180 = Image.createImage("/img/rose/rose_180.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_180;
    }
    public Image getRose_225() {
        if (rose_225 == null) {
            try {
                rose_225 = Image.createImage("/img/rose/rose_225.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_225;
    }
    public Image getRose_270() {
        if (rose_270 == null) {
            try {
                rose_270 = Image.createImage("/img/rose/rose_270.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_270;
    }
    public Image getRose_315() {
        if (rose_315 == null) {
            try {
                rose_315 = Image.createImage("/img/rose/rose_315.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return rose_315;
    }

    

    private Kompas komp = null;
    public Image aktrose = null;

    public ImageRoseProccesing(Kompas komp) {
        this.komp = komp;
    }

    public Image retrieveImage(){
        short aktdir = 0;
        aktdir = komp.direction;
        if (aktdir < 22)
            this.aktrose = this.getRose_0();
        if (aktdir >= 23 && aktdir < 67)
            this.aktrose = this.getRose_45();
        if (aktdir >= 68 && aktdir < 112)
            this.aktrose = this.getRose_90();
        if (aktdir >= 113 && aktdir < 157)
            this.aktrose = this.getRose_135();
        if (aktdir >= 158 && aktdir < 202)
            this.aktrose = this.getRose_180();
        if (aktdir >= 203 && aktdir < 247)
            this.aktrose = this.getRose_225();
        if (aktdir >= 248 && aktdir < 292)
            this.aktrose = this.getRose_270();
        if (aktdir >= 293 && aktdir < 337)
            this.aktrose = this.getRose_315();
        if (aktdir >= 338)
            this.aktrose = this.getRose_0();
        this.aktrose = resizeImage(aktrose);
        return this.aktrose;
    }


    private Image resizeImage(Image src) {
      int srcWidth = src.getWidth();
      int srcHeight = src.getHeight();
      Image tmp = Image.createImage(this.komp.getWidth(), srcHeight);
      Graphics g = tmp.getGraphics();
      int ratio = (srcWidth << 16) / this.komp.getWidth();
      int pos = ratio/2;

      //Horizontal Resize

      for (int x = 0; x < this.komp.getWidth(); x++) {
          g.setClip(x, 0, 1, srcHeight);
          g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }

      Image resizedImage = Image.createImage(this.komp.getWidth(), this.komp.getWidth());
      g = resizedImage.getGraphics();
      ratio = (srcHeight << 16) / this.komp.getWidth();
      pos = ratio/2;

      //Vertical resize

      for (int y = 0; y < this.komp.getWidth(); y++) {
          g.setClip(0, y, this.komp.getWidth(), 1);
          g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }

      
      return resizedImage;


  }//resize image

}
