~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/image-encoders/skia/WEBPImageEncoder.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2011, Google Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are
 
6
 * met:
 
7
 *
 
8
 *     * Redistributions of source code must retain the above copyright
 
9
 * notice, this list of conditions and the following disclaimer.
 
10
 *     * Redistributions in binary form must reproduce the above
 
11
 * copyright notice, this list of conditions and the following disclaimer
 
12
 * in the documentation and/or other materials provided with the
 
13
 * distribution.
 
14
 *     * Neither the name of Google Inc. nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from
 
16
 * this software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
#include "config.h"
 
32
#include "WEBPImageEncoder.h"
 
33
 
 
34
#if USE(WEBP)
 
35
 
 
36
#include "ImageData.h"
 
37
#include "IntSize.h"
 
38
#include "SkBitmap.h"
 
39
#include "SkColorPriv.h"
 
40
#include "webp/encode.h"
 
41
 
 
42
typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int rowStride);
 
43
 
 
44
namespace WebCore {
 
45
 
 
46
static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* const picture)
 
47
{
 
48
    static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size);
 
49
    return 1;
 
50
}
 
51
 
 
52
static bool rgbPictureImport(const unsigned char* pixels, bool premultiplied, WebPImporter importRGBX, WebPImporter importRGB, WebPPicture* picture)
 
53
{
 
54
    if (premultiplied)
 
55
        return importRGBX(picture, pixels, picture->width * 4);
 
56
 
 
57
    // Write the RGB pixels to an rgb data buffer, alpha premultiplied, then import the rgb data.
 
58
 
 
59
    Vector<unsigned char> rgb;
 
60
    size_t pixelCount = picture->height * picture->width;
 
61
    rgb.reserveInitialCapacity(pixelCount * 3);
 
62
 
 
63
    for (unsigned char* data = rgb.data(); pixelCount-- > 0; pixels += 4) {
 
64
        unsigned char alpha = pixels[3];
 
65
        if (alpha != 255) {
 
66
            *data++ = SkMulDiv255Round(pixels[0], alpha);
 
67
            *data++ = SkMulDiv255Round(pixels[1], alpha);
 
68
            *data++ = SkMulDiv255Round(pixels[2], alpha);
 
69
        } else {
 
70
            *data++ = pixels[0];
 
71
            *data++ = pixels[1];
 
72
            *data++ = pixels[2];
 
73
        }
 
74
    }
 
75
 
 
76
    return importRGB(picture, rgb.data(), picture->width * 3);
 
77
}
 
78
 
 
79
template <bool Premultiplied> inline bool importPictureBGRX(const unsigned char* pixels, WebPPicture* picture)
 
80
{
 
81
    return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportBGRX, &WebPPictureImportBGR, picture);
 
82
}
 
83
 
 
84
template <bool Premultiplied> inline bool importPictureRGBX(const unsigned char* pixels, WebPPicture* picture)
 
85
{
 
86
    return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportRGBX, &WebPPictureImportRGB, picture);
 
87
}
 
88
 
 
89
static bool encodePixels(IntSize imageSize, const unsigned char* pixels, bool premultiplied, int quality, Vector<unsigned char>* output)
 
90
{
 
91
    WebPConfig config;
 
92
    if (!WebPConfigInit(&config))
 
93
        return false;
 
94
    WebPPicture picture;
 
95
    if (!WebPPictureInit(&picture))
 
96
        return false;
 
97
 
 
98
    imageSize.clampNegativeToZero();
 
99
    if (!imageSize.width() || imageSize.width() > WEBP_MAX_DIMENSION)
 
100
        return false;
 
101
    picture.width = imageSize.width();
 
102
    if (!imageSize.height() || imageSize.height() > WEBP_MAX_DIMENSION)
 
103
        return false;
 
104
    picture.height = imageSize.height();
 
105
 
 
106
    if (premultiplied && !importPictureBGRX<true>(pixels, &picture))
 
107
        return false;
 
108
    if (!premultiplied && !importPictureRGBX<false>(pixels, &picture))
 
109
        return false;
 
110
 
 
111
    picture.custom_ptr = output;
 
112
    picture.writer = &writeOutput;
 
113
    config.quality = quality;
 
114
    config.method = 3;
 
115
 
 
116
    bool success = WebPEncode(&config, &picture);
 
117
    WebPPictureFree(&picture);
 
118
    return success;
 
119
}
 
120
 
 
121
bool WEBPImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsigned char>* output)
 
122
{
 
123
    SkAutoLockPixels bitmapLock(bitmap);
 
124
 
 
125
    if (bitmap.config() != SkBitmap::kARGB_8888_Config)
 
126
        return false; // Only support 32 bit/pixel skia bitmaps.
 
127
 
 
128
    return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<unsigned char *>(bitmap.getPixels()), true, quality, output);
 
129
}
 
130
 
 
131
bool WEBPImageEncoder::encode(const ImageData& imageData, int quality, Vector<unsigned char>* output)
 
132
{
 
133
    return encodePixels(imageData.size(), imageData.data()->data(), false, quality, output);
 
134
}
 
135
 
 
136
} // namespace WebCore
 
137
 
 
138
#endif