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 

最初の3つの数字は見出しであり、残りは単に各ピクセルのrgb値です。しかし、私は下の画像と一直線上に無地の色が含まれていないため、指定された任意の次元に対して、上記の行列をどのように作成できるかを調べるのに問題がありますか?作成する

イメージ:

enter image description here

私は、リスト内の各インデックスは、に設定され、次のRGB続く1つの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); 
      } 
     } 
    } 

ありがとうございます。


EDITED::更新されたコード は、私が問題のほとんどを解決するために管理している、しかし、生成された画像は、上記の投稿1と一致していません。このコードで。私は、次の画像を取得する:

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)は画像の左上隅にあることに注意してください!

がトップから左へと斜めの底は、右のポイント(0, 0)(width, height)を渡る​​だろう、あなたは寸法widthheightで画像を作成したいと言う:

y = ax + t 

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

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

今、私たちは関数を計算することができます第2の対角線に対して。この対角線はそう、ポイント(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)は第1の斜め(右下に左上)上にある、従ってそれは、青色または緑色のいずれかでなければなりません。それ以外の場合は、赤または白のいずれかでなければなりません。

  • if d2(a) > b:(a、b)は2番目の対角線の上にあります。

    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; 
    } 
    

    を今私たちが取る必要がある最後の事があります。そうしないと、それは4色の特定のポイントが属するかを判断するのは簡単です、両方の関係を適用することにより、白や

青でなければなりません画像は境界線に向かって暗くなります。

各チャネルに係数を乗算することで、より暗いバージョンの色を作成することができます。係数が小さいほど、結果として生じる色が暗くなります。わかりやすくするために、明るさの変化が画像の中心から線形であると仮定します。

輝度は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); 
} 
関連する問題