content/canvas/src/ImageData.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/src/ImageData.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 et tw=78: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "mozilla/dom/ImageData.h"
    1.11 +
    1.12 +#include "mozilla/CheckedInt.h"
    1.13 +#include "mozilla/HoldDropJSObjects.h"
    1.14 +#include "mozilla/dom/ImageDataBinding.h"
    1.15 +
    1.16 +#include "jsapi.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +namespace dom {
    1.20 +
    1.21 +NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData)
    1.22 +NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData)
    1.23 +
    1.24 +NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData)
    1.25 +
    1.26 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData)
    1.27 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.28 +NS_INTERFACE_MAP_END
    1.29 +
    1.30 +NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData)
    1.31 +  NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData)
    1.32 +NS_IMPL_CYCLE_COLLECTION_TRACE_END
    1.33 +
    1.34 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData)
    1.35 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
    1.36 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
    1.37 +
    1.38 +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
    1.39 +  tmp->DropData();
    1.40 +NS_IMPL_CYCLE_COLLECTION_UNLINK_END
    1.41 +
    1.42 +//static
    1.43 +ImageData*
    1.44 +ImageData::Constructor(const GlobalObject& aGlobal,
    1.45 +                       const uint32_t aWidth,
    1.46 +                       const uint32_t aHeight,
    1.47 +                       ErrorResult& aRv)
    1.48 +{
    1.49 +  if (aWidth == 0 || aHeight == 0) {
    1.50 +    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
    1.51 +    return nullptr;
    1.52 +  }
    1.53 +  CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
    1.54 +  if (!length.isValid()) {
    1.55 +    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
    1.56 +    return nullptr;
    1.57 +  }
    1.58 +  js::AssertSameCompartment(aGlobal.GetContext(), aGlobal.Get());
    1.59 +  JSObject* data = Uint8ClampedArray::Create(aGlobal.GetContext(),
    1.60 +                                             length.value());
    1.61 +  if (!data) {
    1.62 +    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    1.63 +    return nullptr;
    1.64 +  }
    1.65 +  return new ImageData(aWidth, aHeight, *data);
    1.66 +}
    1.67 +
    1.68 +//static
    1.69 +ImageData*
    1.70 +ImageData::Constructor(const GlobalObject& aGlobal,
    1.71 +                       const Uint8ClampedArray& aData,
    1.72 +                       const uint32_t aWidth,
    1.73 +                       const Optional<uint32_t>& aHeight,
    1.74 +                       ErrorResult& aRv)
    1.75 +{
    1.76 +  aData.ComputeLengthAndData();
    1.77 +
    1.78 +  uint32_t length = aData.Length();
    1.79 +  if (length == 0 || length % 4) {
    1.80 +    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
    1.81 +    return nullptr;
    1.82 +  }
    1.83 +  length /= 4;
    1.84 +  if (aWidth == 0) {
    1.85 +    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
    1.86 +    return nullptr;
    1.87 +  }
    1.88 +  uint32_t height = length / aWidth;
    1.89 +  if (length != aWidth * height ||
    1.90 +      (aHeight.WasPassed() && aHeight.Value() != height)) {
    1.91 +    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
    1.92 +    return nullptr;
    1.93 +  }
    1.94 +  return new ImageData(aWidth, height, *aData.Obj());
    1.95 +}
    1.96 +
    1.97 +void
    1.98 +ImageData::HoldData()
    1.99 +{
   1.100 +  mozilla::HoldJSObjects(this);
   1.101 +}
   1.102 +
   1.103 +void
   1.104 +ImageData::DropData()
   1.105 +{
   1.106 +  if (mData) {
   1.107 +    mData = nullptr;
   1.108 +    mozilla::DropJSObjects(this);
   1.109 +  }
   1.110 +}
   1.111 +
   1.112 +JSObject*
   1.113 +ImageData::WrapObject(JSContext* cx)
   1.114 +{
   1.115 +  return ImageDataBinding::Wrap(cx, this);
   1.116 +}
   1.117 +
   1.118 +} // namespace dom
   1.119 +} // namespace mozilla

mercurial