001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.html.image.resource;
018
019import java.awt.image.BufferedImage;
020
021import org.apache.wicket.request.resource.DynamicImageResource;
022
023/**
024 * A DynamicImageResource subclass that holds a BufferedImage generated by code elsewhere. The image
025 * is held in a non-transient field, and so if this resource is clustered, the entire image will be
026 * serialized and copied. If you can regenerate your image by drawing on a Graphics2D, you should
027 * prefer the RenderedDynamicImageResource class instead since its image data is transient and
028 * therefore it is very lightweight when clustered.
029 * <p>
030 * The format of the image (and therefore the resource's extension) can be specified with
031 * setFormat(String). The default format is "PNG" because JPEG is lossy and makes generated images
032 * look bad and GIF has patent issues.
033 * 
034 * @see org.apache.wicket.markup.html.image.resource.RenderedDynamicImageResource
035 * @author Jonathan Locke
036 */
037public class BufferedDynamicImageResource extends DynamicImageResource
038{
039        private static final long serialVersionUID = 1L;
040
041        /** The byte array holding the contents of the dynamic image */
042        private byte[] imageData;
043
044        /**
045         * Construct.
046         */
047        public BufferedDynamicImageResource()
048        {
049        }
050
051        /**
052         * Construct.
053         * 
054         * @param format
055         */
056        public BufferedDynamicImageResource(String format)
057        {
058                super(format);
059        }
060
061        /**
062         * @param image
063         *            The image to set
064         */
065        public synchronized void setImage(final BufferedImage image)
066        {
067                imageData = toImageData(image);
068        }
069
070        @Override
071        protected byte[] getImageData(Attributes attributes)
072        {
073                return imageData;
074        }
075}