| 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 88 |
| 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 SkPixmap pixmap; |
| 100 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 100 bool success = bitmap.peekPixels(&pixmap); |
| 101 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | 101 DCHECK(success); |
| 102 static_cast<int>(bitmap.rowBytes()), image_quality_, &data); | 102 encoded = gfx::JPEGCodec::Encode(pixmap, image_quality_, &data); |
| 103 mime_type = kMimeTypeJpeg; | 103 mime_type = kMimeTypeJpeg; |
| 104 break; | 104 break; |
| 105 } |
| 105 case api::extension_types::IMAGE_FORMAT_PNG: | 106 case api::extension_types::IMAGE_FORMAT_PNG: |
| 106 encoded = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, should_discard_alpha, | 107 encoded = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, should_discard_alpha, |
| 107 &data); | 108 &data); |
| 108 mime_type = kMimeTypePng; | 109 mime_type = kMimeTypePng; |
| 109 break; | 110 break; |
| 110 default: | 111 default: |
| 111 NOTREACHED() << "Invalid image format."; | 112 NOTREACHED() << "Invalid image format."; |
| 112 } | 113 } |
| 113 | 114 |
| 114 if (!encoded) | 115 if (!encoded) |
| 115 return false; | 116 return false; |
| 116 | 117 |
| 117 base::StringPiece stream_as_string(reinterpret_cast<const char*>(data.data()), | 118 base::StringPiece stream_as_string(reinterpret_cast<const char*>(data.data()), |
| 118 data.size()); | 119 data.size()); |
| 119 | 120 |
| 120 base::Base64Encode(stream_as_string, base64_result); | 121 base::Base64Encode(stream_as_string, base64_result); |
| 121 base64_result->insert( | 122 base64_result->insert( |
| 122 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); | 123 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); |
| 123 | 124 |
| 124 return true; | 125 return true; |
| 125 } | 126 } |
| 126 | 127 |
| 127 } // namespace extensions | 128 } // namespace extensions |
| OLD | NEW |