/*
 *  BasicImage.java
 *  Cookbook
 *
 *  Copyright (c) 2001-2009 Flagstone Software Ltd. All rights reserved.
 *
 *  This code is distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 *  EXPRESS OR IMPLIED, AND Flagstone HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
 *  WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 *  PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 */

import com.flagstone.transform.*;
import com.flagstone.transform.util.*;

import java.io.IOException;
import java.util.zip.DataFormatException;

/*
 * This example shows how image can be displayed using the FSImageConstructor.
 *
 * To run this example, type the following on a command line:
 *
 *     java -cp cookbook.jar BasicImage image-file file-out
 
 * where
 *
 *     image-file is the path to a file containing either BMP, PNG or JPEG image.
 *
 *     file-out is the path where the file will be written. If no output file 
 *     is specified then a file named after the example will be written to the 
 *     current directory.
 */
public class BasicImage
{
    public static void main(String[] args)
    {
        try
        {
            String out = args.length == 1 ? "BasicImage.swf" : args[1];
            BasicImage example = new BasicImage();         
            FSMovie movie = new FSMovie();            
            example.createMovie(movie, args[0]);
            movie.encodeToFile(out);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public void createMovie(FSMovie movie, String imageFile) throws DataFormatException, IOException
    {
        FSImageConstructor imageGenerator = new FSImageConstructor(imageFile);  

        int imageId = movie.newIdentifier();
        int shapeId = movie.newIdentifier();
        
        int xOrigin = imageGenerator.getWidth()/2;
        int yOrigin = imageGenerator.getHeight()/2;
        
        FSDefineObject image = imageGenerator.defineImage(imageId);
        
        FSDefineShape3 shape = imageGenerator.defineEnclosingShape(shapeId, imageId, 
            -xOrigin, -yOrigin, new FSSolidLine(20, FSColorTable.black()));
        
        movie.setFrameRate(1.0f);
        movie.setFrameSize(shape.getBounds());
        movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
        movie.add(image);
        movie.add(shape);
        movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
        movie.add(new FSShowFrame());
    }
}