001 /* 002 * PromotionGray8 003 * 004 * Copyright (c) 2001, 2002, 2003 Marco Schmidt. 005 * All rights reserved. 006 */ 007 008 package net.sourceforge.jiu.color.promotion; 009 010 import net.sourceforge.jiu.data.BilevelImage; 011 import net.sourceforge.jiu.data.Gray8Image; 012 import net.sourceforge.jiu.data.MemoryGray8Image; 013 import net.sourceforge.jiu.data.PixelImage; 014 import net.sourceforge.jiu.ops.ImageToImageOperation; 015 import net.sourceforge.jiu.ops.MissingParameterException; 016 import net.sourceforge.jiu.ops.WrongParameterException; 017 018 /** 019 * Converts BilevelImage objects to Gray8Image objects. 020 * Promotion is a lossless operation that will lead to an output image 021 * that holds the same image in a way that demands more memory. 022 * 023 * @author Marco Schmidt 024 * @since 0.8.0 025 */ 026 public class PromotionGray8 extends ImageToImageOperation 027 { 028 private void prepare(PixelImage in) throws 029 MissingParameterException, 030 WrongParameterException 031 { 032 if (in == null) 033 { 034 throw new MissingParameterException("Missing input image."); 035 } 036 if (! 037 ( 038 (in instanceof BilevelImage) 039 ) 040 ) 041 { 042 throw new WrongParameterException("Unsupported input image type: " + in.getClass().getName()); 043 } 044 PixelImage out = getOutputImage(); 045 if (out == null) 046 { 047 setOutputImage(new MemoryGray8Image(in.getWidth(), in.getHeight())); 048 } 049 else 050 { 051 if (!(out instanceof Gray8Image)) 052 { 053 throw new WrongParameterException("Specified output image type must be of class Gray8Image; got " + in.getClass().getName()); 054 } 055 if (in.getWidth() != out.getWidth()) 056 { 057 throw new WrongParameterException("Specified output image must have same width as input image."); 058 } 059 if (in.getHeight() != out.getHeight()) 060 { 061 throw new WrongParameterException("Specified output image must have same height as input image."); 062 } 063 } 064 } 065 066 private void process(BilevelImage in, Gray8Image out) 067 { 068 final int WIDTH = in.getWidth(); 069 final int HEIGHT = in.getHeight(); 070 final byte MAX = (byte)255; 071 final byte MIN = (byte)0; 072 for (int y = 0; y < HEIGHT; y++) 073 { 074 for (int x = 0; x < WIDTH; x++) 075 { 076 if (in.isBlack(x, y)) 077 { 078 out.putByteSample(0, x, y, MIN); 079 } 080 else 081 { 082 out.putByteSample(0, x, y, MAX); 083 } 084 } 085 setProgress(y, HEIGHT); 086 } 087 } 088 089 public void process() throws 090 MissingParameterException, 091 WrongParameterException 092 { 093 PixelImage in = getInputImage(); 094 prepare(in); 095 Gray8Image out = (Gray8Image)getOutputImage(); 096 if (in instanceof BilevelImage) 097 { 098 process((BilevelImage)in, out); 099 } 100 } 101 }