LCOV - code coverage report
Current view: top level - include/basebmp - rgbmaskpixelformats.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 20 20 100.0 %
Date: 2015-06-13 12:38:46 Functions: 5 13 38.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_BASEBMP_RGBMASKPIXELFORMATS_HXX
      21             : #define INCLUDED_BASEBMP_RGBMASKPIXELFORMATS_HXX
      22             : 
      23             : #include <basebmp/color.hxx>
      24             : #include <basebmp/colortraits.hxx>
      25             : #include <basebmp/accessor.hxx>
      26             : #include <basebmp/pixeliterator.hxx>
      27             : #include <basebmp/pixelformatadapters.hxx>
      28             : #include <basebmp/metafunctions.hxx>
      29             : #include <basebmp/endian.hxx>
      30             : 
      31             : #include <vigra/numerictraits.hxx>
      32             : #include <vigra/metaprogramming.hxx>
      33             : 
      34             : #include <functional>
      35             : 
      36             : namespace basebmp
      37             : {
      38             : 
      39             : /** Base class operating on RGB truecolor mask pixel
      40             : 
      41             :     Use this template, if you have an (integer) pixel type, and three
      42             :     bitmasks denoting where the channel bits are.
      43             : 
      44             :     @tpl PixelType
      45             :     Input pixel type to operate on
      46             : 
      47             :     @tpl ColorType
      48             :     Underlying color type, to convert the pixel values into
      49             : 
      50             :     @tpl RedMask
      51             :     Bitmask, to access the red bits in the data type
      52             : 
      53             :     @tpl GreenMask
      54             :     Bitmask, to access the green bits in the data type
      55             : 
      56             :     @tpl BlueMask
      57             :     Bitmask, to access the blue bits in the data type
      58             : 
      59             :     @tpl SwapBytes
      60             :     When true, the final pixel values will be byte-swapped before
      61             :     passed on.
      62             :  */
      63             : template< typename     PixelType,
      64             :           typename     ColorType,
      65             :           unsigned int RedMask,
      66             :           unsigned int GreenMask,
      67             :           unsigned int BlueMask,
      68             :           bool         SwapBytes > struct RGBMaskFunctorBase
      69             : {
      70             :     typedef PixelType                                       pixel_type;
      71             :     typedef ColorType                                       color_type;
      72             :     typedef typename make_unsigned<pixel_type>::type        unsigned_pixel_type;
      73             :     typedef typename ColorTraits<ColorType>::component_type component_type;
      74             : 
      75             :     // calc corrective shifts for all three channels in advance
      76             :     enum {
      77             :         red_shift   = numberOfTrailingZeros<RedMask>::value,
      78             :         green_shift = numberOfTrailingZeros<GreenMask>::value,
      79             :         blue_shift  = numberOfTrailingZeros<BlueMask>::value,
      80             : 
      81             :         red_bits    = bitcount<RedMask>::value,
      82             :         green_bits  = bitcount<GreenMask>::value,
      83             :         blue_bits   = bitcount<BlueMask>::value
      84             :     };
      85             : };
      86             : 
      87             : template< typename     PixelType,
      88             :           typename     ColorType,
      89             :           unsigned int RedMask,
      90             :           unsigned int GreenMask,
      91             :           unsigned int BlueMask,
      92             :           bool         SwapBytes > struct RGBMaskGetter :
      93             :         public RGBMaskFunctorBase<PixelType,
      94             :                                   ColorType,
      95             :                                   RedMask,
      96             :                                   GreenMask,
      97             :                                   BlueMask,
      98             :                                   SwapBytes>,
      99             :         public std::unary_function<PixelType, ColorType>
     100             : {
     101             :     typedef RGBMaskFunctorBase<PixelType,
     102             :                                ColorType,
     103             :                                RedMask,
     104             :                                GreenMask,
     105             :                                BlueMask,
     106             :                                SwapBytes> base_type;
     107             : 
     108  1944957039 :     ColorType operator()( PixelType v ) const
     109             :     {
     110  1944957039 :         v = SwapBytes ? byteSwap(v) : v;
     111             : 
     112  1944957039 :         const typename base_type::unsigned_pixel_type red  (v & RedMask);
     113  1944957039 :         const typename base_type::unsigned_pixel_type green(v & GreenMask);
     114  1944957039 :         const typename base_type::unsigned_pixel_type blue (v & BlueMask);
     115             : 
     116             :         // shift color nibbles to right-aligned position. ORing it
     117             :         // channel value shifted twice the number of channel bits, to
     118             :         // spread the value into the component_type range
     119             :         ColorType res( (shiftRight(red,
     120             :                                    base_type::red_shift-8*
     121             :                                    static_cast<signed>(sizeof(typename base_type::component_type))+
     122  1944957039 :                                    base_type::red_bits)) |
     123             :                        (shiftRight(red,
     124             :                                    base_type::red_shift-8*
     125             :                                    static_cast<signed>(sizeof(typename base_type::component_type))+
     126  1944957039 :                                    2*base_type::red_bits)),
     127             : 
     128             :                        (shiftRight(green,
     129             :                                    base_type::green_shift-8*
     130             :                                    static_cast<signed>(sizeof(typename base_type::component_type))+
     131  1944957039 :                                    base_type::green_bits)) |
     132             :                        (shiftRight(green,
     133             :                                    base_type::green_shift-8*
     134             :                                    static_cast<signed>(sizeof(typename base_type::component_type))+
     135  1944957039 :                                    2*base_type::green_bits)),
     136             : 
     137             :                        (shiftRight(blue,
     138             :                                    base_type::blue_shift-8*
     139             :                                    static_cast<signed>(sizeof(typename base_type::component_type))+
     140  1944957039 :                                    base_type::blue_bits)) |
     141             :                        (shiftRight(blue,
     142             :                                    base_type::blue_shift-8*
     143             :                                    static_cast<signed>(sizeof(typename base_type::component_type))+
     144  9724785195 :                                    2*base_type::blue_bits)) );
     145  1944957039 :         return res;
     146             :     }
     147             : };
     148             : 
     149             : template< typename     PixelType,
     150             :           typename     ColorType,
     151             :           unsigned int BaseValue,
     152             :           unsigned int RedMask,
     153             :           unsigned int GreenMask,
     154             :           unsigned int BlueMask,
     155             :           bool         SwapBytes > struct RGBMaskSetter :
     156             :         public RGBMaskFunctorBase<PixelType,
     157             :                                   ColorType,
     158             :                                   RedMask,
     159             :                                   GreenMask,
     160             :                                   BlueMask,
     161             :                                   SwapBytes>,
     162             :         public std::unary_function<ColorType, PixelType>
     163             : {
     164             :     typedef RGBMaskFunctorBase<PixelType,
     165             :                                ColorType,
     166             :                                RedMask,
     167             :                                GreenMask,
     168             :                                BlueMask,
     169             :                                SwapBytes> base_type;
     170             : 
     171  1886836135 :     PixelType operator()( ColorType const& c ) const
     172             :     {
     173  1886836135 :         const typename base_type::unsigned_pixel_type red  (c.getRed());
     174  1886836135 :         const typename base_type::unsigned_pixel_type green(c.getGreen());
     175  1886836135 :         const typename base_type::unsigned_pixel_type blue (c.getBlue());
     176             : 
     177             :         typename base_type::unsigned_pixel_type res(
     178             :             BaseValue |
     179             :             (shiftLeft(red,
     180             :                        base_type::red_shift-8*
     181             :                        static_cast<signed>(sizeof(typename base_type::component_type))+
     182  1886836135 :                        base_type::red_bits) & RedMask) |
     183             :             (shiftLeft(green,
     184             :                        base_type::green_shift-8*
     185             :                        static_cast<signed>(sizeof(typename base_type::component_type))+
     186  1886836135 :                        base_type::green_bits) & GreenMask) |
     187             :             (shiftLeft(blue,
     188             :                        base_type::blue_shift-8*
     189             :                        static_cast<signed>(sizeof(typename base_type::component_type))+
     190  3773672270 :                        base_type::blue_bits) & BlueMask) );
     191             : 
     192  1886836135 :         return SwapBytes ? byteSwap(res) : res;
     193             :     }
     194             : };
     195             : 
     196             : 
     197             : 
     198             : template< typename     PixelType,
     199             :           unsigned int BaseValue,
     200             :           unsigned int RedMask,
     201             :           unsigned int GreenMask,
     202             :           unsigned int BlueMask,
     203             :           bool         SwapBytes > struct PixelFormatTraitsTemplate_RGBMask
     204             : {
     205             :     typedef PixelType                           pixel_type;
     206             : 
     207             :     typedef RGBMaskGetter<pixel_type,
     208             :                           Color,
     209             :                           RedMask,
     210             :                           GreenMask,
     211             :                           BlueMask,
     212             :                           SwapBytes>            getter_type;
     213             :     typedef RGBMaskSetter<pixel_type,
     214             :                           Color,
     215             :                           BaseValue,
     216             :                           RedMask,
     217             :                           GreenMask,
     218             :                           BlueMask,
     219             :                           SwapBytes>            setter_type;
     220             : 
     221             :     typedef PixelIterator<pixel_type>           iterator_type;
     222             :     typedef StandardAccessor<pixel_type>        raw_accessor_type;
     223             :     typedef AccessorSelector<
     224             :         getter_type, setter_type>               accessor_selector;
     225             : };
     226             : 
     227             : 
     228             : 
     229             : // Hopefully this is an understandable plaintext explanation that matches
     230             : // reality...
     231             : 
     232             : // BASEBMP_TRUECOLORMASK_LSB_SWAP means that on a big-endian platform, a pixel
     233             : // value when viewed as an integer (16 or 32 bits) has to be byte-swapped for
     234             : // the channels to match the masks. Or equivalently (I think), on a big-endian
     235             : // platform, the masks need to be byte-swapped to be correct.
     236             : 
     237             : // I.e. on a litte-endian platform the masks work as such.
     238             : 
     239             : // BASEBMP_TRUECOLORMASK_MSB_SWAP means the opposite. The masks work as such
     240             : // on big-endian platforms, on little-endian platforms the pixel needs to be
     241             : // byte-swapped for the masks to work.
     242             : 
     243             : // So in a sense these two names are "backward". It sounds to me as if
     244             : // BASEBMP_TRUECOLORMASK_LSB_SWAP would mean "when on LSB, swap" ;)
     245             : 
     246             : #ifdef OSL_LITENDIAN
     247             : # define BASEBMP_TRUECOLORMASK_LSB_SWAP false
     248             : # define BASEBMP_TRUECOLORMASK_MSB_SWAP true
     249             : #else
     250             : # ifdef OSL_BIGENDIAN
     251             : #  define BASEBMP_TRUECOLORMASK_LSB_SWAP true
     252             : #  define BASEBMP_TRUECOLORMASK_MSB_SWAP false
     253             : # else
     254             : #  error Undetermined endianness!
     255             : # endif
     256             : #endif
     257             : 
     258             : 
     259             : 
     260             : // 16bpp MSB RGB
     261             : typedef PixelFormatTraitsTemplate_RGBMask<
     262             :     sal_uInt16,
     263             :     0,
     264             :     0xF800,
     265             :     0x07E0,
     266             :     0x001F,
     267             :     BASEBMP_TRUECOLORMASK_MSB_SWAP >            PixelFormatTraits_RGB16_565_MSB;
     268             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGB16_565_MSB::getter_type,
     269             :                                   PixelFormatTraits_RGB16_565_MSB::setter_type);
     270             : 
     271             : // 16bpp LSB RGB
     272             : typedef PixelFormatTraitsTemplate_RGBMask<
     273             :     sal_uInt16,
     274             :     0,
     275             :     0xF800,
     276             :     0x07E0,
     277             :     0x001F,
     278             :     BASEBMP_TRUECOLORMASK_LSB_SWAP >            PixelFormatTraits_RGB16_565_LSB;
     279             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGB16_565_LSB::getter_type,
     280             :                                   PixelFormatTraits_RGB16_565_LSB::setter_type);
     281             : 
     282             : 
     283             : // 32bpp formats
     284             : 
     285             : // The intent is that the order of channel names in the names of the 32bpp
     286             : // format typedefs below correspond to the order of the channel bytes in
     287             : // memory, if I understand correctly.... I think the point with the below
     288             : // formats is that the channel byte order in memory is the same regardless of
     289             : // platform byte order.
     290             : 
     291             : // This one used to be called PixelFormatTraits_RGB32_888.
     292             : 
     293             : typedef PixelFormatTraitsTemplate_RGBMask<
     294             :     sal_uInt32,
     295             :     0xFF000000,
     296             :     0x00FF0000,
     297             :     0x0000FF00,
     298             :     0x000000FF,
     299             :     BASEBMP_TRUECOLORMASK_LSB_SWAP >            PixelFormatTraits_BGRA32_8888;
     300             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_BGRA32_8888::getter_type,
     301             :                                   PixelFormatTraits_BGRA32_8888::setter_type);
     302             : 
     303             : // This one used to be called PixelFormatTraits_BGR32_888.
     304             : 
     305             : typedef PixelFormatTraitsTemplate_RGBMask<
     306             :     sal_uInt32,
     307             :     0xFF000000,
     308             :     0x00FF0000,
     309             :     0x0000FF00,
     310             :     0x000000FF,
     311             :     BASEBMP_TRUECOLORMASK_MSB_SWAP >            PixelFormatTraits_ARGB32_8888;
     312             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_ARGB32_8888::getter_type,
     313             :                                   PixelFormatTraits_ARGB32_8888::setter_type);
     314             : 
     315             : // The following two ones were added for Android needs and for completeness
     316             : 
     317             : typedef PixelFormatTraitsTemplate_RGBMask<
     318             :     sal_uInt32,
     319             :     0x000000FF,
     320             :     0xFF000000,
     321             :     0x00FF0000,
     322             :     0x0000FF00,
     323             :     BASEBMP_TRUECOLORMASK_LSB_SWAP >            PixelFormatTraits_ABGR32_8888;
     324             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_ABGR32_8888::getter_type,
     325             :                                   PixelFormatTraits_ABGR32_8888::setter_type);
     326             : 
     327             : typedef PixelFormatTraitsTemplate_RGBMask<
     328             :     sal_uInt32,
     329             :     0x000000FF,
     330             :     0xFF000000,
     331             :     0x00FF0000,
     332             :     0x0000FF00,
     333             :     BASEBMP_TRUECOLORMASK_MSB_SWAP >            PixelFormatTraits_RGBA32_8888;
     334             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGBA32_8888::getter_type,
     335             :                                   PixelFormatTraits_RGBA32_8888::setter_type);
     336             : 
     337             : // Added for Cairo needs, perhaps Android should get an XRGB and replace
     338             : // some uses of ARGB with that instead ?
     339             : 
     340             : typedef PixelFormatTraitsTemplate_RGBMask<
     341             :     sal_uInt32,
     342             :     0x00000000,
     343             :     0x00FF0000,
     344             :     0x0000FF00,
     345             :     0x000000FF,
     346             :     BASEBMP_TRUECOLORMASK_LSB_SWAP >            PixelFormatTraits_BGRX32_8888;
     347             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_BGRX32_8888::getter_type,
     348             :                                   PixelFormatTraits_BGRX32_8888::setter_type);
     349             : 
     350             : 
     351             : } // namespace basebmp
     352             : 
     353             : #endif /* INCLUDED_BASEBMP_RGBMASKPIXELFORMATS_HXX */
     354             : 
     355             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11