2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2008, 2009 Google, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "platform/image-decoders/ImageDecoder.h"
32 ImageFrame::ImageFrame()
35 , m_status(FrameEmpty)
37 , m_disposalMethod(DisposeNotSpecified)
38 , m_alphaBlendSource(BlendAtopPreviousFrame)
39 , m_premultiplyAlpha(true)
40 , m_pixelsChanged(false)
41 , m_requiredPreviousFrameIndex(kNotFound)
43 , m_requiredPreviousFrameIndexValid(false)
48 ImageFrame& ImageFrame::operator=(const ImageFrame& other)
53 m_bitmap = other.m_bitmap;
54 // Keep the pixels locked since we will be writing directly into the
55 // bitmap throughout this object's lifetime.
56 m_bitmap.lockPixels();
57 // Be sure to assign this before calling setStatus(), since setStatus() may
58 // call notifyBitmapIfPixelsChanged().
59 m_pixelsChanged = other.m_pixelsChanged;
60 setMemoryAllocator(other.allocator());
61 setOriginalFrameRect(other.originalFrameRect());
62 setStatus(other.status());
63 setDuration(other.duration());
64 setDisposalMethod(other.disposalMethod());
65 setAlphaBlendSource(other.alphaBlendSource());
66 setPremultiplyAlpha(other.premultiplyAlpha());
67 // Be sure that this is called after we've called setStatus(), since we
68 // look at our status to know what to do with the alpha value.
69 setHasAlpha(other.hasAlpha());
70 // Copy raw fields to avoid ASSERT failure in requiredPreviousFrameIndex().
71 m_requiredPreviousFrameIndex = other.m_requiredPreviousFrameIndex;
73 m_requiredPreviousFrameIndexValid = other.m_requiredPreviousFrameIndexValid;
78 void ImageFrame::clearPixelData()
81 m_status = FrameEmpty;
82 // NOTE: Do not reset other members here; clearFrameBufferCache()
83 // calls this to free the bitmap data, but other functions like
84 // initFrameBuffer() and frameComplete() may still need to read
85 // other metadata out of this frame later.
88 void ImageFrame::zeroFillPixelData()
90 m_bitmap.eraseARGB(0, 0, 0, 0);
94 bool ImageFrame::copyBitmapData(const ImageFrame& other)
99 m_hasAlpha = other.m_hasAlpha;
101 return other.m_bitmap.copyTo(&m_bitmap, other.m_bitmap.colorType());
104 bool ImageFrame::setSize(int newWidth, int newHeight)
106 // setSize() should only be called once, it leaks memory otherwise.
107 ASSERT(!width() && !height());
109 m_bitmap.setInfo(SkImageInfo::MakeN32Premul(newWidth, newHeight));
110 if (!m_bitmap.allocPixels(m_allocator, 0))
117 PassRefPtr<NativeImageSkia> ImageFrame::asNewNativeImage() const
119 return NativeImageSkia::create(m_bitmap);
122 bool ImageFrame::hasAlpha() const
127 void ImageFrame::setHasAlpha(bool alpha)
131 // If the frame is not fully loaded, there will be transparent pixels,
132 // so we can't tell skia we're opaque, even for image types that logically
133 // always are (e.g. jpeg).
134 if (m_status != FrameComplete)
136 m_bitmap.setAlphaType(alpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
139 void ImageFrame::setStatus(Status status)
142 if (m_status == FrameComplete) {
143 m_bitmap.setAlphaType(m_hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType);
144 // Send pending pixels changed notifications now, because we can't do this after
145 // the bitmap has been marked immutable.
146 notifyBitmapIfPixelsChanged();
147 m_bitmap.setImmutable(); // Tell the bitmap it's done.
151 void ImageFrame::zeroFillFrameRect(const IntRect& rect)
156 m_bitmap.eraseArea(rect, SkColorSetARGB(0, 0, 0, 0));