LCOV - code coverage report
Current view: directory - image/src - imgTools.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 74 64 86.5 %
Date: 2012-06-02 Functions: 9 9 100.0 %

       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 Mozilla Foundation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2007
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Justin Dolske <dolske@mozilla.com> (original author)
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "imgTools.h"
      40                 : #include "nsCOMPtr.h"
      41                 : #include "nsString.h"
      42                 : #include "ImageErrors.h"
      43                 : #include "imgIContainer.h"
      44                 : #include "imgIEncoder.h"
      45                 : #include "imgIDecoderObserver.h"
      46                 : #include "imgIContainerObserver.h"
      47                 : #include "gfxContext.h"
      48                 : #include "nsStringStream.h"
      49                 : #include "nsComponentManagerUtils.h"
      50                 : #include "nsWeakReference.h"
      51                 : #include "nsIInterfaceRequestorUtils.h"
      52                 : #include "nsStreamUtils.h"
      53                 : #include "nsNetUtil.h"
      54                 : #include "RasterImage.h"
      55                 : 
      56                 : using namespace mozilla::image;
      57                 : 
      58                 : /* ========== imgITools implementation ========== */
      59                 : 
      60                 : 
      61                 : 
      62              77 : NS_IMPL_ISUPPORTS1(imgTools, imgITools)
      63                 : 
      64               8 : imgTools::imgTools()
      65                 : {
      66                 :   /* member initializers and constructor code */
      67               8 : }
      68                 : 
      69              16 : imgTools::~imgTools()
      70                 : {
      71                 :   /* destructor code */
      72              32 : }
      73                 : 
      74                 : 
      75              14 : NS_IMETHODIMP imgTools::DecodeImageData(nsIInputStream* aInStr,
      76                 :                                         const nsACString& aMimeType,
      77                 :                                         imgIContainer **aContainer)
      78                 : {
      79                 :   nsresult rv;
      80                 :   RasterImage* image;  // convenience alias for *aContainer
      81                 : 
      82              14 :   NS_ENSURE_ARG_POINTER(aInStr);
      83                 : 
      84                 :   // If the caller didn't provide an imgIContainer, create one.
      85              14 :   if (*aContainer) {
      86               0 :     NS_ABORT_IF_FALSE((*aContainer)->GetType() == imgIContainer::TYPE_RASTER,
      87                 :                       "wrong type of imgIContainer for decoding into");
      88               0 :     image = static_cast<RasterImage*>(*aContainer);
      89                 :   } else {
      90              14 :     *aContainer = image = new RasterImage();
      91              14 :     NS_ADDREF(image);
      92                 :   }
      93                 : 
      94                 :   // Initialize the Image. If we're using the one from the caller, we
      95                 :   // require that it not be initialized.
      96              28 :   nsCString mimeType(aMimeType);
      97              14 :   rv = image->Init(nsnull, mimeType.get(), "<unknown>", Image::INIT_FLAG_NONE);
      98              14 :   NS_ENSURE_SUCCESS(rv, rv);
      99                 : 
     100              28 :   nsCOMPtr<nsIInputStream> inStream = aInStr;
     101              14 :   if (!NS_InputStreamIsBuffered(aInStr)) {
     102               0 :     nsCOMPtr<nsIInputStream> bufStream;
     103               0 :     rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream), aInStr, 1024);
     104               0 :     if (NS_SUCCEEDED(rv))
     105               0 :       inStream = bufStream;
     106                 :   }
     107                 : 
     108                 :   // Figure out how much data we've been passed
     109                 :   PRUint32 length;
     110              14 :   rv = inStream->Available(&length);
     111              14 :   NS_ENSURE_SUCCESS(rv, rv);
     112                 : 
     113                 :   // Send the source data to the Image. WriteToRasterImage always
     114                 :   // consumes everything it gets if it doesn't run out of memory.
     115                 :   PRUint32 bytesRead;
     116              14 :   rv = inStream->ReadSegments(RasterImage::WriteToRasterImage,
     117                 :                               static_cast<void*>(image),
     118              14 :                               length, &bytesRead);
     119              14 :   NS_ENSURE_SUCCESS(rv, rv);
     120              14 :   NS_ABORT_IF_FALSE(bytesRead == length || image->HasError(),
     121                 :   "WriteToRasterImage should consume everything or the image must be in error!");
     122                 : 
     123                 :   // Let the Image know we've sent all the data
     124              14 :   rv = image->SourceDataComplete();
     125              14 :   NS_ENSURE_SUCCESS(rv, rv);
     126                 : 
     127                 :   // All done
     128              13 :   return NS_OK;
     129                 : }
     130                 : 
     131                 : 
     132               7 : NS_IMETHODIMP imgTools::EncodeImage(imgIContainer *aContainer,
     133                 :                                     const nsACString& aMimeType,
     134                 :                                     const nsAString& aOutputOptions,
     135                 :                                     nsIInputStream **aStream)
     136                 : {
     137                 :     return EncodeScaledImage(aContainer,
     138                 :                              aMimeType,
     139                 :                              0,
     140                 :                              0,
     141                 :                              aOutputOptions,
     142               7 :                              aStream);
     143                 : }
     144                 : 
     145              19 : NS_IMETHODIMP imgTools::EncodeScaledImage(imgIContainer *aContainer,
     146                 :                                           const nsACString& aMimeType,
     147                 :                                           PRInt32 aScaledWidth,
     148                 :                                           PRInt32 aScaledHeight,
     149                 :                                           const nsAString& aOutputOptions,
     150                 :                                           nsIInputStream **aStream)
     151                 : {
     152                 :   nsresult rv;
     153              19 :   bool doScaling = true;
     154                 :   PRUint8 *bitmapData;
     155                 :   PRUint32 bitmapDataLength, strideSize;
     156                 : 
     157                 :   // If no scaled size is specified, we'll just encode the image at its
     158                 :   // original size (no scaling).
     159              19 :   if (aScaledWidth == 0 && aScaledHeight == 0) {
     160               7 :     doScaling = false;
     161                 :   } else {
     162              12 :     NS_ENSURE_ARG(aScaledWidth > 0);
     163              12 :     NS_ENSURE_ARG(aScaledHeight > 0);
     164                 :   }
     165                 : 
     166                 :   // Get an image encoder for the media type
     167                 :   nsCAutoString encoderCID(
     168              38 :     NS_LITERAL_CSTRING("@mozilla.org/image/encoder;2?type=") + aMimeType);
     169                 : 
     170              38 :   nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get());
     171              19 :   if (!encoder)
     172               0 :     return NS_IMAGELIB_ERROR_NO_ENCODER;
     173                 : 
     174                 :   // Use frame 0 from the image container.
     175              38 :   nsRefPtr<gfxImageSurface> frame;
     176                 :   rv = aContainer->CopyFrame(imgIContainer::FRAME_CURRENT, true,
     177              19 :                              getter_AddRefs(frame));
     178              19 :   NS_ENSURE_SUCCESS(rv, rv);
     179              19 :   if (!frame)
     180               0 :     return NS_ERROR_NOT_AVAILABLE;
     181                 : 
     182              19 :   PRInt32 w = frame->Width(), h = frame->Height();
     183              19 :   if (!w || !h)
     184               0 :     return NS_ERROR_FAILURE;
     185                 : 
     186              38 :   nsRefPtr<gfxImageSurface> dest;
     187                 : 
     188              19 :   if (!doScaling) {
     189                 :     // If we're not scaling the image, use the actual width/height.
     190               7 :     aScaledWidth  = w;
     191               7 :     aScaledHeight = h;
     192                 : 
     193               7 :     bitmapData = frame->Data();
     194               7 :     if (!bitmapData)
     195               0 :       return NS_ERROR_FAILURE;
     196                 : 
     197               7 :     strideSize = frame->Stride();
     198               7 :     bitmapDataLength = aScaledHeight * strideSize;
     199                 : 
     200                 :   } else {
     201                 :     // Prepare to draw a scaled version of the image to a temporary surface...
     202                 : 
     203                 :     // Create a temporary image surface
     204                 :     dest = new gfxImageSurface(gfxIntSize(aScaledWidth, aScaledHeight),
     205              24 :                                gfxASurface::ImageFormatARGB32);
     206              24 :     gfxContext ctx(dest);
     207                 : 
     208                 :     // Set scaling
     209              12 :     gfxFloat sw = (double) aScaledWidth / w;
     210              12 :     gfxFloat sh = (double) aScaledHeight / h;
     211              12 :     ctx.Scale(sw, sh);
     212                 : 
     213                 :     // Paint a scaled image
     214              12 :     ctx.SetOperator(gfxContext::OPERATOR_SOURCE);
     215              12 :     ctx.SetSource(frame);
     216              12 :     ctx.Paint();
     217                 : 
     218              12 :     bitmapData = dest->Data();
     219              12 :     strideSize = dest->Stride();
     220              12 :     bitmapDataLength = aScaledHeight * strideSize;
     221                 :   }
     222                 : 
     223                 :   // Encode the bitmap
     224              19 :   rv = encoder->InitFromData(bitmapData,
     225                 :                              bitmapDataLength,
     226                 :                              aScaledWidth,
     227                 :                              aScaledHeight,
     228                 :                              strideSize,
     229                 :                              imgIEncoder::INPUT_FORMAT_HOSTARGB,
     230              19 :                              aOutputOptions);
     231                 : 
     232              19 :   NS_ENSURE_SUCCESS(rv, rv);
     233                 : 
     234              19 :   return CallQueryInterface(encoder, aStream);
     235                 : }

Generated by: LCOV version 1.7