112366Sgabeblack@google.com/*
212366Sgabeblack@google.com * Copyright (c) 2015 ARM Limited
312366Sgabeblack@google.com * All rights reserved
412366Sgabeblack@google.com *
512366Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612366Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712366Sgabeblack@google.com * property including but not limited to intellectual property relating
812366Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912366Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012366Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112366Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212366Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312366Sgabeblack@google.com *
1412366Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1512366Sgabeblack@google.com * modification, are permitted provided that the following conditions are
1612366Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
1712366Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1812366Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1912366Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2012366Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2112366Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2212366Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2312366Sgabeblack@google.com * this software without specific prior written permission.
2412366Sgabeblack@google.com *
2512366Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2612366Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2712366Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2812366Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2912366Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3012366Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3112366Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3212366Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3312366Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3412366Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3512366Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3612366Sgabeblack@google.com *
3712366Sgabeblack@google.com * Authors: Andreas Sandberg
3812366Sgabeblack@google.com */
3912366Sgabeblack@google.com
4012366Sgabeblack@google.com#include "base/pixel.hh"
4112366Sgabeblack@google.com
4212366Sgabeblack@google.com#include <cassert>
4312366Sgabeblack@google.com
4412366Sgabeblack@google.com#include "base/bitfield.hh"
4512366Sgabeblack@google.com
4612366Sgabeblack@google.comconst PixelConverter PixelConverter::rgba8888_le(4, 0, 8, 16, 8, 8, 8);
4712366Sgabeblack@google.comconst PixelConverter PixelConverter::rgba8888_be(4, 0, 8, 16, 8, 8, 8,
4812366Sgabeblack@google.com                                                 BigEndianByteOrder);
4912366Sgabeblack@google.comconst PixelConverter PixelConverter::rgb565_le(2,  0, 5, 11, 5, 6, 5);
5012366Sgabeblack@google.comconst PixelConverter PixelConverter::rgb565_be(2,  0, 5, 11, 5, 6, 5,
5112366Sgabeblack@google.com                                               BigEndianByteOrder);
5212366Sgabeblack@google.com
5312366Sgabeblack@google.comPixelConverter::PixelConverter(unsigned _length,
5412366Sgabeblack@google.com                               unsigned ro, unsigned go, unsigned bo,
5512366Sgabeblack@google.com                               unsigned rw, unsigned gw, unsigned bw,
5612366Sgabeblack@google.com                               ByteOrder _byte_order)
5712366Sgabeblack@google.com    : length(_length),
5812366Sgabeblack@google.com      depth(rw + gw + bw),
5912366Sgabeblack@google.com      byte_order(_byte_order),
6012366Sgabeblack@google.com      ch_r(ro, rw),
6112366Sgabeblack@google.com      ch_g(go, gw),
6212366Sgabeblack@google.com      ch_b(bo, bw)
6312366Sgabeblack@google.com{
6412366Sgabeblack@google.com    assert(length > 1);
6512366Sgabeblack@google.com}
6612366Sgabeblack@google.com
6712366Sgabeblack@google.comPixelConverter::Channel::Channel(unsigned _offset, unsigned width)
6812366Sgabeblack@google.com    : offset(_offset),
6912366Sgabeblack@google.com      mask(::mask(width)),
7012366Sgabeblack@google.com      factor(255.0 / mask)
7112366Sgabeblack@google.com{
7212366Sgabeblack@google.com}
7312366Sgabeblack@google.com
7412366Sgabeblack@google.comuint32_t
7512366Sgabeblack@google.comPixelConverter::readWord(const uint8_t *p) const
7612366Sgabeblack@google.com{
7712366Sgabeblack@google.com    uint32_t word(0);
7812366Sgabeblack@google.com
7912366Sgabeblack@google.com    if (byte_order == LittleEndianByteOrder) {
8012366Sgabeblack@google.com        for (int i = 0; i < length; ++i)
8112366Sgabeblack@google.com            word |= p[i] << (8 * i);
8212366Sgabeblack@google.com    } else {
8312366Sgabeblack@google.com        for (int i = 0; i < length; ++i)
8412366Sgabeblack@google.com            word |= p[i] << (8 * (length - i - 1));
8512366Sgabeblack@google.com    }
8612366Sgabeblack@google.com
8712366Sgabeblack@google.com    return word;
8812366Sgabeblack@google.com}
8912366Sgabeblack@google.com
9012366Sgabeblack@google.comvoid
9112366Sgabeblack@google.comPixelConverter::writeWord(uint8_t *p, uint32_t word) const
9212366Sgabeblack@google.com{
9312366Sgabeblack@google.com    if (byte_order == LittleEndianByteOrder) {
9412366Sgabeblack@google.com        for (int i = 0; i < length; ++i)
9512366Sgabeblack@google.com            p[i] = (word >> (8 * i)) & 0xFF;
9612366Sgabeblack@google.com    } else {
9712366Sgabeblack@google.com        for (int i = 0; i < length; ++i)
9812366Sgabeblack@google.com            p[i] = (word >> (8 * (length - i - 1))) & 0xFF;
9912366Sgabeblack@google.com    }
10012366Sgabeblack@google.com}
101