Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #ifndef SkImageDecoder_DEFINED |
michael@0 | 11 | #define SkImageDecoder_DEFINED |
michael@0 | 12 | |
michael@0 | 13 | #include "SkBitmap.h" |
michael@0 | 14 | #include "SkImage.h" |
michael@0 | 15 | #include "SkRect.h" |
michael@0 | 16 | #include "SkRefCnt.h" |
michael@0 | 17 | #include "SkTRegistry.h" |
michael@0 | 18 | #include "SkTypes.h" |
michael@0 | 19 | |
michael@0 | 20 | class SkStream; |
michael@0 | 21 | class SkStreamRewindable; |
michael@0 | 22 | |
michael@0 | 23 | /** \class SkImageDecoder |
michael@0 | 24 | |
michael@0 | 25 | Base class for decoding compressed images into a SkBitmap |
michael@0 | 26 | */ |
michael@0 | 27 | class SkImageDecoder : public SkNoncopyable { |
michael@0 | 28 | public: |
michael@0 | 29 | virtual ~SkImageDecoder(); |
michael@0 | 30 | |
michael@0 | 31 | enum Format { |
michael@0 | 32 | kUnknown_Format, |
michael@0 | 33 | kBMP_Format, |
michael@0 | 34 | kGIF_Format, |
michael@0 | 35 | kICO_Format, |
michael@0 | 36 | kJPEG_Format, |
michael@0 | 37 | kPNG_Format, |
michael@0 | 38 | kWBMP_Format, |
michael@0 | 39 | kWEBP_Format, |
michael@0 | 40 | |
michael@0 | 41 | kLastKnownFormat = kWEBP_Format, |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | /** Return the format of image this decoder can decode. If this decoder can decode multiple |
michael@0 | 45 | formats, kUnknown_Format will be returned. |
michael@0 | 46 | */ |
michael@0 | 47 | virtual Format getFormat() const; |
michael@0 | 48 | |
michael@0 | 49 | /** Return the format of the SkStreamRewindable or kUnknown_Format if it cannot be determined. |
michael@0 | 50 | Rewinds the stream before returning. |
michael@0 | 51 | */ |
michael@0 | 52 | static Format GetStreamFormat(SkStreamRewindable*); |
michael@0 | 53 | |
michael@0 | 54 | /** Return a readable string of the Format provided. |
michael@0 | 55 | */ |
michael@0 | 56 | static const char* GetFormatName(Format); |
michael@0 | 57 | |
michael@0 | 58 | /** Return a readable string of the value returned by getFormat(). |
michael@0 | 59 | */ |
michael@0 | 60 | const char* getFormatName() const; |
michael@0 | 61 | |
michael@0 | 62 | /** Whether the decoder should skip writing zeroes to output if possible. |
michael@0 | 63 | */ |
michael@0 | 64 | bool getSkipWritingZeroes() const { return fSkipWritingZeroes; } |
michael@0 | 65 | |
michael@0 | 66 | /** Set to true if the decoder should skip writing any zeroes when |
michael@0 | 67 | creating the output image. |
michael@0 | 68 | This is a hint that may not be respected by the decoder. |
michael@0 | 69 | It should only be used if it is known that the memory to write |
michael@0 | 70 | to has already been set to 0; otherwise the resulting image will |
michael@0 | 71 | have garbage. |
michael@0 | 72 | This is ideal for images that contain a lot of completely transparent |
michael@0 | 73 | pixels, but may be a performance hit for an image that has only a |
michael@0 | 74 | few transparent pixels. |
michael@0 | 75 | The default is false. |
michael@0 | 76 | */ |
michael@0 | 77 | void setSkipWritingZeroes(bool skip) { fSkipWritingZeroes = skip; } |
michael@0 | 78 | |
michael@0 | 79 | /** Returns true if the decoder should try to dither the resulting image. |
michael@0 | 80 | The default setting is true. |
michael@0 | 81 | */ |
michael@0 | 82 | bool getDitherImage() const { return fDitherImage; } |
michael@0 | 83 | |
michael@0 | 84 | /** Set to true if the the decoder should try to dither the resulting image. |
michael@0 | 85 | The default setting is true. |
michael@0 | 86 | */ |
michael@0 | 87 | void setDitherImage(bool dither) { fDitherImage = dither; } |
michael@0 | 88 | |
michael@0 | 89 | /** Returns true if the decoder should try to decode the |
michael@0 | 90 | resulting image to a higher quality even at the expense of |
michael@0 | 91 | the decoding speed. |
michael@0 | 92 | */ |
michael@0 | 93 | bool getPreferQualityOverSpeed() const { return fPreferQualityOverSpeed; } |
michael@0 | 94 | |
michael@0 | 95 | /** Set to true if the the decoder should try to decode the |
michael@0 | 96 | resulting image to a higher quality even at the expense of |
michael@0 | 97 | the decoding speed. |
michael@0 | 98 | */ |
michael@0 | 99 | void setPreferQualityOverSpeed(bool qualityOverSpeed) { |
michael@0 | 100 | fPreferQualityOverSpeed = qualityOverSpeed; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /** Set to true to require the decoder to return a bitmap with unpremultiplied |
michael@0 | 104 | colors. The default is false, meaning the resulting bitmap will have its |
michael@0 | 105 | colors premultiplied. |
michael@0 | 106 | NOTE: Passing true to this function may result in a bitmap which cannot |
michael@0 | 107 | be properly used by Skia. |
michael@0 | 108 | */ |
michael@0 | 109 | void setRequireUnpremultipliedColors(bool request) { |
michael@0 | 110 | fRequireUnpremultipliedColors = request; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | /** Returns true if the decoder will only return bitmaps with unpremultiplied |
michael@0 | 114 | colors. |
michael@0 | 115 | */ |
michael@0 | 116 | bool getRequireUnpremultipliedColors() const { return fRequireUnpremultipliedColors; } |
michael@0 | 117 | |
michael@0 | 118 | /** \class Peeker |
michael@0 | 119 | |
michael@0 | 120 | Base class for optional callbacks to retrieve meta/chunk data out of |
michael@0 | 121 | an image as it is being decoded. |
michael@0 | 122 | */ |
michael@0 | 123 | class Peeker : public SkRefCnt { |
michael@0 | 124 | public: |
michael@0 | 125 | SK_DECLARE_INST_COUNT(Peeker) |
michael@0 | 126 | |
michael@0 | 127 | /** Return true to continue decoding, or false to indicate an error, which |
michael@0 | 128 | will cause the decoder to not return the image. |
michael@0 | 129 | */ |
michael@0 | 130 | virtual bool peek(const char tag[], const void* data, size_t length) = 0; |
michael@0 | 131 | private: |
michael@0 | 132 | typedef SkRefCnt INHERITED; |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | Peeker* getPeeker() const { return fPeeker; } |
michael@0 | 136 | Peeker* setPeeker(Peeker*); |
michael@0 | 137 | |
michael@0 | 138 | /** \class Chooser |
michael@0 | 139 | |
michael@0 | 140 | Base class for optional callbacks to choose an image from a format that |
michael@0 | 141 | contains multiple images. |
michael@0 | 142 | */ |
michael@0 | 143 | class Chooser : public SkRefCnt { |
michael@0 | 144 | public: |
michael@0 | 145 | SK_DECLARE_INST_COUNT(Chooser) |
michael@0 | 146 | |
michael@0 | 147 | virtual void begin(int count) {} |
michael@0 | 148 | virtual void inspect(int index, SkBitmap::Config config, int width, int height) {} |
michael@0 | 149 | /** Return the index of the subimage you want, or -1 to choose none of them. |
michael@0 | 150 | */ |
michael@0 | 151 | virtual int choose() = 0; |
michael@0 | 152 | |
michael@0 | 153 | private: |
michael@0 | 154 | typedef SkRefCnt INHERITED; |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | Chooser* getChooser() const { return fChooser; } |
michael@0 | 158 | Chooser* setChooser(Chooser*); |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * Optional table describing the caller's preferred config based on |
michael@0 | 162 | * information about the src data. Each field should be set to the |
michael@0 | 163 | * preferred config for a src described in the name of the field. The |
michael@0 | 164 | * src attributes are described in terms of depth (8-index, |
michael@0 | 165 | * 8bit-grayscale, or 8-bits/component) and whether there is per-pixel |
michael@0 | 166 | * alpha (does not apply to grayscale). If the caller has no preference |
michael@0 | 167 | * for a particular src type, its slot should be set to kNo_Config. |
michael@0 | 168 | * |
michael@0 | 169 | * NOTE ABOUT PREFERRED CONFIGS: |
michael@0 | 170 | * If a config is preferred, either using a pref table or as a parameter |
michael@0 | 171 | * to some flavor of decode, it is still at the discretion of the codec |
michael@0 | 172 | * as to what output config is actually returned, as it may not be able |
michael@0 | 173 | * to support the caller's preference. |
michael@0 | 174 | * |
michael@0 | 175 | * If a bitmap is decoded into SkBitmap::A8_Config, the resulting bitmap |
michael@0 | 176 | * will either be a conversion of the grayscale in the case of a |
michael@0 | 177 | * grayscale source or the alpha channel in the case of a source with |
michael@0 | 178 | * an alpha channel. |
michael@0 | 179 | */ |
michael@0 | 180 | struct PrefConfigTable { |
michael@0 | 181 | SkBitmap::Config fPrefFor_8Index_NoAlpha_src; |
michael@0 | 182 | SkBitmap::Config fPrefFor_8Index_YesAlpha_src; |
michael@0 | 183 | SkBitmap::Config fPrefFor_8Gray_src; |
michael@0 | 184 | SkBitmap::Config fPrefFor_8bpc_NoAlpha_src; |
michael@0 | 185 | SkBitmap::Config fPrefFor_8bpc_YesAlpha_src; |
michael@0 | 186 | }; |
michael@0 | 187 | |
michael@0 | 188 | /** |
michael@0 | 189 | * Set an optional table for specifying the caller's preferred config |
michael@0 | 190 | * based on information about the src data. |
michael@0 | 191 | * |
michael@0 | 192 | * The default is no preference, which will assume the config set by |
michael@0 | 193 | * decode is preferred. |
michael@0 | 194 | */ |
michael@0 | 195 | void setPrefConfigTable(const PrefConfigTable&); |
michael@0 | 196 | |
michael@0 | 197 | /** |
michael@0 | 198 | * Do not use a PrefConfigTable to determine the output config. This |
michael@0 | 199 | * is the default, so there is no need to call unless a PrefConfigTable |
michael@0 | 200 | * was previously set. |
michael@0 | 201 | */ |
michael@0 | 202 | void resetPrefConfigTable() { fUsePrefTable = false; } |
michael@0 | 203 | |
michael@0 | 204 | SkBitmap::Allocator* getAllocator() const { return fAllocator; } |
michael@0 | 205 | SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*); |
michael@0 | 206 | |
michael@0 | 207 | // sample-size, if set to > 1, tells the decoder to return a smaller than |
michael@0 | 208 | // original bitmap, sampling 1 pixel for every size pixels. e.g. if sample |
michael@0 | 209 | // size is set to 3, then the returned bitmap will be 1/3 as wide and high, |
michael@0 | 210 | // and will contain 1/9 as many pixels as the original. |
michael@0 | 211 | // Note: this is a hint, and the codec may choose to ignore this, or only |
michael@0 | 212 | // approximate the sample size. |
michael@0 | 213 | int getSampleSize() const { return fSampleSize; } |
michael@0 | 214 | void setSampleSize(int size); |
michael@0 | 215 | |
michael@0 | 216 | /** Reset the sampleSize to its default of 1 |
michael@0 | 217 | */ |
michael@0 | 218 | void resetSampleSize() { this->setSampleSize(1); } |
michael@0 | 219 | |
michael@0 | 220 | /** Decoding is synchronous, but for long decodes, a different thread can |
michael@0 | 221 | call this method safely. This sets a state that the decoders will |
michael@0 | 222 | periodically check, and if they see it changed to cancel, they will |
michael@0 | 223 | cancel. This will result in decode() returning false. However, there is |
michael@0 | 224 | no guarantee that the decoder will see the state change in time, so |
michael@0 | 225 | it is possible that cancelDecode() will be called, but will be ignored |
michael@0 | 226 | and decode() will return true (assuming no other problems were |
michael@0 | 227 | encountered). |
michael@0 | 228 | |
michael@0 | 229 | This state is automatically reset at the beginning of decode(). |
michael@0 | 230 | */ |
michael@0 | 231 | void cancelDecode() { |
michael@0 | 232 | // now the subclass must query shouldCancelDecode() to be informed |
michael@0 | 233 | // of the request |
michael@0 | 234 | fShouldCancelDecode = true; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | /** Passed to the decode method. If kDecodeBounds_Mode is passed, then |
michael@0 | 238 | only the bitmap's width/height/config need be set. If kDecodePixels_Mode |
michael@0 | 239 | is passed, then the bitmap must have pixels or a pixelRef. |
michael@0 | 240 | */ |
michael@0 | 241 | enum Mode { |
michael@0 | 242 | kDecodeBounds_Mode, //!< only return width/height/config in bitmap |
michael@0 | 243 | kDecodePixels_Mode //!< return entire bitmap (including pixels) |
michael@0 | 244 | }; |
michael@0 | 245 | |
michael@0 | 246 | /** Given a stream, decode it into the specified bitmap. |
michael@0 | 247 | If the decoder can decompress the image, it calls bitmap.setConfig(), |
michael@0 | 248 | and then if the Mode is kDecodePixels_Mode, call allocPixelRef(), |
michael@0 | 249 | which will allocated a pixelRef. To access the pixel memory, the codec |
michael@0 | 250 | needs to call lockPixels/unlockPixels on the |
michael@0 | 251 | bitmap. It can then set the pixels with the decompressed image. |
michael@0 | 252 | * If the image cannot be decompressed, return false. After the |
michael@0 | 253 | * decoding, the function converts the decoded config in bitmap |
michael@0 | 254 | * to pref if possible. Whether a conversion is feasible is |
michael@0 | 255 | * tested by Bitmap::canCopyTo(pref). |
michael@0 | 256 | |
michael@0 | 257 | If an SkBitmap::Allocator is installed via setAllocator, it will be |
michael@0 | 258 | used to allocate the pixel memory. A clever allocator can be used |
michael@0 | 259 | to allocate the memory from a cache, volatile memory, or even from |
michael@0 | 260 | an existing bitmap's memory. |
michael@0 | 261 | |
michael@0 | 262 | If a Peeker is installed via setPeeker, it may be used to peek into |
michael@0 | 263 | meta data during the decode. |
michael@0 | 264 | |
michael@0 | 265 | If a Chooser is installed via setChooser, it may be used to select |
michael@0 | 266 | which image to return from a format that contains multiple images. |
michael@0 | 267 | */ |
michael@0 | 268 | bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode); |
michael@0 | 269 | bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) { |
michael@0 | 270 | return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | /** |
michael@0 | 274 | * Given a stream, build an index for doing tile-based decode. |
michael@0 | 275 | * The built index will be saved in the decoder, and the image size will |
michael@0 | 276 | * be returned in width and height. |
michael@0 | 277 | * |
michael@0 | 278 | * Return true for success or false on failure. |
michael@0 | 279 | */ |
michael@0 | 280 | bool buildTileIndex(SkStreamRewindable*, int *width, int *height); |
michael@0 | 281 | |
michael@0 | 282 | /** |
michael@0 | 283 | * Decode a rectangle subset in the image. |
michael@0 | 284 | * The method can only be called after buildTileIndex(). |
michael@0 | 285 | * |
michael@0 | 286 | * Return true for success. |
michael@0 | 287 | * Return false if the index is never built or failing in decoding. |
michael@0 | 288 | */ |
michael@0 | 289 | bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref); |
michael@0 | 290 | |
michael@0 | 291 | SK_ATTR_DEPRECATED("use decodeSubset() instead") |
michael@0 | 292 | bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, SkBitmap::Config pref) { |
michael@0 | 293 | return this->decodeSubset(bitmap, rect, pref); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | /** Given a stream, this will try to find an appropriate decoder object. |
michael@0 | 297 | If none is found, the method returns NULL. |
michael@0 | 298 | */ |
michael@0 | 299 | static SkImageDecoder* Factory(SkStreamRewindable*); |
michael@0 | 300 | |
michael@0 | 301 | /** Decode the image stored in the specified file, and store the result |
michael@0 | 302 | in bitmap. Return true for success or false on failure. |
michael@0 | 303 | |
michael@0 | 304 | @param prefConfig If the PrefConfigTable is not set, prefer this config. |
michael@0 | 305 | See NOTE ABOUT PREFERRED CONFIGS. |
michael@0 | 306 | |
michael@0 | 307 | @param format On success, if format is non-null, it is set to the format |
michael@0 | 308 | of the decoded file. On failure it is ignored. |
michael@0 | 309 | */ |
michael@0 | 310 | static bool DecodeFile(const char file[], SkBitmap* bitmap, |
michael@0 | 311 | SkBitmap::Config prefConfig, Mode, |
michael@0 | 312 | Format* format = NULL); |
michael@0 | 313 | static bool DecodeFile(const char file[], SkBitmap* bitmap) { |
michael@0 | 314 | return DecodeFile(file, bitmap, SkBitmap::kNo_Config, |
michael@0 | 315 | kDecodePixels_Mode, NULL); |
michael@0 | 316 | } |
michael@0 | 317 | /** Decode the image stored in the specified memory buffer, and store the |
michael@0 | 318 | result in bitmap. Return true for success or false on failure. |
michael@0 | 319 | |
michael@0 | 320 | @param prefConfig If the PrefConfigTable is not set, prefer this config. |
michael@0 | 321 | See NOTE ABOUT PREFERRED CONFIGS. |
michael@0 | 322 | |
michael@0 | 323 | @param format On success, if format is non-null, it is set to the format |
michael@0 | 324 | of the decoded buffer. On failure it is ignored. |
michael@0 | 325 | */ |
michael@0 | 326 | static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap, |
michael@0 | 327 | SkBitmap::Config prefConfig, Mode, |
michael@0 | 328 | Format* format = NULL); |
michael@0 | 329 | static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){ |
michael@0 | 330 | return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config, |
michael@0 | 331 | kDecodePixels_Mode, NULL); |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | /** |
michael@0 | 335 | * Struct containing information about a pixel destination. |
michael@0 | 336 | */ |
michael@0 | 337 | struct Target { |
michael@0 | 338 | /** |
michael@0 | 339 | * Pre-allocated memory. |
michael@0 | 340 | */ |
michael@0 | 341 | void* fAddr; |
michael@0 | 342 | |
michael@0 | 343 | /** |
michael@0 | 344 | * Rowbytes of the allocated memory. |
michael@0 | 345 | */ |
michael@0 | 346 | size_t fRowBytes; |
michael@0 | 347 | }; |
michael@0 | 348 | |
michael@0 | 349 | /** Decode the image stored in the specified SkStreamRewindable, and store the result |
michael@0 | 350 | in bitmap. Return true for success or false on failure. |
michael@0 | 351 | |
michael@0 | 352 | @param prefConfig If the PrefConfigTable is not set, prefer this config. |
michael@0 | 353 | See NOTE ABOUT PREFERRED CONFIGS. |
michael@0 | 354 | |
michael@0 | 355 | @param format On success, if format is non-null, it is set to the format |
michael@0 | 356 | of the decoded stream. On failure it is ignored. |
michael@0 | 357 | */ |
michael@0 | 358 | static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap, |
michael@0 | 359 | SkBitmap::Config prefConfig, Mode, |
michael@0 | 360 | Format* format = NULL); |
michael@0 | 361 | static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) { |
michael@0 | 362 | return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, |
michael@0 | 363 | kDecodePixels_Mode, NULL); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | /** Return the default config for the running device. |
michael@0 | 367 | Currently this used as a suggestion to image decoders that need to guess |
michael@0 | 368 | what config they should decode into. |
michael@0 | 369 | Default is kNo_Config, but this can be changed with SetDeviceConfig() |
michael@0 | 370 | */ |
michael@0 | 371 | static SkBitmap::Config GetDeviceConfig(); |
michael@0 | 372 | /** Set the default config for the running device. |
michael@0 | 373 | Currently this used as a suggestion to image decoders that need to guess |
michael@0 | 374 | what config they should decode into. |
michael@0 | 375 | Default is kNo_Config. |
michael@0 | 376 | This can be queried with GetDeviceConfig() |
michael@0 | 377 | */ |
michael@0 | 378 | static void SetDeviceConfig(SkBitmap::Config); |
michael@0 | 379 | |
michael@0 | 380 | protected: |
michael@0 | 381 | // must be overridden in subclasses. This guy is called by decode(...) |
michael@0 | 382 | virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; |
michael@0 | 383 | |
michael@0 | 384 | // If the decoder wants to support tiled based decoding, |
michael@0 | 385 | // this method must be overridden. This guy is called by buildTileIndex(...) |
michael@0 | 386 | virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height) { |
michael@0 | 387 | return false; |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | // If the decoder wants to support tiled based decoding, |
michael@0 | 391 | // this method must be overridden. This guy is called by decodeRegion(...) |
michael@0 | 392 | virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) { |
michael@0 | 393 | return false; |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | /* |
michael@0 | 397 | * Crop a rectangle from the src Bitmap to the dest Bitmap. src and dst are |
michael@0 | 398 | * both sampled by sampleSize from an original Bitmap. |
michael@0 | 399 | * |
michael@0 | 400 | * @param dst the destination bitmap. |
michael@0 | 401 | * @param src the source bitmap that is sampled by sampleSize from the |
michael@0 | 402 | * original bitmap. |
michael@0 | 403 | * @param sampleSize the sample size that src is sampled from the original bitmap. |
michael@0 | 404 | * @param (dstX, dstY) the upper-left point of the dest bitmap in terms of |
michael@0 | 405 | * the coordinate in the original bitmap. |
michael@0 | 406 | * @param (width, height) the width and height of the unsampled dst. |
michael@0 | 407 | * @param (srcX, srcY) the upper-left point of the src bitmap in terms of |
michael@0 | 408 | * the coordinate in the original bitmap. |
michael@0 | 409 | * @return bool Whether or not it succeeded. |
michael@0 | 410 | */ |
michael@0 | 411 | bool cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, |
michael@0 | 412 | int dstX, int dstY, int width, int height, |
michael@0 | 413 | int srcX, int srcY); |
michael@0 | 414 | |
michael@0 | 415 | /** |
michael@0 | 416 | * Copy all fields on this decoder to the other decoder. Used by subclasses |
michael@0 | 417 | * to decode a subimage using a different decoder, but with the same settings. |
michael@0 | 418 | */ |
michael@0 | 419 | void copyFieldsToOther(SkImageDecoder* other); |
michael@0 | 420 | |
michael@0 | 421 | /** |
michael@0 | 422 | * Return the default preference being used by the current or latest call to |
michael@0 | 423 | * decode. |
michael@0 | 424 | */ |
michael@0 | 425 | SkBitmap::Config getDefaultPref() { return fDefaultPref; } |
michael@0 | 426 | |
michael@0 | 427 | /** Can be queried from within onDecode, to see if the user (possibly in |
michael@0 | 428 | a different thread) has requested the decode to cancel. If this returns |
michael@0 | 429 | true, your onDecode() should stop and return false. |
michael@0 | 430 | Each subclass needs to decide how often it can query this, to balance |
michael@0 | 431 | responsiveness with performance. |
michael@0 | 432 | |
michael@0 | 433 | Calling this outside of onDecode() may return undefined values. |
michael@0 | 434 | */ |
michael@0 | 435 | |
michael@0 | 436 | public: |
michael@0 | 437 | bool shouldCancelDecode() const { return fShouldCancelDecode; } |
michael@0 | 438 | |
michael@0 | 439 | protected: |
michael@0 | 440 | SkImageDecoder(); |
michael@0 | 441 | |
michael@0 | 442 | // helper function for decoders to handle the (common) case where there is only |
michael@0 | 443 | // once choice available in the image file. |
michael@0 | 444 | bool chooseFromOneChoice(SkBitmap::Config config, int width, int height) const; |
michael@0 | 445 | |
michael@0 | 446 | /* Helper for subclasses. Call this to allocate the pixel memory given the bitmap's |
michael@0 | 447 | width/height/rowbytes/config. Returns true on success. This method handles checking |
michael@0 | 448 | for an optional Allocator. |
michael@0 | 449 | */ |
michael@0 | 450 | bool allocPixelRef(SkBitmap*, SkColorTable*) const; |
michael@0 | 451 | |
michael@0 | 452 | /** |
michael@0 | 453 | * The raw data of the src image. |
michael@0 | 454 | */ |
michael@0 | 455 | enum SrcDepth { |
michael@0 | 456 | // Color-indexed. |
michael@0 | 457 | kIndex_SrcDepth, |
michael@0 | 458 | // Grayscale in 8 bits. |
michael@0 | 459 | k8BitGray_SrcDepth, |
michael@0 | 460 | // 8 bits per component. Used for 24 bit if there is no alpha. |
michael@0 | 461 | k32Bit_SrcDepth, |
michael@0 | 462 | }; |
michael@0 | 463 | /** The subclass, inside onDecode(), calls this to determine the config of |
michael@0 | 464 | the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the |
michael@0 | 465 | src image. This routine returns the caller's preference given |
michael@0 | 466 | srcDepth and hasAlpha, or kNo_Config if there is no preference. |
michael@0 | 467 | |
michael@0 | 468 | Note: this also takes into account GetDeviceConfig(), so the subclass |
michael@0 | 469 | need not call that. |
michael@0 | 470 | */ |
michael@0 | 471 | SkBitmap::Config getPrefConfig(SrcDepth, bool hasAlpha) const; |
michael@0 | 472 | |
michael@0 | 473 | private: |
michael@0 | 474 | Peeker* fPeeker; |
michael@0 | 475 | Chooser* fChooser; |
michael@0 | 476 | SkBitmap::Allocator* fAllocator; |
michael@0 | 477 | int fSampleSize; |
michael@0 | 478 | SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false |
michael@0 | 479 | PrefConfigTable fPrefTable; // use if fUsePrefTable is true |
michael@0 | 480 | bool fDitherImage; |
michael@0 | 481 | bool fUsePrefTable; |
michael@0 | 482 | bool fSkipWritingZeroes; |
michael@0 | 483 | mutable bool fShouldCancelDecode; |
michael@0 | 484 | bool fPreferQualityOverSpeed; |
michael@0 | 485 | bool fRequireUnpremultipliedColors; |
michael@0 | 486 | }; |
michael@0 | 487 | |
michael@0 | 488 | /** Calling newDecoder with a stream returns a new matching imagedecoder |
michael@0 | 489 | instance, or NULL if none can be found. The caller must manage its ownership |
michael@0 | 490 | of the stream as usual, calling unref() when it is done, as the returned |
michael@0 | 491 | decoder may have called ref() (and if so, the decoder is responsible for |
michael@0 | 492 | balancing its ownership when it is destroyed). |
michael@0 | 493 | */ |
michael@0 | 494 | class SkImageDecoderFactory : public SkRefCnt { |
michael@0 | 495 | public: |
michael@0 | 496 | SK_DECLARE_INST_COUNT(SkImageDecoderFactory) |
michael@0 | 497 | |
michael@0 | 498 | virtual SkImageDecoder* newDecoder(SkStreamRewindable*) = 0; |
michael@0 | 499 | |
michael@0 | 500 | private: |
michael@0 | 501 | typedef SkRefCnt INHERITED; |
michael@0 | 502 | }; |
michael@0 | 503 | |
michael@0 | 504 | class SkDefaultImageDecoderFactory : SkImageDecoderFactory { |
michael@0 | 505 | public: |
michael@0 | 506 | // calls SkImageDecoder::Factory(stream) |
michael@0 | 507 | virtual SkImageDecoder* newDecoder(SkStreamRewindable* stream) { |
michael@0 | 508 | return SkImageDecoder::Factory(stream); |
michael@0 | 509 | } |
michael@0 | 510 | }; |
michael@0 | 511 | |
michael@0 | 512 | // This macro declares a global (i.e., non-class owned) creation entry point |
michael@0 | 513 | // for each decoder (e.g., CreateJPEGImageDecoder) |
michael@0 | 514 | #define DECLARE_DECODER_CREATOR(codec) \ |
michael@0 | 515 | SkImageDecoder *Create ## codec (); |
michael@0 | 516 | |
michael@0 | 517 | // This macro defines the global creation entry point for each decoder. Each |
michael@0 | 518 | // decoder implementation that registers with the decoder factory must call it. |
michael@0 | 519 | #define DEFINE_DECODER_CREATOR(codec) \ |
michael@0 | 520 | SkImageDecoder *Create ## codec () { \ |
michael@0 | 521 | return SkNEW( Sk ## codec ); \ |
michael@0 | 522 | } |
michael@0 | 523 | |
michael@0 | 524 | // All the decoders known by Skia. Note that, depending on the compiler settings, |
michael@0 | 525 | // not all of these will be available |
michael@0 | 526 | DECLARE_DECODER_CREATOR(BMPImageDecoder); |
michael@0 | 527 | DECLARE_DECODER_CREATOR(GIFImageDecoder); |
michael@0 | 528 | DECLARE_DECODER_CREATOR(ICOImageDecoder); |
michael@0 | 529 | DECLARE_DECODER_CREATOR(JPEGImageDecoder); |
michael@0 | 530 | DECLARE_DECODER_CREATOR(PNGImageDecoder); |
michael@0 | 531 | DECLARE_DECODER_CREATOR(WBMPImageDecoder); |
michael@0 | 532 | DECLARE_DECODER_CREATOR(WEBPImageDecoder); |
michael@0 | 533 | |
michael@0 | 534 | |
michael@0 | 535 | // Typedefs to make registering decoder and formatter callbacks easier. |
michael@0 | 536 | // These have to be defined outside SkImageDecoder. :( |
michael@0 | 537 | typedef SkTRegistry<SkImageDecoder*(*)(SkStreamRewindable*)> SkImageDecoder_DecodeReg; |
michael@0 | 538 | typedef SkTRegistry<SkImageDecoder::Format(*)(SkStreamRewindable*)> SkImageDecoder_FormatReg; |
michael@0 | 539 | |
michael@0 | 540 | #endif |