PageRenderTime 16ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/image/decoders/nsPNGDecoder.h

http://github.com/zpao/v8monkey
C Header | 147 lines | 73 code | 24 blank | 50 comment | 4 complexity | 2cd5683da5714f44632d86034c9324fd MD5 | raw file
  1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2 *
  3 * ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is mozilla.org code.
 17 *
 18 * The Initial Developer of the Original Code is
 19 * Netscape Communications Corporation.
 20 * Portions created by the Initial Developer are Copyright (C) 2001
 21 * the Initial Developer. All Rights Reserved.
 22 *
 23 * Contributor(s):
 24 *   Stuart Parmenter <pavlov@netscape.com>
 25 *   Bobby Holley <bobbyholley@gmail.com>
 26 *
 27 * Alternatively, the contents of this file may be used under the terms of
 28 * either the GNU General Public License Version 2 or later (the "GPL"), or
 29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 30 * in which case the provisions of the GPL or the LGPL are applicable instead
 31 * of those above. If you wish to allow use of your version of this file only
 32 * under the terms of either the GPL or the LGPL, and not to allow others to
 33 * use your version of this file under the terms of the MPL, indicate your
 34 * decision by deleting the provisions above and replace them with the notice
 35 * and other provisions required by the GPL or the LGPL. If you do not delete
 36 * the provisions above, a recipient may use your version of this file under
 37 * the terms of any one of the MPL, the GPL or the LGPL.
 38 *
 39 * ***** END LICENSE BLOCK ***** */
 40
 41#ifndef nsPNGDecoder_h__
 42#define nsPNGDecoder_h__
 43
 44#include "Decoder.h"
 45
 46#include "imgIDecoderObserver.h"
 47#include "gfxASurface.h"
 48
 49#include "nsCOMPtr.h"
 50
 51#include "png.h"
 52
 53#include "qcms.h"
 54
 55namespace mozilla {
 56namespace image {
 57class RasterImage;
 58
 59class nsPNGDecoder : public Decoder
 60{
 61public:
 62  nsPNGDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver);
 63  virtual ~nsPNGDecoder();
 64
 65  virtual void InitInternal();
 66  virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
 67  virtual Telemetry::ID SpeedHistogram();
 68
 69  void CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
 70                   PRInt32 width, PRInt32 height,
 71                   gfxASurface::gfxImageFormat format);
 72  void SetAnimFrameInfo();
 73
 74  void EndImageFrame();
 75
 76  // Check if PNG is valid ICO (32bpp RGBA)
 77  // http://blogs.msdn.com/b/oldnewthing/archive/2010/10/22/10079192.aspx
 78  bool IsValidICO() const
 79  {
 80    png_uint_32
 81        png_width,  // Unused
 82        png_height; // Unused
 83
 84    int png_bit_depth,
 85        png_color_type;
 86
 87    if (png_get_IHDR(mPNG, mInfo, &png_width, &png_height, &png_bit_depth,
 88                     &png_color_type, NULL, NULL, NULL)) {
 89
 90      return (png_color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
 91              png_bit_depth == 8);
 92    } else {
 93      return false;
 94    }
 95  }
 96
 97public:
 98  png_structp mPNG;
 99  png_infop mInfo;
100  nsIntRect mFrameRect;
101  PRUint8 *mCMSLine;
102  PRUint8 *interlacebuf;
103  PRUint8 *mImageData;
104  qcms_profile *mInProfile;
105  qcms_transform *mTransform;
106
107  gfxASurface::gfxImageFormat format;
108
109  // For size decodes
110  PRUint8 *mHeaderBuf;
111  PRUint32 mHeaderBytesRead;
112
113  PRUint8 mChannels;
114  bool mFrameHasNoAlpha;
115  bool mFrameIsHidden;
116
117  // whether CMS or premultiplied alpha are forced off
118  PRUint32 mCMSMode;
119  bool mDisablePremultipliedAlpha;
120  
121  /*
122   * libpng callbacks
123   *
124   * We put these in the class so that they can access protected members.
125   */
126  static void PNGAPI info_callback(png_structp png_ptr, png_infop info_ptr);
127  static void PNGAPI row_callback(png_structp png_ptr, png_bytep new_row,
128                                  png_uint_32 row_num, int pass);
129#ifdef PNG_APNG_SUPPORTED
130  static void PNGAPI frame_info_callback(png_structp png_ptr,
131                                         png_uint_32 frame_num);
132#endif
133  static void PNGAPI end_callback(png_structp png_ptr, png_infop info_ptr);
134  static void PNGAPI error_callback(png_structp png_ptr,
135                                    png_const_charp error_msg);
136  static void PNGAPI warning_callback(png_structp png_ptr,
137                                      png_const_charp warning_msg);
138
139  // This is defined in the PNG spec as an invariant. We use it to
140  // do manual validation without libpng.
141  static const PRUint8 pngSignatureBytes[];
142};
143
144} // namespace image
145} // namespace mozilla
146
147#endif // nsPNGDecoder_h__