Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Unified Diff: media/capture/video/blob_utils.cc

Issue 2354783002: ImageCapture: Implement takePhoto() for Windows (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/capture/video/blob_utils.cc
diff --git a/media/capture/video/blob_utils.cc b/media/capture/video/blob_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..81f597857439d6249d5982e831716ff90a1ee0a4
--- /dev/null
+++ b/media/capture/video/blob_utils.cc
@@ -0,0 +1,70 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/capture/video/blob_utils.h"
+
xianglu 2016/09/20 18:43:03 nit: redundant line
mcasas 2016/09/20 19:51:10 ? l.5 needs to be separated [1], and I should incl
xianglu 2016/09/20 20:42:58 I thought l.6 was redundant.
+#include "media/base/video_capture_types.h"
+#include "media/base/video_frame.h"
+#include "third_party/libyuv/include/libyuv.h"
+#include "third_party/skia/include/core/SkImage.h"
+#include "ui/gfx/codec/png_codec.h"
+
+namespace media {
+
+mojom::BlobPtr Blobify(const uint8_t* buffer,
+ const uint32_t bytesused,
+ VideoCaptureFormat capture_format) {
+ DCHECK(buffer);
+ DCHECK(bytesused);
+ DCHECK(capture_format.IsValid());
+
+ const VideoPixelFormat pixel_format = capture_format.pixel_format;
+ if (pixel_format == VideoPixelFormat::PIXEL_FORMAT_MJPEG) {
+ mojom::BlobPtr blob = mojom::Blob::New();
+ blob->data.resize(bytesused);
+ memcpy(blob->data.data(), buffer, bytesused);
+ blob->mime_type = "image/jpeg";
+ return blob;
+ }
+
+ uint32_t src_format;
+ if (pixel_format == VideoPixelFormat::PIXEL_FORMAT_UYVY)
+ src_format = libyuv::FOURCC_UYVY;
+ else if (pixel_format == VideoPixelFormat::PIXEL_FORMAT_YUY2)
+ src_format = libyuv::FOURCC_YUY2;
+ else if (pixel_format == VideoPixelFormat::PIXEL_FORMAT_I420)
+ src_format = libyuv::FOURCC_I420;
+ else if (pixel_format == VideoPixelFormat::PIXEL_FORMAT_RGB24)
+ src_format = libyuv::FOURCC_24BG;
+ else
+ return nullptr;
+
+ // PNGCodec does not support YUV formats, convert to a temporary ARGB buffer.
+ std::unique_ptr<uint8_t[]> tmp_argb(new uint8_t[VideoFrame::AllocationSize(
xianglu 2016/09/20 18:43:03 Since it's used several times below, maybe you can
mcasas 2016/09/20 19:51:10 Done.
+ PIXEL_FORMAT_ARGB, capture_format.frame_size)]);
+ if (ConvertToARGB(buffer, bytesused, tmp_argb.get(),
+ capture_format.frame_size.width() * 4, 0 /* crop_x_pos */,
+ 0 /* crop_y_pos */, capture_format.frame_size.width(),
+ capture_format.frame_size.height(),
+ capture_format.frame_size.width(),
+ capture_format.frame_size.height(),
+ libyuv::RotationMode::kRotate0, src_format) != 0) {
+ return nullptr;
+ }
+
+ mojom::BlobPtr blob = mojom::Blob::New();
+ const gfx::PNGCodec::ColorFormat codec_color_format =
+ (kN32_SkColorType == kRGBA_8888_SkColorType) ? gfx::PNGCodec::FORMAT_RGBA
+ : gfx::PNGCodec::FORMAT_BGRA;
+ const bool result = gfx::PNGCodec::Encode(
+ tmp_argb.get(), codec_color_format, capture_format.frame_size,
+ capture_format.frame_size.width() * 4, true /* discard_transparency */,
+ std::vector<gfx::PNGCodec::Comment>(), &blob->data);
+ DCHECK(result);
+
+ blob->mime_type = "image/png";
+ return blob;
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698