tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gfxColor.h (2152B)


      1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      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/. */
      5 
      6 #ifndef GFX_COLOR_H
      7 #define GFX_COLOR_H
      8 
      9 #include "mozilla/Attributes.h"  // for MOZ_ALWAYS_INLINE
     10 #include "mozilla/gfx/Types.h"   // for mozilla::gfx::SurfaceFormatBit
     11 
     12 /**
     13 * Fast approximate division by 255. It has the property that
     14 * for all 0 <= n <= 255*255, GFX_DIVIDE_BY_255(n) == n/255.
     15 * But it only uses two adds and two shifts instead of an
     16 * integer division (which is expensive on many processors).
     17 *
     18 * equivalent to ((v)/255)
     19 */
     20 #define GFX_DIVIDE_BY_255(v) \
     21  (((((unsigned)(v)) << 8) + ((unsigned)(v)) + 255) >> 16)
     22 
     23 /**
     24 * Fast premultiply
     25 *
     26 * equivalent to (((c)*(a))/255)
     27 */
     28 uint8_t MOZ_ALWAYS_INLINE gfxPreMultiply(uint8_t c, uint8_t a) {
     29  return GFX_DIVIDE_BY_255((c) * (a));
     30 }
     31 
     32 /**
     33 * Pack the 4 8-bit channels (A,R,G,B)
     34 * into a 32-bit packed NON-premultiplied pixel.
     35 */
     36 uint32_t MOZ_ALWAYS_INLINE gfxPackedPixelNoPreMultiply(uint8_t a, uint8_t r,
     37                                                       uint8_t g, uint8_t b) {
     38  return (((a) << mozilla::gfx::SurfaceFormatBit::OS_A) |
     39          ((r) << mozilla::gfx::SurfaceFormatBit::OS_R) |
     40          ((g) << mozilla::gfx::SurfaceFormatBit::OS_G) |
     41          ((b) << mozilla::gfx::SurfaceFormatBit::OS_B));
     42 }
     43 
     44 /**
     45 * Pack the 4 8-bit channels (A,R,G,B)
     46 * into a 32-bit packed premultiplied pixel.
     47 */
     48 uint32_t MOZ_ALWAYS_INLINE gfxPackedPixel(uint8_t a, uint8_t r, uint8_t g,
     49                                          uint8_t b) {
     50  if (a == 0x00)
     51    return 0x00000000;
     52  else if (a == 0xFF) {
     53    return gfxPackedPixelNoPreMultiply(a, r, g, b);
     54  } else {
     55    return ((a) << mozilla::gfx::SurfaceFormatBit::OS_A) |
     56           (gfxPreMultiply(r, a) << mozilla::gfx::SurfaceFormatBit::OS_R) |
     57           (gfxPreMultiply(g, a) << mozilla::gfx::SurfaceFormatBit::OS_G) |
     58           (gfxPreMultiply(b, a) << mozilla::gfx::SurfaceFormatBit::OS_B);
     59  }
     60 }
     61 
     62 #endif /* _GFX_COLOR_H */