OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The LibYuv Project Authors. All rights reserved. | 2 * Copyright 2015 The LibYuv Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 memset(orig_y, y, kPixels); | 217 memset(orig_y, y, kPixels); |
218 | 218 |
219 /* YUV converted to ARGB. */ | 219 /* YUV converted to ARGB. */ |
220 J400ToARGB(orig_y, kWidth, orig_pixels, kWidth * 4, kWidth, kHeight); | 220 J400ToARGB(orig_y, kWidth, orig_pixels, kWidth * 4, kWidth, kHeight); |
221 | 221 |
222 *b = orig_pixels[0]; | 222 *b = orig_pixels[0]; |
223 *g = orig_pixels[1]; | 223 *g = orig_pixels[1]; |
224 *r = orig_pixels[2]; | 224 *r = orig_pixels[2]; |
225 } | 225 } |
226 | 226 |
227 // Pick a method for clamping. | |
228 #define CLAMPMETHOD_IF 1 | |
229 // #define CLAMPMETHOD_TABLE 1 | |
230 // #define CLAMPMETHOD_TERNARY 1 | |
231 // #define CLAMPMETHOD_MASK 1 | |
232 | |
233 // Pick a method for rounding. | 227 // Pick a method for rounding. |
234 #define ROUND(f) static_cast<int>(f + 0.5) | 228 #define ROUND(f) static_cast<int>(f + 0.5) |
235 // #define ROUND(f) lrintf(f) | |
236 // #define ROUND(f) static_cast<int>(round(f)) | |
237 // #define ROUND(f) _mm_cvt_ss2si(_mm_load_ss(&f)) | |
238 | |
239 #if defined(CLAMPMETHOD_IF) | |
240 static int RoundToByte(float f) { | 229 static int RoundToByte(float f) { |
241 int i = ROUND(f); | 230 int i = ROUND(f); |
242 if (i < 0) { | 231 if (i < 0) { |
243 i = 0; | 232 i = 0; |
244 } | 233 } |
245 if (i > 255) { | 234 if (i > 255) { |
246 i = 255; | 235 i = 255; |
247 } | 236 } |
248 return i; | 237 return i; |
249 } | 238 } |
250 #elif defined(CLAMPMETHOD_TABLE) | |
251 static const unsigned char clamptable[811] = { | |
252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
259 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | |
263 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, | |
264 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, | |
265 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, | |
266 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, | |
267 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, | |
268 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, | |
269 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, | |
270 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, | |
271 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, | |
272 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, | |
273 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, | |
274 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, | |
275 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, | |
276 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, | |
277 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, | |
278 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
279 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
280 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
281 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
282 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
283 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
284 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
285 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
286 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
287 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
288 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
289 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
290 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
291 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
292 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
293 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
294 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
295 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
296 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 | |
297 }; | |
298 | |
299 static int RoundToByte(float f) { | |
300 return clamptable[ROUND(f) + 276]; | |
301 } | |
302 #elif defined(CLAMPMETHOD_TERNARY) | |
303 static int RoundToByte(float f) { | |
304 int i = ROUND(f); | |
305 return (i < 0) ? 0 : ((i > 255) ? 255 : i); | |
306 } | |
307 #elif defined(CLAMPMETHOD_MASK) | |
308 static int RoundToByte(float f) { | |
309 int i = ROUND(f); | |
310 i = ((-(i) >> 31) & (i)); // clamp to 0. | |
311 return (((255 - (i)) >> 31) | (i)) & 255; // clamp to 255. | |
312 } | |
313 #endif | |
314 | 239 |
315 #define RANDOM256(s) ((s & 1) ? ((s >> 1) ^ 0xb8) : (s >> 1)) | 240 #define RANDOM256(s) ((s & 1) ? ((s >> 1) ^ 0xb8) : (s >> 1)) |
316 | 241 |
317 TEST_F(libyuvTest, ScaleTestRoundToByte) { | |
318 int allb = 0; | |
319 for (int i = 0; i < benchmark_iterations_; ++i) { | |
320 for (int u2 = 0; u2 < 256; ++u2) { | |
321 for (int v2 = 0; v2 < 256; ++v2) { | |
322 for (int y2 = 0; y2 < 256; ++y2) { | |
323 int y = RANDOM256(y2); | |
324 int b = RoundToByte(y * 810.33 - 257); | |
325 allb |= b; | |
326 } | |
327 } | |
328 } | |
329 } | |
330 EXPECT_GE(allb, 0); | |
331 EXPECT_LE(allb, 255); | |
332 } | |
333 | |
334 static void YUVToRGBReference(int y, int u, int v, int* r, int* g, int* b) { | 242 static void YUVToRGBReference(int y, int u, int v, int* r, int* g, int* b) { |
335 *r = RoundToByte((y - 16) * 1.164 - (v - 128) * -1.596); | 243 *r = RoundToByte((y - 16) * 1.164 - (v - 128) * -1.596); |
336 *g = RoundToByte((y - 16) * 1.164 - (u - 128) * 0.391 - (v - 128) * 0.813); | 244 *g = RoundToByte((y - 16) * 1.164 - (u - 128) * 0.391 - (v - 128) * 0.813); |
337 *b = RoundToByte((y - 16) * 1.164 - (u - 128) * -2.018); | 245 *b = RoundToByte((y - 16) * 1.164 - (u - 128) * -2.018); |
338 } | 246 } |
339 | 247 |
340 static void YUVJToRGBReference(int y, int u, int v, int* r, int* g, int* b) { | 248 static void YUVJToRGBReference(int y, int u, int v, int* r, int* g, int* b) { |
341 *r = RoundToByte(y - (v - 128) * -1.40200); | 249 *r = RoundToByte(y - (v - 128) * -1.40200); |
342 *g = RoundToByte(y - (u - 128) * 0.34414 - (v - 128) * 0.71414); | 250 *g = RoundToByte(y - (u - 128) * 0.34414 - (v - 128) * 0.71414); |
343 *b = RoundToByte(y - (u - 128) * -1.77200); | 251 *b = RoundToByte(y - (u - 128) * -1.77200); |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 EXPECT_EQ(r0, r1); | 461 EXPECT_EQ(r0, r1); |
554 EXPECT_EQ(g0, g1); | 462 EXPECT_EQ(g0, g1); |
555 EXPECT_EQ(b0, b1); | 463 EXPECT_EQ(b0, b1); |
556 EXPECT_EQ(r0, r2); | 464 EXPECT_EQ(r0, r2); |
557 EXPECT_EQ(g0, g2); | 465 EXPECT_EQ(g0, g2); |
558 EXPECT_EQ(b0, b2); | 466 EXPECT_EQ(b0, b2); |
559 } | 467 } |
560 } | 468 } |
561 | 469 |
562 } // namespace libyuv | 470 } // namespace libyuv |
OLD | NEW |