File: nsImageToPixbuf.cpp

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (156 lines) | stat: -rw-r--r-- 5,194 bytes parent folder | download | duplicates (7)
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
/* vim:set sw=2 sts=2 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <gdk-pixbuf/gdk-pixbuf.h>

#include "nsImageToPixbuf.h"

#include "imgIContainer.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/RefPtr.h"
#include "GRefPtr.h"
#include "nsCOMPtr.h"

using mozilla::gfx::DataSourceSurface;
using mozilla::gfx::SurfaceFormat;

inline unsigned char unpremultiply(unsigned char color, unsigned char alpha) {
  if (alpha == 0) return 0;
  // plus alpha/2 to round instead of truncate
  return (color * 255 + alpha / 2) / alpha;
}

already_AddRefed<GdkPixbuf> nsImageToPixbuf::ImageToPixbuf(
    imgIContainer* aImage, const mozilla::Maybe<nsIntSize>& aOverrideSize) {
  RefPtr<SourceSurface> surface;

  const uint32_t flags =
      imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY;
  if (aOverrideSize) {
    surface = aImage->GetFrameAtSize(*aOverrideSize,
                                     imgIContainer::FRAME_CURRENT, flags);
  } else {
    surface = aImage->GetFrame(imgIContainer::FRAME_CURRENT, flags);
  }

  // If the last call failed, it was probably because our call stack originates
  // in an imgINotificationObserver event, meaning that we're not allowed
  // request a sync decode. Presumably the originating event is something
  // sensible like OnStopFrame(), so we can just retry the call without a sync
  // decode.
  if (!surface) {
    if (aOverrideSize) {
      surface =
          aImage->GetFrameAtSize(*aOverrideSize, imgIContainer::FRAME_CURRENT,
                                 imgIContainer::FLAG_NONE);
    } else {
      surface = aImage->GetFrame(imgIContainer::FRAME_CURRENT,
                                 imgIContainer::FLAG_NONE);
    }
  }

  NS_ENSURE_TRUE(surface, nullptr);

  return SourceSurfaceToPixbuf(surface, surface->GetSize().width,
                               surface->GetSize().height);
}

already_AddRefed<GdkPixbuf> nsImageToPixbuf::SourceSurfaceToPixbuf(
    SourceSurface* aSurface, int32_t aWidth, int32_t aHeight) {
  using mozilla::gfx::Factory;

  MOZ_ASSERT(aSurface);
  MOZ_ASSERT(aWidth <= aSurface->GetSize().width &&
                 aHeight <= aSurface->GetSize().height,
             "Requested rect is bigger than the supplied surface");

  RefPtr<GdkPixbuf> pixbuf =
      dont_AddRef(gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, aWidth, aHeight));
  if (!pixbuf) {
    return nullptr;
  }

  uint32_t destStride = gdk_pixbuf_get_rowstride(pixbuf);
  guchar* destPixels = gdk_pixbuf_get_pixels(pixbuf);

  RefPtr<DataSourceSurface> dataSurface;
  DataSourceSurface::MappedSurface map;

  SurfaceFormat sourceFormat = aSurface->GetFormat();
  if (MOZ_UNLIKELY(sourceFormat != SurfaceFormat::B8G8R8A8 &&
                   sourceFormat != SurfaceFormat::B8G8R8X8)) {
    dataSurface = Factory::CreateDataSourceSurface(
        mozilla::gfx::IntSize(aWidth, aHeight), SurfaceFormat::B8G8R8A8);
    if (NS_WARN_IF(!dataSurface)) {
      return nullptr;
    }

    if (!dataSurface->Map(DataSourceSurface::MapType::READ_WRITE, &map)) {
      return nullptr;
    }

    RefPtr<mozilla::gfx::DrawTarget> dt = Factory::CreateDrawTargetForData(
        mozilla::gfx::BackendType::CAIRO, map.mData, dataSurface->GetSize(),
        map.mStride, dataSurface->GetFormat());
    if (!dt) {
      dataSurface->Unmap();
      return nullptr;
    }

    dt->FillRect(
        mozilla::gfx::Rect(0, 0, aWidth, aHeight),
        mozilla::gfx::SurfacePattern(aSurface, mozilla::gfx::ExtendMode::CLAMP),
        mozilla::gfx::DrawOptions(1.0f,
                                  mozilla::gfx::CompositionOp::OP_SOURCE));
  } else {
    dataSurface = aSurface->GetDataSurface();
    if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map)) {
      return nullptr;
    }
  }
  MOZ_ASSERT(dataSurface);
  MOZ_ASSERT(map.mData);

  uint8_t* srcData = map.mData;
  int32_t srcStride = map.mStride;

  SurfaceFormat format = dataSurface->GetFormat();

  for (int32_t row = 0; row < aHeight; ++row) {
    for (int32_t col = 0; col < aWidth; ++col) {
      guchar* destPixel = destPixels + row * destStride + 4 * col;

      uint32_t* srcPixel =
          reinterpret_cast<uint32_t*>((srcData + row * srcStride + 4 * col));

      if (format == SurfaceFormat::B8G8R8A8) {
        const uint8_t a = (*srcPixel >> 24) & 0xFF;
        const uint8_t r = unpremultiply((*srcPixel >> 16) & 0xFF, a);
        const uint8_t g = unpremultiply((*srcPixel >> 8) & 0xFF, a);
        const uint8_t b = unpremultiply((*srcPixel >> 0) & 0xFF, a);

        *destPixel++ = r;
        *destPixel++ = g;
        *destPixel++ = b;
        *destPixel++ = a;
      } else {
        MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8);

        const uint8_t r = (*srcPixel >> 16) & 0xFF;
        const uint8_t g = (*srcPixel >> 8) & 0xFF;
        const uint8_t b = (*srcPixel >> 0) & 0xFF;

        *destPixel++ = r;
        *destPixel++ = g;
        *destPixel++ = b;
        *destPixel++ = 0xFF;  // A
      }
    }
  }

  dataSurface->Unmap();

  return pixbuf.forget();
}