widget/gtk/nsImageToPixbuf.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* vim:set sw=4 sts=4 et cin: */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <gdk-pixbuf/gdk-pixbuf.h>
     8 #include "nsImageToPixbuf.h"
    10 #include "imgIContainer.h"
    11 #include "mozilla/gfx/2D.h"
    12 #include "mozilla/RefPtr.h"
    13 #include "nsAutoPtr.h"
    15 using mozilla::gfx::DataSourceSurface;
    16 using mozilla::gfx::SurfaceFormat;
    17 using mozilla::RefPtr;
    19 NS_IMPL_ISUPPORTS(nsImageToPixbuf, nsIImageToPixbuf)
    21 inline unsigned char
    22 unpremultiply (unsigned char color,
    23                unsigned char alpha)
    24 {
    25     if (alpha == 0)
    26         return 0;
    27     // plus alpha/2 to round instead of truncate
    28     return (color * 255 + alpha / 2) / alpha;
    29 }
    31 NS_IMETHODIMP_(GdkPixbuf*)
    32 nsImageToPixbuf::ConvertImageToPixbuf(imgIContainer* aImage)
    33 {
    34     return ImageToPixbuf(aImage);
    35 }
    37 GdkPixbuf*
    38 nsImageToPixbuf::ImageToPixbuf(imgIContainer* aImage)
    39 {
    40     RefPtr<SourceSurface> surface =
    41       aImage->GetFrame(imgIContainer::FRAME_CURRENT,
    42                        imgIContainer::FLAG_SYNC_DECODE);
    44     // If the last call failed, it was probably because our call stack originates
    45     // in an imgINotificationObserver event, meaning that we're not allowed request
    46     // a sync decode. Presumably the originating event is something sensible like
    47     // OnStopFrame(), so we can just retry the call without a sync decode.
    48     if (!surface)
    49       surface = aImage->GetFrame(imgIContainer::FRAME_CURRENT,
    50                                  imgIContainer::FLAG_NONE);
    52     NS_ENSURE_TRUE(surface, nullptr);
    54     return SourceSurfaceToPixbuf(surface,
    55                                  surface->GetSize().width,
    56                                  surface->GetSize().height);
    57 }
    59 GdkPixbuf*
    60 nsImageToPixbuf::SourceSurfaceToPixbuf(SourceSurface* aSurface,
    61                                        int32_t aWidth,
    62                                        int32_t aHeight)
    63 {
    64     MOZ_ASSERT(aWidth <= aSurface->GetSize().width &&
    65                aHeight <= aSurface->GetSize().height,
    66                "Requested rect is bigger than the supplied surface");
    68     GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
    69                                        aWidth, aHeight);
    70     if (!pixbuf)
    71         return nullptr;
    73     uint32_t destStride = gdk_pixbuf_get_rowstride (pixbuf);
    74     guchar* destPixels = gdk_pixbuf_get_pixels (pixbuf);
    76     RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
    77     DataSourceSurface::MappedSurface map;
    78     dataSurface->Map(DataSourceSurface::MapType::READ, &map);
    79     uint8_t* srcData = map.mData;
    80     int32_t srcStride = map.mStride;
    82     SurfaceFormat format = dataSurface->GetFormat();
    84     for (int32_t row = 0; row < aHeight; ++row) {
    85         for (int32_t col = 0; col < aWidth; ++col) {
    86             guchar* destPixel = destPixels + row * destStride + 4 * col;
    88             uint32_t* srcPixel =
    89                 reinterpret_cast<uint32_t*>((srcData + row * srcStride + 4 * col));
    91             if (format == SurfaceFormat::B8G8R8A8) {
    92                 const uint8_t a = (*srcPixel >> 24) & 0xFF;
    93                 const uint8_t r = unpremultiply((*srcPixel >> 16) & 0xFF, a);
    94                 const uint8_t g = unpremultiply((*srcPixel >>  8) & 0xFF, a);
    95                 const uint8_t b = unpremultiply((*srcPixel >>  0) & 0xFF, a);
    97                 *destPixel++ = r;
    98                 *destPixel++ = g;
    99                 *destPixel++ = b;
   100                 *destPixel++ = a;
   101             } else {
   102                 MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8);
   104                 const uint8_t r = (*srcPixel >> 16) & 0xFF;
   105                 const uint8_t g = (*srcPixel >>  8) & 0xFF;
   106                 const uint8_t b = (*srcPixel >>  0) & 0xFF;
   108                 *destPixel++ = r;
   109                 *destPixel++ = g;
   110                 *destPixel++ = b;
   111                 *destPixel++ = 0xFF; // A
   112             }
   113         }
   114     }
   116     dataSurface->Unmap();
   118     return pixbuf;
   119 }

mercurial