OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/test/frame_generator_util.h" |
| 6 |
| 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/path_service.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 12 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" |
| 13 #include "ui/gfx/codec/png_codec.h" |
| 14 |
| 15 namespace remoting { |
| 16 namespace test { |
| 17 |
| 18 namespace { |
| 19 const int kBarcodeCellWidth = 8; |
| 20 const int kBarcodeCellHeight = 8; |
| 21 const int kBarcodeBits = 10; |
| 22 const int kBarcodeBlackThreshold = 85; |
| 23 const int kBarcodeWhiteThreshold = 170; |
| 24 } // namespace |
| 25 |
| 26 std::unique_ptr<webrtc::DesktopFrame> LoadDesktopFrameFromPng( |
| 27 const char* name) { |
| 28 base::FilePath file_path; |
| 29 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 30 file_path = file_path.AppendASCII("remoting"); |
| 31 file_path = file_path.AppendASCII("test"); |
| 32 file_path = file_path.AppendASCII("data"); |
| 33 file_path = file_path.AppendASCII(name); |
| 34 |
| 35 std::string file_content; |
| 36 if (!base::ReadFileToString(file_path, &file_content)) |
| 37 LOG(FATAL) << "Failed to read " << file_path.MaybeAsASCII() |
| 38 << ". Please run remoting/test/data/download.sh"; |
| 39 SkBitmap bitmap; |
| 40 gfx::PNGCodec::Decode(reinterpret_cast<const uint8_t*>(file_content.data()), |
| 41 file_content.size(), &bitmap); |
| 42 std::unique_ptr<webrtc::DesktopFrame> frame(new webrtc::BasicDesktopFrame( |
| 43 webrtc::DesktopSize(bitmap.width(), bitmap.height()))); |
| 44 bitmap.copyPixelsTo(frame->data(), |
| 45 frame->stride() * frame->size().height(), |
| 46 frame->stride()); |
| 47 return frame; |
| 48 } |
| 49 |
| 50 void DrawRect(webrtc::DesktopFrame* frame, |
| 51 webrtc::DesktopRect rect, |
| 52 uint32_t color) { |
| 53 for (int y = rect.top(); y < rect.bottom(); ++y) { |
| 54 uint32_t* data = reinterpret_cast<uint32_t*>( |
| 55 frame->GetFrameDataAtPos(webrtc::DesktopVector(rect.left(), y))); |
| 56 for (int x = 0; x < rect.width(); ++x) { |
| 57 data[x] = color; |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 void DrawBarcode(int value, bool changed, webrtc::DesktopFrame* frame) { |
| 63 CHECK(value < (1 << kBarcodeBits)); |
| 64 for (int i = 0; i < kBarcodeBits; ++i) { |
| 65 DrawRect(frame, webrtc::DesktopRect::MakeXYWH(i * kBarcodeCellWidth, 0, |
| 66 kBarcodeCellWidth, |
| 67 kBarcodeCellHeight), |
| 68 (value & 1) ? 0xffffffff : 0xff000000); |
| 69 value >>= 1; |
| 70 } |
| 71 if (changed) { |
| 72 frame->mutable_updated_region()->AddRect(webrtc::DesktopRect::MakeXYWH( |
| 73 0, 0, kBarcodeCellWidth * kBarcodeBits, kBarcodeCellHeight)); |
| 74 } |
| 75 } |
| 76 |
| 77 int ReadBarcode(const webrtc::DesktopFrame& frame) { |
| 78 int result = 0; |
| 79 for (int i = kBarcodeBits - 1; i >= 0; --i) { |
| 80 // Sample barcode in the center of the cell for each bit. |
| 81 int x = i * kBarcodeCellWidth + kBarcodeCellWidth / 2; |
| 82 int y = kBarcodeCellHeight / 2; |
| 83 uint8_t* data = frame.GetFrameDataAtPos(webrtc::DesktopVector(x, y)); |
| 84 int b = data[0]; |
| 85 int g = data[1]; |
| 86 int r = data[2]; |
| 87 bool bit = 0; |
| 88 if (b > kBarcodeWhiteThreshold && g > kBarcodeWhiteThreshold && |
| 89 r > kBarcodeWhiteThreshold) { |
| 90 bit = 1; |
| 91 } else if (b < kBarcodeBlackThreshold && g < kBarcodeBlackThreshold && |
| 92 r < kBarcodeBlackThreshold) { |
| 93 bit = 0; |
| 94 } else { |
| 95 LOG(FATAL) << "Invalid barcode."; |
| 96 } |
| 97 result <<= 1; |
| 98 if (bit) |
| 99 result |= 1; |
| 100 } |
| 101 return result; |
| 102 } |
| 103 |
| 104 } // namespace test |
| 105 } // namespace remoting |
OLD | NEW |