jpeg_codec.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_CODEC_JPEG_CODEC_H_
  5. #define UI_GFX_CODEC_JPEG_CODEC_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "third_party/skia/include/core/SkImageInfo.h"
  10. #include "third_party/skia/include/core/SkPixmap.h"
  11. #include "third_party/skia/include/encode/SkJpegEncoder.h"
  12. #include "ui/gfx/codec/codec_export.h"
  13. class SkBitmap;
  14. namespace gfx {
  15. // Interface for encoding/decoding JPEG data. This is a wrapper around libjpeg,
  16. // which has an inconvenient interface for callers. This is only used for UI
  17. // elements, WebKit has its own more complicated JPEG decoder which handles,
  18. // among other things, partially downloaded data.
  19. class CODEC_EXPORT JPEGCodec {
  20. public:
  21. enum ColorFormat {
  22. // 4 bytes per pixel, in RGBA order in mem regardless of endianness.
  23. FORMAT_RGBA,
  24. // 4 bytes per pixel, in BGRA order in mem regardless of endianness.
  25. // This is the default Windows DIB order.
  26. FORMAT_BGRA,
  27. // 4 bytes per pixel, it can be either RGBA or BGRA. It depends on the bit
  28. // order in kARGB_8888_Config skia bitmap.
  29. FORMAT_SkBitmap
  30. };
  31. // Encodes the given raw 'input' pixmap, which includes a pointer to pixels
  32. // as well as information describing the pixel format. The encoded JPEG data
  33. // will be written into the supplied vector and true will be returned on
  34. // success. On failure (false), the contents of the output buffer are
  35. // undefined.
  36. //
  37. // downsample: specifies how pixels will be sampled in the encoded JPEG image,
  38. // can be either k420, k422 or k444.
  39. // quality: an integer in the range 0-100, where 100 is the highest quality.
  40. static bool Encode(const SkPixmap& input,
  41. int quality,
  42. SkJpegEncoder::Downsample downsample,
  43. std::vector<unsigned char>* output);
  44. // Encodes the given raw 'input' pixmap, which includes a pointer to pixels
  45. // as well as information describing the pixel format. The encoded JPEG data
  46. // will be written into the supplied vector and true will be returned on
  47. // success. On failure (false), the contents of the output buffer are
  48. // undefined.
  49. //
  50. // quality: an integer in the range 0-100, where 100 is the highest quality.
  51. static bool Encode(const SkPixmap& input,
  52. int quality,
  53. std::vector<unsigned char>* output);
  54. // Encodes the 'input' bitmap. The encoded JPEG data will be written into
  55. // the supplied vector and true will be returned on success. On failure
  56. // (false), the contents of the output buffer are undefined.
  57. //
  58. // quality: an integer in the range 0-100, where 100 is the highest quality.
  59. static bool Encode(const SkBitmap& input,
  60. int quality,
  61. std::vector<unsigned char>* output);
  62. // Decodes the JPEG data contained in input of length input_size. The
  63. // decoded data will be placed in *output with the dimensions in *w and *h
  64. // on success (returns true). This data will be written in the'format'
  65. // format. On failure, the values of these output variables is undefined.
  66. static bool Decode(const unsigned char* input, size_t input_size,
  67. ColorFormat format, std::vector<unsigned char>* output,
  68. int* w, int* h);
  69. // Decodes the JPEG data contained in input of length input_size. If
  70. // successful, a SkBitmap is created and returned.
  71. static std::unique_ptr<SkBitmap> Decode(const unsigned char* input,
  72. size_t input_size);
  73. };
  74. } // namespace gfx
  75. #endif // UI_GFX_CODEC_JPEG_CODEC_H_