 /**   Resource files.
 *  All resource are store into resource files.   Resource files are created during build time, and packed all resources of same type
 *  into one file.  All resource files have a common header:
 *  <nbr of resource><offset of resource 1><offset of resource 2><offset of resource 3><offset of resource 4>....   
 *  Each header elts is 4 byte longs (int), so the header size is ((nbr of resource)+1)*4 
 *  
 *  Resource ID.
 *  Each resource is identified by an ID (and no longer by a string), which is the position of the resource in the file,
 *  and then the position of the resource in the array.   All Ids are static final member of the TextRscId class, which is generated 
 *  during the build.   For ex the RscId class will look like that:   
 *  
 *  public final class RscId
 *  {
 *      public final short Im_mainViewBackground = 0 ;
 *      public final short Im_logo = 1 ;
 *      public final short Im_sbBackgroundImage = 2 ;
 *      ...
 *  }
 *  
 *  it could be generate by the build from such resource files:
 *  image.scr320_200.resource
 *  mainViewBackground="view_background.png"
 *  logo="logo.png"
 *  sbBackgroundImage="softbar_background.png"
 *  
 *  Each resource file name is coded as the following: <type>.<name>.resource   The build shall automatically process any .resource files.
 */

import java.io.IOException;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.File;
import org.apache.tools.ant.BuildException;

public class ImgToBinary extends ResourceToBinary
{
    public ImgToBinary()
    {
        super();
        CLASS_NAME = "public final class ImageRscId";
        PREFIX = "IM_";
    }

    public void execute() throws BuildException
    {
        ImgToBinary itb = new ImgToBinary();
        itb.setFileName(fileName);
        itb.setJavaFileName(javaFileName);
        itb.setTodir(toDir) ;
        
        try
        {
            itb.loadText();
            itb.writeToBinary();
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }

    }
    
    // get the resource folder path
    private String getRscPath(String path)
    {
        int lastSlashIndex = path.lastIndexOf("/");
        return path.substring(0, lastSlashIndex+1);
    }

    protected void writeBinary(DataOutputStream dos) throws IOException
    {
        // write number of resources
        dos.writeInt(values.size());

        String rscPath = getRscPath(fileName);
        int offset = 0;
        // write the offset of resources one by one
        for (int i = 0; i < values.size(); i++)
        {
            dos.writeInt(offset);
            String value = (String) this.values.get(i);
            value.trim();
            String imgName = rscPath + value;
            FileInputStream is = new FileInputStream(imgName);
            offset += is.available();
            is.close();
        }

        // get image binary data and write into dos
        byte[] imgData;
        for (int i = 0; i < values.size(); i++)
        {
            String value = (String) this.values.get(i);
            String imgName = rscPath + value;
            value.trim();
            File file = new File(imgName);
            FileInputStream is = new FileInputStream(file);
            imgData = new byte[is.available()];
            is.read(imgData);
            dos.write(imgData, 0, imgData.length);
            is.close();
        }
    }

}
