[dali_1.0.27] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / internal / common / image-sampler.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/common/image-sampler.h>
20
21 #include <iostream>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 namespace ImageSampler
30 {
31
32 namespace
33 {
34
35 // Adjust these shift sizes if the FilterMode enum grows
36 const int MINIFY_BIT_SHIFT  = 0;    // Room for 16
37 const int MAGNIFY_BIT_SHIFT = 4;
38
39 const int MASK_MINIFY_FILTER    = 0x0000000F;
40 const int MASK_MAGNIFY_FILTER   = 0x000000F0;
41
42 const unsigned int FILTER_MODE_COUNT = 4;
43
44 FilterMode::Type FILTER_MODE_OPTIONS[ FILTER_MODE_COUNT ] =
45   { FilterMode::NONE,
46     FilterMode::DEFAULT,
47     FilterMode::NEAREST,
48     FilterMode::LINEAR };
49
50 } // namespace
51
52 /**
53  * Utility to store one of the FilterMode values.
54  * @param[out] options A bitmask used to store the FilterMode values.
55  * @param[in] factor The FilterMode value.
56  * @param[in] bitshift Used to shift to the correct part of options.
57  */
58 void StoreFilterMode( unsigned int& options, FilterMode::Type mode, int bitShift )
59 {
60   // Start shifting from 1 as 0 is the unassigned state
61   switch ( mode )
62   {
63     case FilterMode::NONE:
64     {
65       // Nothing to do
66       break;
67     }
68     case FilterMode::DEFAULT:
69     {
70       options |= ( 1u << bitShift );
71       break;
72     }
73     case FilterMode::NEAREST:
74     {
75       options |= ( 2u << bitShift );
76       break;
77     }
78     case FilterMode::LINEAR:
79     {
80       options |= ( 3u << bitShift );
81       break;
82     }
83   }
84 }
85
86 /**
87  * Utility to retrieve one of the FilterMode values.
88  * @param[in] options A bitmask of filter values.
89  * @param[in] mask The used to mask unwanted values.
90  * @param[in] bitshift Used to shift to the correct part of options.
91  * @return Return the filter mode.
92  */
93 FilterMode::Type RetrieveFilterMode( unsigned int options, int mask, int bitShift )
94 {
95   unsigned int index = options & mask;
96
97   index = ( index >> bitShift );    // Zero based index for array
98
99   DALI_ASSERT_DEBUG( index < FILTER_MODE_COUNT );
100
101   return FILTER_MODE_OPTIONS[ index ];
102 }
103
104 unsigned int PackBitfield( FilterMode::Type minify, FilterMode::Type magnify )
105 {
106   unsigned int bitfield = 0;
107   StoreFilterMode( bitfield, minify, MINIFY_BIT_SHIFT );
108   StoreFilterMode( bitfield, magnify, MAGNIFY_BIT_SHIFT );
109   return bitfield;
110 }
111
112 FilterMode::Type GetMinifyFilterMode( unsigned int bitfield )
113 {
114   return RetrieveFilterMode( bitfield, MASK_MINIFY_FILTER, MINIFY_BIT_SHIFT );
115 }
116
117 FilterMode::Type GetMagnifyFilterMode( unsigned int bitfield )
118 {
119   return RetrieveFilterMode( bitfield, MASK_MAGNIFY_FILTER, MAGNIFY_BIT_SHIFT );
120 }
121
122 } // namespace ImageSampler
123
124 } // namespace Internal
125
126 } // namespace Dali