Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / gfx / thebes / src / gfxImageSurface.cpp
blobee45e6825c90a50282fab97c487585af25ffe4ed
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Oracle Corporation code.
17 * The Initial Developer of the Original Code is Oracle Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Stuart Parmenter <pavlov@pavlov.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "prmem.h"
40 #include "gfxImageSurface.h"
42 #include "cairo.h"
44 gfxImageSurface::gfxImageSurface(unsigned char *aData, const gfxIntSize& aSize,
45 long aStride, gfxImageFormat aFormat)
46 : mSize(aSize)
47 , mOwnsData(PR_FALSE)
48 , mData(aData)
49 , mFormat(aFormat)
50 , mStride(aStride)
52 if (!CheckSurfaceSize(aSize))
53 return;
55 cairo_surface_t *surface =
56 cairo_image_surface_create_for_data((unsigned char*)mData,
57 (cairo_format_t)mFormat,
58 mSize.width,
59 mSize.height,
60 mStride);
62 // cairo_image_surface_create_for_data can return a 'null' surface
63 // in out of memory conditions. The gfxASurface::Init call checks
64 // the surface it receives to see if there is an error with the
65 // surface and handles it appropriately. That is why there is
66 // no check here.
67 Init(surface);
70 gfxImageSurface::gfxImageSurface(const gfxIntSize& size, gfxImageFormat format) :
71 mSize(size), mOwnsData(PR_FALSE), mFormat(format)
73 mStride = ComputeStride();
75 if (!CheckSurfaceSize(size))
76 return;
78 // if we have a zero-sized surface, just set mData to nsnull
79 if (mSize.height * mStride > 0) {
80 mData = (unsigned char *) calloc(mSize.height, mStride);
81 if (!mData)
82 return;
83 } else {
84 mData = nsnull;
87 mOwnsData = PR_TRUE;
89 cairo_surface_t *surface =
90 cairo_image_surface_create_for_data((unsigned char*)mData,
91 (cairo_format_t)format,
92 mSize.width,
93 mSize.height,
94 mStride);
95 Init(surface);
98 gfxImageSurface::gfxImageSurface(cairo_surface_t *csurf)
100 mSize.width = cairo_image_surface_get_width(csurf);
101 mSize.height = cairo_image_surface_get_height(csurf);
102 mData = cairo_image_surface_get_data(csurf);
103 mFormat = (gfxImageFormat) cairo_image_surface_get_format(csurf);
104 mOwnsData = PR_FALSE;
105 mStride = cairo_image_surface_get_stride(csurf);
107 Init(csurf, PR_TRUE);
110 gfxImageSurface::~gfxImageSurface()
112 if (mOwnsData)
113 free(mData);
116 long
117 gfxImageSurface::ComputeStride() const
119 long stride;
121 if (mFormat == ImageFormatARGB32)
122 stride = mSize.width * 4;
123 else if (mFormat == ImageFormatRGB24)
124 stride = mSize.width * 4;
125 else if (mFormat == ImageFormatA8)
126 stride = mSize.width;
127 else if (mFormat == ImageFormatA1) {
128 stride = (mSize.width + 7) / 8;
129 } else {
130 NS_WARNING("Unknown format specified to gfxImageSurface!");
131 stride = mSize.width * 4;
134 stride = ((stride + 3) / 4) * 4;
136 return stride;
139 PRBool
140 gfxImageSurface::CopyFrom(gfxImageSurface *other)
142 if (other->mSize != mSize)
144 return PR_FALSE;
147 if (other->mFormat != mFormat &&
148 !(other->mFormat == ImageFormatARGB32 && mFormat == ImageFormatRGB24) &&
149 !(other->mFormat == ImageFormatRGB24 && mFormat == ImageFormatARGB32))
151 return PR_FALSE;
154 if (other->mStride == mStride) {
155 memcpy (mData, other->mData, mStride * mSize.height);
156 } else {
157 int lineSize = PR_MIN(other->mStride, mStride);
158 for (int i = 0; i < mSize.height; i++) {
159 unsigned char *src = other->mData + other->mStride * i;
160 unsigned char *dst = mData + mStride * i;
162 memcpy (dst, src, lineSize);
166 return PR_TRUE;