180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkDither.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*  The base dither matrix we use to derive optimized ones for 565 and 4444
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 0,  32, 8,  40, 2,  34, 10, 42 },
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 48, 16, 56, 24, 50, 18, 58, 26 },
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 12, 44, 4,  36, 14, 46, 6,  38 },
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 60, 28, 52, 20, 62, 30, 54, 22 },
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 3,  35, 11, 43, 1,  33, 9,  41 },
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 51, 19, 59, 27, 49, 17, 57, 25 },
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 15, 47, 7,  39, 13, 45, 5,  37 },
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 63, 31, 55, 23, 61, 29, 53, 21 }
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    The 4444 version only needs 4 bits, and given that we can reduce its size
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    since the other 4x4 sub pieces all look the same once we truncate the bits.
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    The 565 version only needs 3 bits for red/blue, and only 2 bits for green.
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    For simplicity, we store 3 bits, and have the dither macros for green know
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this, and they shift the dither value down by 1 to make it 2 bits.
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef ENABLE_DITHER_MATRIX_4X4
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruconst uint8_t gDitherMatrix_4Bit_4X4[4][4] = {
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {  0,  8,  2, 10 },
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 12,  4, 14,  6 },
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {  3, 11,  1,  9 },
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    { 15,  7, 13,  5 }
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruconst uint8_t gDitherMatrix_3Bit_4X4[4][4] = {
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {  0,  4,  1,  5 },
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {  6,  2,  7,  3 },
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {  1,  5,  0,  4 },
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {  7,  3,  6,  2 }
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else   // used packed shorts for a scanlines worth of dither values
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruconst uint16_t gDitherMatrix_4Bit_16[4] = {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    0xA280, 0x6E4C, 0x91B3, 0x5D7F
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruconst uint16_t gDitherMatrix_3Bit_16[4] = {
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    0x5140, 0x3726, 0x4051, 0x2637
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
56