1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
4  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_DATA_BUFFER_H_
29 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_DATA_BUFFER_H_
30 
31 #include <memory>
32 #include "base/memory/scoped_refptr.h"
33 #include "third_party/blink/renderer/platform/geometry/float_rect.h"
34 #include "third_party/blink/renderer/platform/geometry/int_size.h"
35 #include "third_party/blink/renderer/platform/graphics/graphics_types.h"
36 #include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
37 #include "third_party/blink/renderer/platform/platform_export.h"
38 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
39 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
40 #include "third_party/blink/renderer/platform/wtf/vector.h"
41 #include "third_party/skia/include/core/SkRefCnt.h"
42 
43 namespace blink {
44 
45 class PLATFORM_EXPORT ImageDataBuffer {
46   USING_FAST_MALLOC(ImageDataBuffer);
47 
48  public:
49   static std::unique_ptr<ImageDataBuffer> Create(
50       scoped_refptr<StaticBitmapImage>);
51   static std::unique_ptr<ImageDataBuffer> Create(const SkPixmap&);
52 
53   String ToDataURL(const ImageEncodingMimeType mime_type,
54                    const double& quality) const;
55   bool EncodeImage(const ImageEncodingMimeType mime_type,
56                    const double& quality,
57                    Vector<unsigned char>* encoded_image) const;
58 
59   const unsigned char* Pixels() const;
size()60   const IntSize& size() const { return size_; }
Height()61   int Height() const { return size_.Height(); }
Width()62   int Width() const { return size_.Width(); }
63 
64  private:
65   ImageDataBuffer(const IntSize&,
66                   const unsigned char*,
67                   const CanvasColorParams&);
68   ImageDataBuffer(const SkPixmap&);
69   ImageDataBuffer(scoped_refptr<StaticBitmapImage>);
70 
IsValid()71   bool IsValid() { return is_valid_; }  // Only used by Create()
72 
73   bool EncodeImageInternal(const ImageEncodingMimeType mime_type,
74                            const double& quality,
75                            Vector<unsigned char>* encoded_image,
76                            const SkPixmap& pixmap) const;
77 
78   sk_sp<SkImage> retained_image_;
79   SkPixmap pixmap_;
80   bool is_valid_ = false;
81   IntSize size_;
82 };
83 
84 }  // namespace blink
85 
86 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_DATA_BUFFER_H_
87