1// Copyright (c) 2012 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#ifndef REMOTING_BASE_UTIL_H_
6#define REMOTING_BASE_UTIL_H_
7
8#include <string>
9
10#include "media/base/video_frame.h"
11#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
12
13namespace remoting {
14
15// Return a string that contains the current date formatted as 'MMDD/HHMMSS:'.
16std::string GetTimestampString();
17
18// Calculate the offset of a specific pixel in an RGB32 buffer.
19int CalculateRGBOffset(int x, int y, int stride);
20
21// Calculate the offset of a specific pixel in a YV12/YUV420 buffer. Note that
22// the X and Y coordinates must both be even owing to the YV12 buffer layout.
23int CalculateYOffset(int x, int y, int stride);
24int CalculateUVOffset(int x, int y, int stride);
25
26// Convert and scale YUV to RGB32 on a specific rectangle. The source and
27// destination buffers are assumed to contain only |source_buffer_rect| and
28// |dest_buffer_rect| areas correspondingly. The scaling factor is determined
29// as ratio between |dest_size| and |source_size|. The target rectangle
30// |dect_rect| is specified in the destination coordinates.
31//
32// |source_buffer_rect| and |dest_buffer_rect| must fall entirely within
33// the source and destination dimensions, respectively. |dest_rect| must be
34// completely contained within the source and destinations buffers boundaries
35// including the case when scaling is requested.
36//
37// N.B. The top left corner coordinates of YUV buffer should have even X and Y
38// coordinates.
39void ConvertAndScaleYUVToRGB32Rect(
40    const uint8* source_yplane,
41    const uint8* source_uplane,
42    const uint8* source_vplane,
43    int source_ystride,
44    int source_uvstride,
45    const webrtc::DesktopSize& source_size,
46    const webrtc::DesktopRect& source_buffer_rect,
47    uint8* dest_buffer,
48    int dest_stride,
49    const webrtc::DesktopSize& dest_size,
50    const webrtc::DesktopRect& dest_buffer_rect,
51    const webrtc::DesktopRect& dest_rect);
52
53int RoundToTwosMultiple(int x);
54
55// Align the sides of the rectangle to multiples of 2 (expanding outwards).
56webrtc::DesktopRect AlignRect(const webrtc::DesktopRect& rect);
57
58// Scales the supplied rectangle from |in_size| coordinates to |out_size|.
59// If the result has non-integer coordinates then the smallest integer-
60// coordinate rectangle that wholly encloses it is returned.
61webrtc::DesktopRect ScaleRect(const webrtc::DesktopRect& rect,
62                              const webrtc::DesktopSize& in_size,
63                              const webrtc::DesktopSize& out_size);
64
65// Copy content of a rectangle in a RGB32 image.
66void CopyRGB32Rect(const uint8* source_buffer,
67                   int source_stride,
68                   const webrtc::DesktopRect& source_buffer_rect,
69                   uint8* dest_buffer,
70                   int dest_stride,
71                   const webrtc::DesktopRect& dest_buffer_rect,
72                   const webrtc::DesktopRect& dest_rect);
73
74// Replaces every occurrence of "\n" in a string by "\r\n".
75std::string ReplaceLfByCrLf(const std::string& in);
76
77// Replaces every occurrence of "\r\n" in a string by "\n".
78std::string ReplaceCrLfByLf(const std::string& in);
79
80// Checks if the given string is a valid UTF-8 string.
81bool StringIsUtf8(const char* data, size_t length);
82
83bool DoesRectContain(const webrtc::DesktopRect& a,
84                     const webrtc::DesktopRect& b);
85
86}  // namespace remoting
87
88#endif  // REMOTING_BASE_UTIL_H_
89