Back to index

lightning-sunbird  0.9+nobinonly
nsIconDecoder.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Scott MacGregor <mscott@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsIconDecoder.h"
00041 #include "nsIInputStream.h"
00042 #include "imgIContainer.h"
00043 #include "imgIContainerObserver.h"
00044 #include "imgILoad.h"
00045 #include "nspr.h"
00046 #include "nsIComponentManager.h"
00047 #include "nsRect.h"
00048 
00049 NS_IMPL_THREADSAFE_ADDREF(nsIconDecoder)
00050 NS_IMPL_THREADSAFE_RELEASE(nsIconDecoder)
00051 
00052 NS_INTERFACE_MAP_BEGIN(nsIconDecoder)
00053    NS_INTERFACE_MAP_ENTRY(imgIDecoder)
00054 NS_INTERFACE_MAP_END_THREADSAFE
00055 
00056 nsIconDecoder::nsIconDecoder()
00057 {
00058 }
00059 
00060 nsIconDecoder::~nsIconDecoder()
00061 { }
00062 
00063 
00066 NS_IMETHODIMP nsIconDecoder::Init(imgILoad *aLoad)
00067 {
00068   mObserver = do_QueryInterface(aLoad);  // we're holding 2 strong refs to the request.
00069 
00070   mImage = do_CreateInstance("@mozilla.org/image/container;1");
00071   if (!mImage) return NS_ERROR_OUT_OF_MEMORY;
00072 
00073   aLoad->SetImage(mImage);                                                   
00074 
00075   mFrame = do_CreateInstance("@mozilla.org/gfx/image/frame;2");
00076   if (!mFrame) return NS_ERROR_OUT_OF_MEMORY;
00077 
00078   return NS_OK;
00079 }
00080 
00081 NS_IMETHODIMP nsIconDecoder::Close()
00082 {
00083   if (mObserver) 
00084   {
00085     mObserver->OnStopFrame(nsnull, mFrame);
00086     mObserver->OnStopContainer(nsnull, mImage);
00087     mObserver->OnStopDecode(nsnull, NS_OK, nsnull);
00088   }
00089   
00090   return NS_OK;
00091 }
00092 
00093 NS_IMETHODIMP nsIconDecoder::Flush()
00094 {
00095   return NS_ERROR_NOT_IMPLEMENTED;
00096 }
00097 
00098 NS_IMETHODIMP nsIconDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
00099 {
00100   nsresult rv;
00101 
00102   PRUint8 * const buf = (PRUint8 *)PR_Malloc(count);
00103   if (!buf) return NS_ERROR_OUT_OF_MEMORY; /* we couldn't allocate the object */
00104  
00105   // read the data from the input stram...
00106   PRUint32 readLen;
00107   rv = inStr->Read((char*)buf, count, &readLen);
00108   NS_ENSURE_SUCCESS(rv, rv);
00109   NS_ENSURE_TRUE(readLen >= 3, NS_ERROR_UNEXPECTED); // w, h, alphaBits
00110 
00111   PRUint8 * const buf_end = buf + readLen;
00112   PRUint8 *data = buf;
00113 
00114   // since WriteFrom is only called once, go ahead and fire the on start notifications..
00115 
00116   mObserver->OnStartDecode(nsnull);
00117   // Read size
00118   PRInt32 w = *(data++);
00119   PRInt32 h = *(data++);
00120   PRUint8 alphaBits = *(data++);
00121   NS_ENSURE_TRUE(w > 0 && h > 0 && (alphaBits == 1 || alphaBits == 8),
00122                  NS_ERROR_UNEXPECTED);
00123 
00124   mImage->Init(w, h, mObserver);
00125   if (mObserver)
00126     mObserver->OnStartContainer(nsnull, mImage);
00127 
00128   gfx_format format = alphaBits == 1 ? gfx_format(gfxIFormats::RGB_A1)
00129                                      : gfx_format(gfxIFormats::RGB_A8);
00130   rv = mFrame->Init(0, 0, w, h, format, 24);
00131   if (NS_FAILED(rv))
00132     return rv;
00133 
00134   mImage->AppendFrame(mFrame);
00135   if (mObserver)
00136     mObserver->OnStartFrame(nsnull, mFrame);
00137   
00138   PRUint32 bpr, abpr;
00139   PRInt32 width, height;
00140   mFrame->GetImageBytesPerRow(&bpr);
00141   mFrame->GetAlphaBytesPerRow(&abpr);
00142   mFrame->GetWidth(&width);
00143   mFrame->GetHeight(&height);
00144 
00145   NS_ENSURE_TRUE(buf_end - data >= PRInt32(bpr + abpr) * height,
00146                  NS_ERROR_UNEXPECTED);
00147   
00148   PRInt32 rownum;
00149   for (rownum = 0; rownum < height; ++rownum, data += bpr)
00150     mFrame->SetImageData(data, bpr, rownum * bpr);
00151 
00152   for (rownum = 0; rownum < height; ++rownum, data += abpr)
00153     mFrame->SetAlphaData(data, abpr, rownum * abpr);   
00154 
00155   nsIntRect r(0, 0, width, height);
00156   mObserver->OnDataAvailable(nsnull, mFrame, &r);
00157 
00158   PR_Free(buf);
00159 
00160   return NS_OK;
00161 }
00162