DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * mozilla.org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Stuart Parmenter <pavlov@pavlov.net>
 *   Vladimir Vukicevic <vladimir@pobox.com>
 *   Joe Hewitt <hewitt@netscape.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsMemory.h"
#include "nsCairoImage.h"
#include "nsCairoRenderingContext.h"
#include "nsCairoDrawingSurface.h"

#include "nsIServiceManager.h"

static void ARGBToThreeChannel(PRUint32* aARGB, PRUint8* aData) {
    PRUint8 r = (PRUint8)(*aARGB >> 16);
    PRUint8 g = (PRUint8)(*aARGB >> 8);
    PRUint8 b = (PRUint8)*aARGB;
#if defined(XP_WIN) || defined(XP_OS2) || defined(XP_BEOS) || defined(MOZ_WIDGET_PHOTON)
    // BGR format; assume little-endian system
#ifndef IS_LITTLE_ENDIAN
#error Strange big-endian/OS combination
#endif
    // BGR, blue byte first
    aData[0] = b; aData[1] = g; aData[2] = r;
#else
    // RGB, red byte first
    aData[0] = r; aData[1] = g; aData[2] = b;
#endif
}

static PRUint32 ThreeChannelToARGB(PRUint8* aData, PRUint8 aAlpha) {
    PRUint8 r, g, b;
#if defined(XP_WIN) || defined(XP_OS2) || defined(XP_BEOS) || defined(MOZ_WIDGET_PHOTON)
    // BGR format; assume little-endian system
#ifndef IS_LITTLE_ENDIAN
#error Strange big-endian/OS combination
#endif
    // BGR, blue byte first
    b = aData[0]; g = aData[1]; r = aData[2];
#else
    // RGB, red byte first
    r = aData[0]; g = aData[1]; b = aData[2];
#endif
    return (aAlpha << 24) | (r << 16) | (g << 8) | b;
}

nsCairoImage::nsCairoImage()
 : mWidth(0),
   mHeight(0),
   mDecoded(0,0,0,0),
   mImageSurface(nsnull),
   mImageSurfaceBuf(nsnull),
   mImageSurfaceData(nsnull),
   mImageSurfaceAlpha(nsnull),
   mAlphaDepth(0),
   mHadAnyData(PR_FALSE)
{
}

nsCairoImage::~nsCairoImage()
{
    if (mImageSurface)
        cairo_surface_destroy(mImageSurface);
    // XXXX - totally not safe. mImageSurface should own data,
    // but it doesn't.
    if (mImageSurfaceBuf)
        nsMemory::Free(mImageSurfaceBuf);
    if (mImageSurfaceData)
        nsMemory::Free(mImageSurfaceData);
    if (mImageSurfaceAlpha)
        nsMemory::Free(mImageSurfaceAlpha);
}

NS_IMPL_ISUPPORTS1(nsCairoImage, nsIImage)

//////////////////////////////////////////////
//// nsIImage
nsresult
nsCairoImage::Init(PRInt32 aWidth, PRInt32 aHeight, PRInt32 aDepth, nsMaskRequirements aMaskRequirements)
{
    mWidth = aWidth;
    mHeight = aHeight;

    switch(aMaskRequirements)
    {
    case nsMaskRequirements_kNeeds1Bit:
        mAlphaDepth = 1;
        mCairoFormat = CAIRO_FORMAT_ARGB32;
        break;
    case nsMaskRequirements_kNeeds8Bit:
        mAlphaDepth = 8;
        mCairoFormat = CAIRO_FORMAT_ARGB32;
        break;
    default:
        mAlphaDepth = 0;
        // this should be able to be RGB24, but that doesn't seem to work
        mCairoFormat = CAIRO_FORMAT_ARGB32;
        break;
    }

    PRInt32 size = aWidth*aHeight*4;
    mImageSurfaceBuf = (PRUint32*)nsMemory::Alloc(size);
    NS_ENSURE_TRUE(mImageSurfaceBuf, NS_ERROR_OUT_OF_MEMORY);
    // Fill it in. Setting alpha to FF is important for images with no
    // alpha of their own
    memset(mImageSurfaceBuf, 0xFF, size);

    mImageSurface = cairo_image_surface_create_for_data
        ((char*)mImageSurfaceBuf, mCairoFormat, mWidth, mHeight, aWidth * 4);
    return NS_OK;
}

PRInt32
nsCairoImage::GetBytesPix()
{
    // not including alpha, I guess
    return 3;
}

PRBool
nsCairoImage::GetIsRowOrderTopToBottom()
{
    return PR_FALSE;
}

PRInt32
nsCairoImage::GetWidth()
{
    return mWidth;
}

PRInt32
nsCairoImage::GetHeight()
{
    return mHeight;
}

PRUint8 *
nsCairoImage::GetBits()
{
    return (PRUint8 *) mImageSurfaceData;
}

PRInt32
nsCairoImage::GetLineStride()
{
    return mWidth*3;
}

PRBool
nsCairoImage::GetHasAlphaMask()
{
    return mAlphaDepth > 0;
}

PRUint8 *
nsCairoImage::GetAlphaBits()
{
    return (PRUint8 *) mImageSurfaceAlpha;
}

PRInt32
nsCairoImage::GetAlphaLineStride()
{
    return mAlphaDepth == 1 ? (mWidth+7)/8 : mWidth;
}

void
nsCairoImage::ImageUpdated(nsIDeviceContext *aContext, PRUint8 aFlags, nsRect *aUpdateRect)
{
    mDecoded.UnionRect(mDecoded, *aUpdateRect);
}

PRBool
nsCairoImage::GetIsImageComplete()
{
    return mDecoded == nsRect(0, 0, mWidth, mHeight);
}

nsresult
nsCairoImage::Optimize(nsIDeviceContext* aContext)
{
    return NS_OK;
}

nsColorMap *
nsCairoImage::GetColorMap()
{
    return NULL;
}

/* WHY ARE THESE HERE?!?!?!?!?!? */
/* INSTEAD OF RENDERINGCONTEXT, WHICH ONLY TAKES IMGICONTAINER?!?!?!? */
NS_IMETHODIMP
nsCairoImage::Draw(nsIRenderingContext &aContext, nsIDrawingSurface *aSurface,
                   PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
{
    return Draw(aContext, aSurface, 0, 0, mWidth, mHeight, aX, aY, aWidth, aHeight);
}

void
nsCairoImage::UpdateFromImageData()
{
    if (!mImageSurfaceData)
        return;
    
    NS_ASSERTION(mAlphaDepth == 0 || mAlphaDepth == 1 || mAlphaDepth == 8,
                 "Bad alpha depth");

    PRInt32 alphaIndex = 0;
    PRInt32 bufIndex = 0;
    for (PRInt32 row = 0; row < mHeight; ++row) {
        for (PRInt32 col = 0; col < mWidth; ++col) {
            PRUint8 alpha = 0xFF;
            if (mAlphaDepth == 1) {
                PRUint8 mask = 1 << (7 - (col&7));
                if (!(mImageSurfaceAlpha[alphaIndex] & mask)) {
                    alpha = 0;
                }
                if (mask == 0x01) {
                    ++alphaIndex;
                }
            } else if (mAlphaDepth == 8) {
                alpha = mImageSurfaceAlpha[bufIndex];
            }
            mImageSurfaceBuf[bufIndex] = ThreeChannelToARGB(&mImageSurfaceData[bufIndex*3], alpha);
            ++bufIndex;
        }
        if (mAlphaDepth == 1) {
            if (mWidth & 7) {
                ++alphaIndex;
            }
        }
    }

    if (PR_FALSE) { // Enabling this saves memory but can lead to pathological
        // behaviour
        nsMemory::Free(mImageSurfaceData);
        mImageSurfaceData = nsnull;
        if (mImageSurfaceAlpha) {
            nsMemory::Free(mImageSurfaceAlpha);
            mImageSurfaceAlpha = nsnull;
        }

        mHadAnyData = PR_TRUE;
    }
}

NS_IMETHODIMP
nsCairoImage::Draw(nsIRenderingContext &aContext, nsIDrawingSurface *aSurface,
                   PRInt32 aSX, PRInt32 aSY, PRInt32 aSWidth, PRInt32 aSHeight,
                   PRInt32 aDX, PRInt32 aDY, PRInt32 aDWidth, PRInt32 aDHeight)
{
#if 1
    fprintf (stderr, "****** nsCairoImage::Draw: (%d,%d,%d,%d) -> (%d,%d,%d,%d)\n",
             aSX, aSY, aSWidth, aSHeight,
             aDX, aDY, aDWidth, aDHeight);
#endif
    UpdateFromImageData();

    nsCairoDrawingSurface *dstSurf = NS_STATIC_CAST(nsCairoDrawingSurface*, aSurface);
    nsCairoRenderingContext *cairoContext = NS_STATIC_CAST(nsCairoRenderingContext*, &aContext);

    cairo_t *dstCairo = cairoContext->GetCairo();

    cairo_translate(dstCairo, aDX, aDY);

    cairo_pattern_t *pat = cairo_pattern_create_for_surface (mImageSurface);
    cairo_matrix_t* matrix = cairo_matrix_create();
    if (matrix) {
        cairo_matrix_set_affine(matrix, double(aSWidth)/aDWidth, 0,
                                0, double(aSHeight)/aDHeight, aSX, aSY);
        cairo_pattern_set_matrix(pat, matrix);
        cairo_matrix_destroy(matrix);
    }
    cairo_set_pattern (dstCairo, pat);
    cairo_pattern_destroy (pat);

    cairo_new_path(dstCairo);
    cairo_rectangle (dstCairo, 0, 0, aDWidth, aDHeight);
    cairo_fill (dstCairo);
    cairo_translate(dstCairo, -aDX, -aDY);
    
    return NS_OK;
}

NS_IMETHODIMP
nsCairoImage::DrawTile(nsIRenderingContext &aContext,
                       nsIDrawingSurface *aSurface,
                       PRInt32 aSXOffset, PRInt32 aSYOffset,
                       PRInt32 aPadX, PRInt32 aPadY,
                       const nsRect &aTileRect)
{
    UpdateFromImageData();

    if (aPadX || aPadY)
        fprintf (stderr, "Warning: nsCairoImage::DrawTile given padX(%d)/padY(%d), ignoring\n", aPadX, aPadY);

    nsCairoDrawingSurface *dstSurf = NS_STATIC_CAST(nsCairoDrawingSurface*, aSurface);
    nsCairoRenderingContext *cairoContext = NS_STATIC_CAST(nsCairoRenderingContext*, &aContext);

    cairo_t *dstCairo = cairoContext->GetCairo();

    cairo_translate(dstCairo, aTileRect.x, aTileRect.y);

    cairo_pattern_t *pat = cairo_pattern_create_for_surface (mImageSurface);
    cairo_matrix_t* matrix = cairo_matrix_create();
    if (matrix) {
        nsCOMPtr<nsIDeviceContext> dc;
        aContext.GetDeviceContext(*getter_AddRefs(dc));
        float app2dev = dc->AppUnitsToDevUnits();
        cairo_matrix_set_affine(matrix, app2dev, 0, 0, app2dev, aSXOffset, aSYOffset);
        cairo_pattern_set_matrix(pat, matrix);
        cairo_matrix_destroy(matrix);
    }
    cairo_pattern_set_extend(pat, CAIRO_EXTEND_REPEAT);
    cairo_set_pattern (dstCairo, pat);
    cairo_pattern_destroy (pat);

    cairo_new_path(dstCairo);
    cairo_rectangle (dstCairo, 0, 0, aTileRect.width, aTileRect.height);
    cairo_fill (dstCairo);
    cairo_translate(dstCairo, -aTileRect.x, -aTileRect.y);

    return NS_OK;
}

NS_IMETHODIMP
nsCairoImage::DrawToImage(nsIImage* aDstImage, PRInt32 aDX, PRInt32 aDY, PRInt32 aDWidth, PRInt32 aDHeight)
{
    UpdateFromImageData();

    nsCairoImage *dstCairoImage = NS_STATIC_CAST(nsCairoImage*, aDstImage);

    cairo_t *dstCairo = cairo_create ();

    cairo_set_target_surface(dstCairo, dstCairoImage->mImageSurface);

    // clip to target
    cairo_rectangle(dstCairo, double(aDX), double(aDY), double(aDWidth), double(aDHeight));
    cairo_clip(dstCairo);

    // scale up to the size difference
    cairo_scale(dstCairo, double(aDWidth)/double(mWidth), double(aDHeight)/double(mHeight));

    // move to where we need to start the image rectangle, so that
    // it gets clipped to the right place
    cairo_translate(dstCairo, double(aDX), double(aDY));

    // show it
    cairo_show_surface (dstCairo, mImageSurface, mWidth, mHeight);

    cairo_destroy (dstCairo);

    return NS_OK;
}

PRInt8
nsCairoImage::GetAlphaDepth()
{
    return mAlphaDepth;
}

void *
nsCairoImage::GetBitInfo()
{
    return NULL;
}

NS_IMETHODIMP
nsCairoImage::LockImagePixels(PRBool aMaskPixels)
{
    if (mImageSurfaceData) {
        return NS_OK;
    }

    if (mAlphaDepth > 0) {
        NS_ASSERTION(mAlphaDepth == 1 || mAlphaDepth == 8,
                     "Bad alpha depth");
        PRUint32 size = mHeight*(mAlphaDepth == 1 ? ((mWidth+7)/8) : mWidth);
        mImageSurfaceAlpha = (PRUint8*)nsMemory::Alloc(size);
        if (!mImageSurfaceAlpha)
            return NS_ERROR_OUT_OF_MEMORY;
    }

    PRUint32 size = mWidth * mHeight * 3;
    mImageSurfaceData = (PRUint8*)nsMemory::Alloc(size);
    if (!mImageSurfaceData)
        return NS_ERROR_OUT_OF_MEMORY;

    PRInt32 count = mWidth*mHeight;
    if (mAlphaDepth > 0 && mHadAnyData) {
        PRInt32 size = mHeight*(mAlphaDepth == 1 ? ((mWidth+7)/8) : mWidth);
        mImageSurfaceAlpha = (PRUint8*)nsMemory::Alloc(size);

        // fill from existing ARGB buffer
        if (mAlphaDepth == 8) {
            for (PRInt32 i = 0; i < count; ++i) {
                mImageSurfaceAlpha[i] = (PRUint8)(mImageSurfaceBuf[i] >> 24);
            }
        } else {
            PRInt32 i = 0;
            for (PRInt32 row = 0; row < mHeight; ++row) {
                PRUint8 alphaBits = 0;
                for (PRInt32 col = 0; col < mWidth; ++col) {
                    PRUint8 mask = 1 << (7 - (col&7));
                    if (mImageSurfaceBuf[row*mWidth + col] & 0xFF000000) {
                        alphaBits |= mask;
                    }
                    if (mask == 0x01) {
                        // This mask byte is complete, write it back
                        mImageSurfaceAlpha[i] = alphaBits;
                        alphaBits = 0;
                        ++i;
                    }
                }
                if (mWidth & 7) {
                    // write back the incomplete alpha mask
                    mImageSurfaceAlpha[i] = alphaBits;
                    ++i;
                }
            }
        }
    }
    
    if (mHadAnyData) {
        // fill from existing ARGB buffer
        for (PRInt32 i = 0; i < count; ++i) {
            ARGBToThreeChannel(&mImageSurfaceBuf[i], &mImageSurfaceData[i*3]);
        }
    }

    return NS_OK;
}

NS_IMETHODIMP
nsCairoImage::UnlockImagePixels(PRBool aMaskPixels)
{
    return NS_OK;
}