2017-02-01 8 views
-1

그래서 ppm 파일에 더 기록 될 ppm 이미지로 직사각형 이미지 (아래 링크 참조)를 만드는 java 프로그램에서 작업하고 있습니다. 만들고 이미지를 파일에 쓰기. 그러나 이미지를 동적으로 생성하는 데 어려움을 겪고 있으므로 지정된 폭 및 높이로 작동합니다. 필자의 이해로, p3 ppm 파일은 4x4 이미지에 대해 다음 형식을 따르기 만합니다.파일에 쓸 PPM 이미지 만들기 Java

P3 
4 4 
15 
0 0 0 0 0 0 0 0 0 15 0 15 
0 0 0 0 15 7 0 0 0 0 0 0 
0 0 0 0 0 0 0 15 7 0 0 0 
15 0 15 0 0 0 0 0 0 0 0 0 

처음 세 숫자 표제 어디 나머지는 단순히 각 화소의 RGB 값이다. 그러나 아래 이미지와 직선의 단색을 포함하지 않는 모든 치수에 대해 위의 행렬을 어떻게 만들 수 있는지 알아 내는데 문제가 있습니까?

이미지가 생성된다 :

enter image description here

내가 목록의 각 인덱스로 설정된 다음 RGB이어서 하나 개 RGB 세트가되도록 RGB 값의 배열을 유지하는의 ArrayList를 만들 수 상상

권리. 그러나 나는 rgb 값이 무엇인지 혼란 스럽다. 여기에 내가 무엇을 가지고 있습니다 :

public static void createImage(int width, int height){ 
     pic = new ArrayList(); 
     int[] rgb = new int[3]; 

     for(int i = 0; i <= width; i++){ 
      for(int j = 0; i <= height; j++){ 
       rgb[0] = 255-j; //random values as im not sure what they should be or how to calculate them    
       rgb[1] = 0+j; 
       rgb[1] = 0+j; 
       pic.add(rgb); 
      } 
     } 
    } 

미리 감사드립니다. 편집을 할


: : 나는 대부분의 문제를 해결하기 위해 코드 관리가 업데이트 그러나 이미지는 위의 게시와 일치하지 않습니다 생성. 이 코드로.

enter image description here

package ppm; 

    import java.awt.Color; 
    import java.awt.image.BufferedImage; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.util.ArrayList; 


    public class PPM { 

    private BufferedImage img; 
    private static final String imageDir = "Image/rect.ppm"; 
    private final static String filename = "assignment1_q1.ppm"; 

    private static byte bytes[]=null;  // bytes which make up binary PPM image 
    private static double doubles[] = null; 
    private static int height = 0; 
    private static int width = 0; 
    private static ArrayList pic; 
    private static String matrix=""; 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws IOException { 
     createImage(200, 200); 
     writeImage(filename); 
    } 


    public static void createImage(int width, int height){ 
     pic = new ArrayList(); 
     int[] rgb = new int[3]; 
     matrix +="P3\n" + width + "\n" + height + "\n255\n"; 
     for(int i = 0; i <= height; i++){ 
      for(int j = 0; j <= width; j++){ 
       Color c = getColor(width, height, j, i); 
       //System.out.println(c); 
       if(c==Color.red){ 
         rgb[0] = (int) (255*factor(width, height, j, i)); 
         rgb[1] = 0; 
         rgb[2] = 0; 
       }else if(c==Color.green){ 
         rgb[0] = 0; 
         rgb[1] = (int) (255*factor(width, height, j, i)); 
         rgb[2] = 0; 
       }else if(c==Color.blue){ 
         rgb[0] = 0; 
         rgb[1] = 0; 
         rgb[2] = (int) (255*factor(width, height, j, i)); 
       }else if(c== Color.white){ 
         rgb[0] = (int) (255*factor(width, height, j, i)); 
         rgb[1] = (int) (255*factor(width, height, j, i)); 
         rgb[2] = (int) (255*factor(width, height, j, i)); 
       } 
       matrix += ""+ rgb[0] + " " + rgb[1] + " " + rgb[2] + " " ; 
       //System.out.println(""+ rgb[0] + " " + rgb[1] + " " + rgb[2] + " "); 
       //pic.add(rgb); 
      } 
      matrix += "\n"; 
     } 
    } 

    public static Color getColor(int width, int height, int a, int b){ 
     double d1 = ((double) width/height) * a; 
     double d2 = (((double) -width/height) * a + height); 

     if(d1 > b && d2 > b) return Color.green; 
     if(d1 > b && d2 < b) return Color.blue; 
     if(d1 < b && d2 > b) return Color.red; 
     return Color.white; 
    } 

    public static double factor(int width, int height, int a, int b){ 
     double factorX = (double) Math.min(a, width - a)/width * 2; 
     double factorY = (double) Math.min(b, height - b)/height * 2; 

     //System.out.println(Math.min(factorX, factorY)); 

     return Math.min(factorX, factorY); 
    } 

    public static void writeImage(String fn) throws FileNotFoundException, IOException { 

     //if (pic != null) { 

       FileOutputStream fos = new FileOutputStream(fn); 
       fos.write(new String(matrix).getBytes()); 

       //fos.write(data.length); 
       //System.out.println(data.length); 
       fos.close(); 
     // } 
    } 
} 

답변

0

당신은 그림에서 대각선을 모델링 Linear functions을 사용할 수 있습니다 : 나는 다음과 같은 이미지를 얻을. 좌표의 (0, 0)은 이미지의 왼쪽 상단 모서리에 있습니다.

는 치수 widthheight에서 대각선으로 이미지를 생성 할 말 왼쪽 상단 점 (0, 0)(width, height) 교차 것이다 하단 오른쪽 :

y = ax + t 

0  = a *  0 + t => t = 0 
height = a * width + 0 => a = height/width 

d1(x) = (height/width) * x 

이제 우리는 함수를 계산할 수 있습니다 두 번째 대각선에 대해서. 이 대각선 때문에, 점 (0, height) 및 통과 :이에서

y = ax + t 

height = a *  0 + t  => t = height 
0  = a * width + height => a = -(height/width) 

d2(x) = -(height/width) * x + height 

우리는 이미지의 특정 지점이 아래 또는 대각선 위에있는 여부를 결정할 수 있습니다.

  • if d1(a) > b : 점 (a, b) 대한 예로서 (a, b)는 제 대각선 (오른쪽 하단 좌측 상단) 위에있는, 따라서, 청색 또는 녹색이어야한다. 그렇지 않으면 빨간색 또는 흰색 중 하나 여야합니다.

  • if d2(a) > b : (a, b)는 두 번째 대각선 위에 있으므로 빨간색 또는 녹색이어야합니다.

    Color getColor(int width, int height, int a, int b){ 
        double d1 = ((double) height/width) * a; 
        double d2 = ((double) -height/width) * a + height; 
    
        if(d1 > b && d2 > b) return greenColor; 
        if(d1 > b && d2 < b) return blueColor; 
        if(d1 < b && d2 > b) return redColor; 
        return whiteColor; 
    } 
    

    을 이제 우리가 취할 필요가 마지막으로 한 일이있다 : 그렇지 않으면 그것은 네 가지 색상의 특정 지점이 속한 결정하기 쉽고 모두 관계를 적용하여 흰색 또는

블루해야합니다 이미지는 국경으로 어두워집니다.

어두운 버전의 색상은 각 채널에 요인을 곱하여 생성 할 수 있습니다. 요인이 낮을수록 색상이 어둡게됩니다. 간단히하기 위해 밝기의 변화가 이미지의 중심에서 선형이라고 가정합니다.

밝기가 2 개의 축을 따라 독립적으로 변경되므로 두 축의 변화를 계산하고 최대를 사용하여이를 모델링해야합니다.

중심까지의 거리의 함수로서 밝기 변화는 (하나의 축 상)의 중심까지의 거리와 관련하여 이미지의 가까운 경계까지의 거리를 이용하여 모델링 될 수있다 :

deltaX = min(a, width - a)/(width/2) 
deltaY = min(b, height - b)/(height/2) 

그래서 우리는 다음과 같은 방법으로 각 색상 채널을 배가시키는 요소를 얻을 수 있습니다.

double factor(int width, int height, int a, int b){ 
    double factorX = (double) Math.min(a, width - a)/width * 2; 
    double factorY = (double) Math.min(b, height - b)/height * 2; 

    return Math.min(factorX, factorY); 
}