FileDocCategorySizeDatePackage
GraphicImportCompositing.javaAPI DocExample5302Wed Nov 10 12:38:06 GMT 2004com.oreilly.qtjnotebook.ch05

GraphicImportCompositing

public class GraphicImportCompositing extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        try {
            QTSessionCheck.check();

            File graphicsDir = new File ("graphics");
            QTFile pngFile1 = new QTFile (new File (graphicsDir, "1.png"));
            QTFile pngFile2 = new QTFile (new File (graphicsDir, "2.png"));
            GraphicsImporter gi1 = new GraphicsImporter (pngFile1);
            GraphicsImporter gi2 = new GraphicsImporter (pngFile2);
            
            // define some matrix transforms on importer 1
            QDRect bounds = gi1.getBoundsRect();
            // combine translation (movement) and scaling into
            // one call to rect
            QDRect newBounds =
                new QDRect (bounds.getWidth()/4,
                            bounds.getHeight()/4,
                            bounds.getWidth()/2,
                            bounds.getHeight()/2);
            Matrix matrix1 = new Matrix();
            matrix1.rect(bounds, newBounds);
            // rotate about its center
            Matrix matrix2 = new Matrix();
            matrix1.rotate (30,
                           (bounds.getWidth() - bounds.getX())/2,
                           (bounds.getHeight() - bounds.getY())/2);
            // matrix1.concat (matrix2);
            gi1.setMatrix (matrix1);


            // draw somewhere
            QDGraphics scratchWorld = new QDGraphics (gi2.getBoundsRect());
            System.out.println ("Scratch world: " + scratchWorld);
            // draw background
            gi2.setGWorld (scratchWorld, null);
            gi2.draw();
            // draw foreground
            GraphicsMode alphaMode =
                new GraphicsMode (QDConstants.transparent,
                                  QDColor.green);
            gi1.setGraphicsMode (alphaMode);
            gi1.setGWorld (scratchWorld, null);
            gi1.draw();

            int bufSize =
                QTImage.getMaxCompressionSize (scratchWorld, 
                                               scratchWorld.getBounds(),
                                               0,
                                               StdQTConstants.codecNormalQuality,
                                               StdQTConstants4.kPNGCodecType,
                                               CodecComponent.anyCodec);
            byte[] compBytes = new byte[bufSize];
            RawEncodedImage compImg = new RawEncodedImage (compBytes);
            ImageDescription id = QTImage.compress(scratchWorld,
                                                   scratchWorld.getBounds(),
                                                   StdQTConstants.codecNormalQuality,
                                                   StdQTConstants4.kPNGCodecType,
                                                   compImg);
            System.out.println ("rei compressed from gw is " +
                                compImg.getSize());

            /* ok but kludgy
            try {
                FileOutputStream out = new FileOutputStream ("composite.png");
                byte compImgBytes[] = compImg.getBytes();
                out.write(compImgBytes, 0, compImgBytes.length);
                out.flush();
                out.close();
            } catch (java.io.IOException ioe) {
                ioe.printStackTrace();
            }
            */

            System.out.println ("exporting");
            GraphicsExporter exporter =
                new GraphicsExporter (StdQTConstants4.kQTFileTypePNG);
            exporter.setInputPtr (compImg, id);
            QTFile outFile = new QTFile (new File ("composite.png"));
            exporter.setOutputFile (outFile);
            exporter.doExport();
            System.out.println ("did export");
            

        } catch (QTException qte) {
            qte.printStackTrace();
        }
        System.exit(0);