LCOV - code coverage report
Current view: top level - libreoffice/basebmp/inc/basebmp - rgbmaskpixelformats.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 13 13 100.0 %
Date: 2012-12-27 Functions: 4 12 33.3 %
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        6730 :     ColorType operator()( PixelType v ) const
     109             :     {
     110        6730 :         v = SwapBytes ? byteSwap(v) : v;
     111             : 
     112        6730 :         const typename base_type::unsigned_pixel_type red  (v & RedMask);
     113        6730 :         const typename base_type::unsigned_pixel_type green(v & GreenMask);
     114        6730 :         const typename base_type::unsigned_pixel_type blue (v & BlueMask);
     115             : 
     116             :         // shift color nibbles to right-aligend 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             :                                    (signed)sizeof(typename base_type::component_type)+
     122             :                                    base_type::red_bits)) |
     123             :                        (shiftRight(red,
     124             :                                    base_type::red_shift-8*
     125             :                                    (signed)sizeof(typename base_type::component_type)+
     126             :                                    2*base_type::red_bits)),
     127             : 
     128             :                        (shiftRight(green,
     129             :                                    base_type::green_shift-8*
     130             :                                    (signed)sizeof(typename base_type::component_type)+
     131             :                                    base_type::green_bits)) |
     132             :                        (shiftRight(green,
     133             :                                    base_type::green_shift-8*
     134             :                                    (signed)sizeof(typename base_type::component_type)+
     135             :                                    2*base_type::green_bits)),
     136             : 
     137             :                        (shiftRight(blue,
     138             :                                    base_type::blue_shift-8*
     139             :                                    (signed)sizeof(typename base_type::component_type)+
     140             :                                    base_type::blue_bits)) |
     141             :                        (shiftRight(blue,
     142             :                                    base_type::blue_shift-8*
     143             :                                    (signed)sizeof(typename base_type::component_type)+
     144        6730 :                                    2*base_type::blue_bits)) );
     145        6730 :         return res;
     146             :     }
     147             : };
     148             : 
     149             : template< typename     PixelType,
     150             :           typename     ColorType,
     151             :           unsigned int RedMask,
     152             :           unsigned int GreenMask,
     153             :           unsigned int BlueMask,
     154             :           bool         SwapBytes > struct RGBMaskSetter :
     155             :         public RGBMaskFunctorBase<PixelType,
     156             :                                   ColorType,
     157             :                                   RedMask,
     158             :                                   GreenMask,
     159             :                                   BlueMask,
     160             :                                   SwapBytes>,
     161             :         public std::unary_function<ColorType, PixelType>
     162             : {
     163             :     typedef RGBMaskFunctorBase<PixelType,
     164             :                                ColorType,
     165             :                                RedMask,
     166             :                                GreenMask,
     167             :                                BlueMask,
     168             :                                SwapBytes> base_type;
     169             : 
     170         557 :     PixelType operator()( ColorType const& c ) const
     171             :     {
     172         557 :         const typename base_type::unsigned_pixel_type red  (c.getRed());
     173         557 :         const typename base_type::unsigned_pixel_type green(c.getGreen());
     174         557 :         const typename base_type::unsigned_pixel_type blue (c.getBlue());
     175             : 
     176             :         typename base_type::unsigned_pixel_type res(
     177             :             (shiftLeft(red,
     178             :                        base_type::red_shift-8*
     179             :                        (signed)sizeof(typename base_type::component_type)+
     180             :                        base_type::red_bits) & RedMask) |
     181             :             (shiftLeft(green,
     182             :                        base_type::green_shift-8*
     183             :                        (signed)sizeof(typename base_type::component_type)+
     184             :                        base_type::green_bits) & GreenMask) |
     185             :             (shiftLeft(blue,
     186             :                        base_type::blue_shift-8*
     187             :                        (signed)sizeof(typename base_type::component_type)+
     188         557 :                        base_type::blue_bits) & BlueMask) );
     189             : 
     190         557 :         return SwapBytes ? byteSwap(res) : res;
     191             :     }
     192             : };
     193             : 
     194             : //-----------------------------------------------------------------------------
     195             : 
     196             : template< typename     PixelType,
     197             :           unsigned int RedMask,
     198             :           unsigned int GreenMask,
     199             :           unsigned int BlueMask,
     200             :           bool         SwapBytes > struct PixelFormatTraitsTemplate_RGBMask
     201             : {
     202             :     typedef PixelType                           pixel_type;
     203             : 
     204             :     typedef RGBMaskGetter<pixel_type,
     205             :                           Color,
     206             :                           RedMask,
     207             :                           GreenMask,
     208             :                           BlueMask,
     209             :                           SwapBytes>            getter_type;
     210             :     typedef RGBMaskSetter<pixel_type,
     211             :                           Color,
     212             :                           RedMask,
     213             :                           GreenMask,
     214             :                           BlueMask,
     215             :                           SwapBytes>            setter_type;
     216             : 
     217             :     typedef PixelIterator<pixel_type>           iterator_type;
     218             :     typedef StandardAccessor<pixel_type>        raw_accessor_type;
     219             :     typedef AccessorSelector<
     220             :         getter_type, setter_type>               accessor_selector;
     221             : };
     222             : 
     223             : //-----------------------------------------------------------------------------
     224             : 
     225             : // Hopefully this is an understandable plaintext explanation that matches
     226             : // reality...
     227             : 
     228             : // BASEBMP_TRUECOLORMASK_LSB_SWAP means that on a big-endian platform, a pixel
     229             : // value when viewed as an integer (16 or 32 bits) has to be byte-swapped for
     230             : // the channels to match the masks. Or equivalently (I think), on a big-endian
     231             : // platform, the masks need to be byte-swapped to be correct.
     232             : 
     233             : // I.e. on a litte-endian platform the masks work as such.
     234             : 
     235             : // BASEBMP_TRUECOLORMASK_MSB_SWAP means the opposite. The masks work as such
     236             : // on big-endian platforms, on little-endian platforms the pixel needs to be
     237             : // byte-swapped for the masks to work.
     238             : 
     239             : // So in a sense these two names are "backward". It sounds to me as if
     240             : // BASEBMP_TRUECOLORMASK_LSB_SWAP would mean "when on LSB, swap" ;)
     241             : 
     242             : #ifdef OSL_LITENDIAN
     243             : # define BASEBMP_TRUECOLORMASK_LSB_SWAP false
     244             : # define BASEBMP_TRUECOLORMASK_MSB_SWAP true
     245             : #else
     246             : # ifdef OSL_BIGENDIAN
     247             : #  define BASEBMP_TRUECOLORMASK_LSB_SWAP true
     248             : #  define BASEBMP_TRUECOLORMASK_MSB_SWAP false
     249             : # else
     250             : #  error Undetermined endianness!
     251             : # endif
     252             : #endif
     253             : 
     254             : //-----------------------------------------------------------------------------
     255             : 
     256             : // 16bpp MSB RGB
     257             : typedef PixelFormatTraitsTemplate_RGBMask<
     258             :     sal_uInt16,
     259             :     0xF800,
     260             :     0x07E0,
     261             :     0x001F,
     262             :     BASEBMP_TRUECOLORMASK_MSB_SWAP >            PixelFormatTraits_RGB16_565_MSB;
     263             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGB16_565_MSB::getter_type,
     264             :                                   PixelFormatTraits_RGB16_565_MSB::setter_type);
     265             : 
     266             : // 16bpp LSB RGB
     267             : typedef PixelFormatTraitsTemplate_RGBMask<
     268             :     sal_uInt16,
     269             :     0xF800,
     270             :     0x07E0,
     271             :     0x001F,
     272             :     BASEBMP_TRUECOLORMASK_LSB_SWAP >            PixelFormatTraits_RGB16_565_LSB;
     273             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGB16_565_LSB::getter_type,
     274             :                                   PixelFormatTraits_RGB16_565_LSB::setter_type);
     275             : 
     276             : 
     277             : // 32bpp formats
     278             : 
     279             : // The intent is that the order of channel names in the names of the 32bpp
     280             : // format typedefs below correspond to the order of the channel bytes in
     281             : // memory, if I understand correctly.... I think the point with the below
     282             : // formats is that the channel byte order in memory is the same regardless of
     283             : // platform byte order.
     284             : 
     285             : // This one used to be called PixelFormatTraits_RGB32_888.
     286             : 
     287             : typedef PixelFormatTraitsTemplate_RGBMask<
     288             :     sal_uInt32,
     289             :     0x00FF0000,
     290             :     0x0000FF00,
     291             :     0x000000FF,
     292             :     BASEBMP_TRUECOLORMASK_LSB_SWAP >            PixelFormatTraits_BGRX32_8888;
     293             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_BGRX32_8888::getter_type,
     294             :                                   PixelFormatTraits_BGRX32_8888::setter_type);
     295             : 
     296             : // This one used to be called PixelFormatTraits_BGR32_888.
     297             : 
     298             : typedef PixelFormatTraitsTemplate_RGBMask<
     299             :     sal_uInt32,
     300             :     0x00FF0000,
     301             :     0x0000FF00,
     302             :     0x000000FF,
     303             :     BASEBMP_TRUECOLORMASK_MSB_SWAP >            PixelFormatTraits_XRGB32_8888;
     304             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_XRGB32_8888::getter_type,
     305             :                                   PixelFormatTraits_XRGB32_8888::setter_type);
     306             : 
     307             : // The following two ones were added for Android needs and for completeness
     308             : 
     309             : typedef PixelFormatTraitsTemplate_RGBMask<
     310             :     sal_uInt32,
     311             :     0xFF000000,
     312             :     0x00FF0000,
     313             :     0x0000FF00,
     314             :     BASEBMP_TRUECOLORMASK_LSB_SWAP >            PixelFormatTraits_XBGR32_8888;
     315             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_XBGR32_8888::getter_type,
     316             :                                   PixelFormatTraits_XBGR32_8888::setter_type);
     317             : 
     318             : typedef PixelFormatTraitsTemplate_RGBMask<
     319             :     sal_uInt32,
     320             :     0xFF000000,
     321             :     0x00FF0000,
     322             :     0x0000FF00,
     323             :     BASEBMP_TRUECOLORMASK_MSB_SWAP >            PixelFormatTraits_RGBX32_8888;
     324             : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGBX32_8888::getter_type,
     325             :                                   PixelFormatTraits_RGBX32_8888::setter_type);
     326             : 
     327             : } // namespace basebmp
     328             : 
     329             : #endif /* INCLUDED_BASEBMP_RGBMASKPIXELFORMATS_HXX */
     330             : 
     331             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10