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
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.
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 ***** */
40 #include "gfxImageSurface.h"
44 gfxImageSurface::gfxImageSurface(unsigned char *aData
, const gfxIntSize
& aSize
,
45 long aStride
, gfxImageFormat aFormat
)
52 if (!CheckSurfaceSize(aSize
))
55 cairo_surface_t
*surface
=
56 cairo_image_surface_create_for_data((unsigned char*)mData
,
57 (cairo_format_t
)mFormat
,
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
70 gfxImageSurface::gfxImageSurface(const gfxIntSize
& size
, gfxImageFormat format
) :
71 mSize(size
), mOwnsData(PR_FALSE
), mFormat(format
)
73 mStride
= ComputeStride();
75 if (!CheckSurfaceSize(size
))
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
);
89 cairo_surface_t
*surface
=
90 cairo_image_surface_create_for_data((unsigned char*)mData
,
91 (cairo_format_t
)format
,
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()
117 gfxImageSurface::ComputeStride() const
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;
130 NS_WARNING("Unknown format specified to gfxImageSurface!");
131 stride
= mSize
.width
* 4;
134 stride
= ((stride
+ 3) / 4) * 4;
140 gfxImageSurface::CopyFrom(gfxImageSurface
*other
)
142 if (other
->mSize
!= mSize
)
147 if (other
->mFormat
!= mFormat
&&
148 !(other
->mFormat
== ImageFormatARGB32
&& mFormat
== ImageFormatRGB24
) &&
149 !(other
->mFormat
== ImageFormatRGB24
&& mFormat
== ImageFormatARGB32
))
154 if (other
->mStride
== mStride
) {
155 memcpy (mData
, other
->mData
, mStride
* mSize
.height
);
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
);