/**
 * Copyright (c) 2014 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 */

package cafe.image.png;

import cafe.util.Builder;

/**
 * PNG IHDR chunk builder
 *  
 * @author Wen Yu, yuwen_66@yahoo.com
 * @version 1.0 04/25/2013
 */
public class IHDRBuilder extends ChunkBuilder implements Builder<Chunk> {
	/*
	 * Color   Allowed    Interpretation
   	 * FieldType    Bit Depths
   	 *
   	 *	0      1,2,4,8,16  Each pixel is a grayscale sample.
     *
   	 *	2      8,16        Each pixel is an R,G,B triple.
     *
   	 *	3      1,2,4,8     Each pixel is a palette index;
     *                     a PLTE chunk must appear.
     *
   	 *	4      8,16        Each pixel is a grayscale sample,
     *                     followed by an alpha sample.
     *
   	 *	6      8,16        Each pixel is an R,G,B triple,
     *                     followed by an alpha sample.
	 */
	private int width = 0;
	private int height = 0;
	private int bitDepth = 0;
	private int colorType = 0;
	private int compressionMethod = 0;
	private int filterMethod = 0;
	private int interlaceMethod = 0;

	public IHDRBuilder width(int width) {
		if (width <= 0) throw new IllegalArgumentException("Invalid width: " + width);
		this.width = width;
		return this;
	}
	
	public IHDRBuilder height(int height) {
		if (height <= 0) throw new IllegalArgumentException("Invalid height: " + height);
		this.height = height;
		return this;
	}
	
	public IHDRBuilder bitDepth(int bitDepth) {
		switch(bitDepth) {
			case 1:
			case 2:
			case 4:
			case 8:
			case 16:
				this.bitDepth = bitDepth;
				return this;
			default:
				throw new IllegalArgumentException("Invalid bitDepth: " + bitDepth);
		}		
	}
	
	public IHDRBuilder colorType(int colorType) {
		switch(colorType) {
			case 0:
			case 2:
			case 3:
			case 4:
			case 6:
				this.colorType = colorType;
				return this;
			default:
				throw new IllegalArgumentException("Invalid colorType: " + colorType);
		}		
	}
	
	// Only compression method 0 => deflate/inflate is allowed.
	public IHDRBuilder compressionMethod(int compressionMethod) {
		if (compressionMethod != 0) throw new IllegalArgumentException("Invalid comressionMethod" + compressionMethod);
		this.compressionMethod = compressionMethod;
		return this;
	}
	
	// Only filter method 0 (adaptive filtering with five basic filter types) is defined.
	public IHDRBuilder filterMethod(int filterMethod) {
		if(filterMethod != 0) throw new IllegalArgumentException("Invalid filterMethod: " + filterMethod);
		this.filterMethod = filterMethod;
		return this;
	}
	
	// 0 => no interlace; 1 => Adam7 interlace
	public IHDRBuilder interlaceMethod(int interlaceMethod) {
		if ((interlaceMethod != 0) && (interlaceMethod != 1) )throw new IllegalArgumentException("Invalid interlaceMethod" + interlaceMethod);
		this.interlaceMethod = interlaceMethod;
		return this;
	}
	
	public IHDRBuilder() {
		super(ChunkType.IHDR);		
	}	

	@Override
	protected byte[] buildData() {
		// 13 bytes
		byte[] data = {(byte)(width >>> 24),
		         (byte)(width >>> 16), (byte)(width >>> 8),
		         (byte)width, (byte)(height >>> 24),
		         (byte)(height >>> 16), (byte)(height >>> 8),
		         (byte)height, (byte)bitDepth, (byte)colorType, (byte)compressionMethod,
		         (byte)filterMethod, (byte)interlaceMethod };
		
		return data;
	}
}

@@ -613,16 +613,21 @@
 owed    
+     
 Interpre
@@ -644,13 +644,8 @@
 %09 * 
-Field
 Type
@@ -686,14 +686,18 @@
   1,
+ 
 2,
+ 
 4,
+ 
 8,
+ 
 16  
@@ -745,32 +745,33 @@
    %09 *%092      8,
+ 
 16        Each p
@@ -756,32 +756,35 @@
    8, 16        
+ 
+  
 Each pixel is an
@@ -825,18 +825,22 @@
   1,
+ 
 2,
+ 
 4,
+ 
 8     
+ 
 Each
@@ -868,24 +868,28 @@
 ndex;%0A     *
+    
             
@@ -938,32 +938,33 @@
    %09 *%094      8,
+ 
 16        Each p
@@ -949,32 +949,35 @@
    8, 16        
+   
 Each pixel is a 
@@ -1013,32 +1013,36 @@
                 
+  
+  
 followed by an a
@@ -1077,16 +1077,17 @@
       8,
+ 
 16      
@@ -1080,32 +1080,35 @@
    8, 16        
+  
+ 
 Each pixel is an
@@ -1120,32 +1120,36 @@
 B triple,%0A     *
+    
                 

Update comments