OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/api/web_contents_capture_client.h" | 5 #include "extensions/browser/api/web_contents_capture_client.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "content/public/browser/render_widget_host.h" | 9 #include "content/public/browser/render_widget_host.h" |
10 #include "content/public/browser/render_widget_host_view.h" | 10 #include "content/public/browser/render_widget_host_view.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // TODO(wjmaclean) can this be static? | 89 // TODO(wjmaclean) can this be static? |
90 bool WebContentsCaptureClient::EncodeBitmap(const SkBitmap& bitmap, | 90 bool WebContentsCaptureClient::EncodeBitmap(const SkBitmap& bitmap, |
91 std::string* base64_result) { | 91 std::string* base64_result) { |
92 DCHECK(base64_result); | 92 DCHECK(base64_result); |
93 std::vector<unsigned char> data; | 93 std::vector<unsigned char> data; |
94 const bool should_discard_alpha = !ClientAllowsTransparency(); | 94 const bool should_discard_alpha = !ClientAllowsTransparency(); |
95 bool encoded = false; | 95 bool encoded = false; |
96 std::string mime_type; | 96 std::string mime_type; |
97 switch (image_format_) { | 97 switch (image_format_) { |
98 case api::extension_types::IMAGE_FORMAT_JPEG: | 98 case api::extension_types::IMAGE_FORMAT_JPEG: |
99 encoded = gfx::JPEGCodec::Encode( | 99 encoded = gfx::JPEGCodec::Encode(bitmap, image_quality_, &data); |
100 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
101 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | |
102 static_cast<int>(bitmap.rowBytes()), image_quality_, &data); | |
103 mime_type = kMimeTypeJpeg; | 100 mime_type = kMimeTypeJpeg; |
104 break; | 101 break; |
105 case api::extension_types::IMAGE_FORMAT_PNG: | 102 case api::extension_types::IMAGE_FORMAT_PNG: |
106 encoded = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, should_discard_alpha, | 103 encoded = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, should_discard_alpha, |
107 &data); | 104 &data); |
108 mime_type = kMimeTypePng; | 105 mime_type = kMimeTypePng; |
109 break; | 106 break; |
110 default: | 107 default: |
111 NOTREACHED() << "Invalid image format."; | 108 NOTREACHED() << "Invalid image format."; |
112 } | 109 } |
113 | 110 |
114 if (!encoded) | 111 if (!encoded) |
115 return false; | 112 return false; |
116 | 113 |
117 base::StringPiece stream_as_string(reinterpret_cast<const char*>(data.data()), | 114 base::StringPiece stream_as_string(reinterpret_cast<const char*>(data.data()), |
118 data.size()); | 115 data.size()); |
119 | 116 |
120 base::Base64Encode(stream_as_string, base64_result); | 117 base::Base64Encode(stream_as_string, base64_result); |
121 base64_result->insert( | 118 base64_result->insert( |
122 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); | 119 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); |
123 | 120 |
124 return true; | 121 return true; |
125 } | 122 } |
126 | 123 |
127 } // namespace extensions | 124 } // namespace extensions |
OLD | NEW |