1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "qt_pixmapruntime.h"
#include "APICast.h"
#include "APIShims.h"
#include "CachedImage.h"
#include "HTMLImageElement.h"
#include "ImageData.h"
#include "IntSize.h"
#include "JSDOMBinding.h"
#include "JSGlobalObject.h"
#include "JSHTMLImageElement.h"
#include "JSImageData.h"
#include "JSRetainPtr.h"
#include "JavaScript.h"
#include "StillImageQt.h"
#include <QBuffer>
#include <QByteArray>
#include <QColor>
#include <QImage>
#include <QPixmap>
#include <QVariant>
#include <QtEndian>
using namespace WebCore;
namespace JSC {
namespace Bindings {
static void copyPixelsInto(const QImage& sourceImage, int width, int height, unsigned char* destPixels)
{
QImage image(sourceImage);
switch (image.format()) {
case QImage::Format_RGB888:
for (int y = 0; y < height; y++) {
const uchar* scanLine = image.scanLine(y);
for (int x = 0; x < width; x++) {
*(destPixels++) = *(scanLine++);
*(destPixels++) = *(scanLine++);
*(destPixels++) = *(scanLine++);
*(destPixels++) = 0xFF;
}
}
break;
default:
image = image.convertToFormat(QImage::Format_ARGB32);
// Fall through
case QImage::Format_RGB32:
case QImage::Format_ARGB32:
for (int y = 0; y < height; y++) {
const quint32* scanLine = reinterpret_cast_ptr<const quint32*>(image.scanLine(y));
for (int x = 0; x < width; x++) {
QRgb pixel = scanLine[x];
qToBigEndian<quint32>((pixel << 8) | qAlpha(pixel), destPixels);
destPixels += 4;
}
}
break;
}
}
static QPixmap toPixmap(const QVariant& data)
{
if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QPixmap>()))
return data.value<QPixmap>();
if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QImage>()))
return QPixmap::fromImage(data.value<QImage>());
return QPixmap();
}
static QImage toImage(const QVariant& data)
{
if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QImage>()))
return data.value<QImage>();
if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QPixmap>()))
return data.value<QPixmap>().toImage();
return QImage();
}
static QSize imageSizeForVariant(const QVariant& data)
{
if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QPixmap>()))
return data.value<QPixmap>().size();
if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QImage>()))
return data.value<QImage>().size();
return QSize(0, 0);
}
static JSValueRef getPixmapWidth(JSContextRef context, JSObjectRef object, JSStringRef, JSValueRef*)
{
QVariant& data = *static_cast<QVariant*>(JSObjectGetPrivate(object));
return JSValueMakeNumber(context, imageSizeForVariant(data).width());
}
static JSValueRef getPixmapHeight(JSContextRef context, JSObjectRef object, JSStringRef, JSValueRef*)
{
QVariant& data = *static_cast<QVariant*>(JSObjectGetPrivate(object));
return JSValueMakeNumber(context, imageSizeForVariant(data).height());
}
static JSValueRef assignToHTMLImageElement(JSContextRef context, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
if (!argumentCount)
return JSValueMakeUndefined(context);
JSObjectRef objectArg = JSValueToObject(context, arguments[0], exception);
if (!objectArg)
return JSValueMakeUndefined(context);
JSObject* jsObject = ::toJS(objectArg);
if (!jsObject->inherits(&JSHTMLImageElement::s_info))
return JSValueMakeUndefined(context);
QVariant& data = *static_cast<QVariant*>(JSObjectGetPrivate(object));
// We now know that we have a valid <img> element as the argument, we can attach the pixmap to it.
RefPtr<StillImage> stillImage = WebCore::StillImage::create(toPixmap(data));
HTMLImageElement* imageElement = toHTMLImageElement(static_cast<JSHTMLImageElement*>(jsObject)->impl());
imageElement->setCachedImage(new CachedImage(stillImage.get()));
return JSValueMakeUndefined(context);
}
static JSValueRef pixmapToImageData(JSContextRef context, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
QVariant& data = *static_cast<QVariant*>(JSObjectGetPrivate(object));
QImage image = toImage(data);
int width = image.width();
int height = image.height();
JSC::ExecState* exec = ::toJS(context);
APIEntryShim entryShim(exec);
RefPtr<ImageData> imageData = ImageData::create(IntSize(width, height));
copyPixelsInto(image, width, height, imageData->data()->data());
JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
return ::toRef(exec, toJS(exec, globalObject, imageData.get()));
}
static JSValueRef pixmapToDataUrl(JSContextRef context, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
QVariant& data = *static_cast<QVariant*>(JSObjectGetPrivate(object));
QByteArray byteArray;
QBuffer buffer(&byteArray);
toImage(data).save(&buffer, "PNG");
QByteArray encoded = QByteArray("data:image/png;base64,") + byteArray.toBase64();
JSRetainPtr<JSStringRef> str(Adopt, JSStringCreateWithUTF8CString(encoded.constData()));
JSValueRef value = JSValueMakeString(context, str.get());
return value;
}
static JSValueRef pixmapToString(JSContextRef context, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
QVariant& data = *static_cast<QVariant*>(JSObjectGetPrivate(object));
QSize size = imageSizeForVariant(data);
QString stringValue = QString::fromLatin1("[Qt Native Pixmap %1,%2]").arg(size.width()).arg(size.height());
JSRetainPtr<JSStringRef> str(Adopt, JSStringCreateWithUTF8CString(stringValue.toUtf8().constData()));
JSValueRef value = JSValueMakeString(context, str.get());
return value;
}
static void finalizePixmap(JSObjectRef object)
{
delete static_cast<QVariant*>(JSObjectGetPrivate(object));
}
JSObjectRef QtPixmapRuntime::toJS(JSContextRef context, const QVariant& value, JSValueRef* exception)
{
return JSObjectMake(context, getClassRef(), new QVariant(value));
}
static QVariant emptyVariantForHint(QMetaType::Type hint)
{
if (hint == qMetaTypeId<QPixmap>())
return QVariant::fromValue(QPixmap());
if (hint == qMetaTypeId<QImage>())
return QVariant::fromValue(QImage());
return QVariant();
}
QVariant QtPixmapRuntime::toQt(JSContextRef context, JSObjectRef obj, QMetaType::Type hint, JSValueRef* exception)
{
if (!obj)
return emptyVariantForHint(hint);
if (JSValueIsObjectOfClass(context, obj, QtPixmapRuntime::getClassRef())) {
QVariant* originalVariant = static_cast<QVariant*>(JSObjectGetPrivate(obj));
if (hint == qMetaTypeId<QPixmap>())
return QVariant::fromValue<QPixmap>(toPixmap(*originalVariant));
if (hint == qMetaTypeId<QImage>())
return QVariant::fromValue<QImage>(toImage(*originalVariant));
}
JSObject* jsObject = ::toJS(obj);
if (!jsObject->inherits(&JSHTMLImageElement::s_info))
return emptyVariantForHint(hint);
JSHTMLImageElement* elementJSWrapper = static_cast<JSHTMLImageElement*>(jsObject);
HTMLImageElement* imageElement = toHTMLImageElement(elementJSWrapper->impl());
if (!imageElement)
return emptyVariantForHint(hint);
CachedImage* cachedImage = imageElement->cachedImage();
if (!cachedImage)
return emptyVariantForHint(hint);
Image* image = cachedImage->imageForRenderer(imageElement->renderer());
if (!image)
return emptyVariantForHint(hint);
QPixmap* pixmap = image->nativeImageForCurrentFrame();
if (!pixmap)
return emptyVariantForHint(hint);
return (hint == static_cast<QMetaType::Type>(qMetaTypeId<QPixmap>()))
? QVariant::fromValue<QPixmap>(*pixmap)
: QVariant::fromValue<QImage>(pixmap->toImage());
}
bool QtPixmapRuntime::canHandle(QMetaType::Type hint)
{
return hint == qMetaTypeId<QImage>() || hint == qMetaTypeId<QPixmap>();
}
JSClassRef QtPixmapRuntime::getClassRef()
{
static const JSStaticValue staticValues[] = {
{ "width", getPixmapWidth, 0, 0 },
{ "height", getPixmapHeight, 0, 0 },
{ 0, 0, 0, 0}
};
static const JSStaticFunction staticFunctions[] = {
{ "assignToHTMLImageElement", assignToHTMLImageElement, 0 },
{ "toDataUrl", pixmapToDataUrl, 0 },
{ "toImageData", pixmapToImageData, 0 },
{ "toString", pixmapToString, 0 },
{ 0, 0, 0 }
};
static const JSClassDefinition classDefinition = {
0, 0, "QtPixmapRuntimeObject", 0, staticValues, staticFunctions,
0, finalizePixmap, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static JSClassRef classRef = JSClassCreate(&classDefinition);
return classRef;
}
}
}
|