summaryrefslogtreecommitdiff
path: root/plugins/fast_float/src/fast_float_separate.c
blob: c4186a9292b58d0cd115509ea5c82b0b0a4c7f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//---------------------------------------------------------------------------------
//
//  Little Color Management System, fast floating point extensions
//  Copyright (c) 1998-2020 Marti Maria Saguer, all rights reserved
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------------

#include "fast_float_internal.h"

// Separable input. It just computes the distance from 
// each component to the next one in bytes. It gives components RGB in this order
// 
// Encoding  Starting      Increment   DoSwap   Swapfirst Extra 
// RGB,       012            333          0         0       0   
// RGBA,      012            444          0         0       1   
// ARGB,      123            444          0         1       1   
// BGR,       210            333          1         0       0   
// BGRA,      210            444          1         1       1   
// ABGR       321            444          1         0       1   
//
//
//  On planar configurations, the distance is the stride added to any non-negative
//
//  RGB       0, S, 2*S      111
//  RGBA      0, S, 2*S      111    (fourth plane is safely ignored)
//  ARGB      S, 2*S, 3*S    111
//  BGR       2*S, S, 0      111
//  BGRA      2*S, S, 0,     111    (fourth plane is safely ignored)
//  ABGR      3*S, 2*S, S    111
//
//----------------------------------------------------------------------------------------


// Return the size in bytes of a given formatter
static
int trueBytesSize(cmsUInt32Number Format)
{
       int fmt_bytes = T_BYTES(Format);

       // For double, the T_BYTES field returns zero
       if (fmt_bytes == 0)
              return sizeof(double);

       // Otherwise, it is already correct for all formats
       return fmt_bytes;
}

// RGBA -> normal
// ARGB -> swap first
// ABGR -> doSwap
// BGRA -> doSwap swapFirst

// This function computes the distance from each component to the next one in bytes. 
static
void ComputeIncrementsForChunky(cmsUInt32Number Format, 
                                cmsUInt32Number BytesPerPlane,
                                cmsUInt32Number* nChannels,
                                cmsUInt32Number* nAlpha,
                                cmsUInt32Number ComponentStartingOrder[], 
                                cmsUInt32Number ComponentPointerIncrements[])
{
       int extra = T_EXTRA(Format);
       int channels = T_CHANNELS(Format);
       int total_chans = channels + extra;
       int i;       
       int channelSize = trueBytesSize(Format);
       int pixelSize = channelSize * total_chans;
       
       UNUSED_PARAMETER(BytesPerPlane);

       // Setup the counts
       if (nChannels != NULL)
              *nChannels = channels;

       if (nAlpha != NULL)
              *nAlpha = extra;

       // Separation is independent of starting point and only depends on channel size
       for (i = 0; i < total_chans; i++)
              ComponentPointerIncrements[i] = pixelSize;

       // Handle do swap
       for (i = 0; i < total_chans; i++)
       {
              if (T_DOSWAP(Format)) {
                     ComponentStartingOrder[i] = total_chans - i - 1;
              }
              else {
                     ComponentStartingOrder[i] = i;
              }
       }

       // Handle swap first (ROL of positions), example CMYK -> KCMY | 0123 -> 3012
       if (T_SWAPFIRST(Format)) {
              
              cmsUInt32Number tmp = ComponentStartingOrder[0];
              for (i = 0; i < total_chans-1; i++)
                     ComponentStartingOrder[i] = ComponentStartingOrder[i + 1];

              ComponentStartingOrder[total_chans - 1] = tmp;
       }

       // Handle size
       if (channelSize > 1)
              for (i = 0; i < total_chans; i++) {
                     ComponentStartingOrder[i] *= channelSize;
              }
}



//  On planar configurations, the distance is the stride added to any non-negative
static
void ComputeIncrementsForPlanar(cmsUInt32Number Format, 
                                cmsUInt32Number BytesPerPlane,
                                cmsUInt32Number* nChannels,
                                cmsUInt32Number* nAlpha,
                                cmsUInt32Number ComponentStartingOrder[], 
                                cmsUInt32Number ComponentPointerIncrements[])
{
       int extra = T_EXTRA(Format);
       int channels = T_CHANNELS(Format);
       int total_chans = channels + extra;
       int i;
       int channelSize = trueBytesSize(Format);
       
       // Setup the counts
       if (nChannels != NULL) 
              *nChannels = channels;

       if (nAlpha != NULL) 
              *nAlpha = extra;

       // Separation is independent of starting point and only depends on channel size
       for (i = 0; i < total_chans; i++)
              ComponentPointerIncrements[i] = channelSize;

       // Handle do swap
       for (i = 0; i < total_chans; i++)
       {
              if (T_DOSWAP(Format)) {
                     ComponentStartingOrder[i] = total_chans - i - 1;
              }
              else {
                     ComponentStartingOrder[i] = i;
              }
       }

       // Handle swap first (ROL of positions), example CMYK -> KCMY | 0123 -> 3012
       if (T_SWAPFIRST(Format)) {

              cmsUInt32Number tmp = ComponentStartingOrder[0];
              for (i = 0; i < total_chans - 1; i++)
                     ComponentStartingOrder[i] = ComponentStartingOrder[i + 1];

              ComponentStartingOrder[total_chans - 1] = tmp;
       }

       // Handle size
       for (i = 0; i < total_chans; i++) {
              ComponentStartingOrder[i] *= BytesPerPlane;
       }
}



// Dispatcher por chunky and planar RGB
void  _cmsComputeComponentIncrements(cmsUInt32Number Format,
                                     cmsUInt32Number BytesPerPlane,
                                     cmsUInt32Number* nChannels,
                                     cmsUInt32Number* nAlpha,
                                     cmsUInt32Number ComponentStartingOrder[], 
                                     cmsUInt32Number ComponentPointerIncrements[])
{
       if (T_PLANAR(Format)) {

              ComputeIncrementsForPlanar(Format,  BytesPerPlane, nChannels, nAlpha, ComponentStartingOrder, ComponentPointerIncrements);
       }
       else {
              ComputeIncrementsForChunky(Format,  BytesPerPlane, nChannels, nAlpha, ComponentStartingOrder, ComponentPointerIncrements);
       }

}