Back to index

lightning-sunbird  0.9+nobinonly
nsImageModule.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  *   Stuart Parmenter <pavlov@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 "nsImgBuildDefines.h"
00041 
00042 #ifdef XP_MAC
00043 #define IMG_BUILD_gif 1
00044 #define IMG_BUILD_bmp 1
00045 #define IMG_BUILD_png 1
00046 #define IMG_BUILD_jpeg 1
00047 #define IMG_BUILD_xbm 1
00048 #endif
00049 
00050 #include "nsIGenericFactory.h"
00051 #include "nsIModule.h"
00052 #include "nsICategoryManager.h"
00053 #include "nsXPCOMCID.h"
00054 #include "nsServiceManagerUtils.h"
00055 
00056 #include "imgCache.h"
00057 #include "imgContainer.h"
00058 #include "imgLoader.h"
00059 #include "imgRequest.h"
00060 #include "imgRequestProxy.h"
00061 
00062 #ifdef IMG_BUILD_DECODER_gif
00063 // gif
00064 #include "imgContainerGIF.h"
00065 #include "nsGIFDecoder2.h"
00066 #endif
00067 
00068 #ifdef IMG_BUILD_DECODER_bmp
00069 // bmp/ico
00070 #include "nsBMPDecoder.h"
00071 #include "nsICODecoder.h"
00072 #endif
00073 
00074 #ifdef IMG_BUILD_DECODER_png
00075 // png
00076 #include "nsPNGDecoder.h"
00077 #endif
00078 
00079 #ifdef IMG_BUILD_DECODER_jpeg
00080 // jpeg
00081 #include "nsJPEGDecoder.h"
00082 #endif
00083 
00084 #ifdef IMG_BUILD_DECODER_xbm
00085 // xbm
00086 #include "nsXBMDecoder.h"
00087 #endif
00088 
00089 
00090 #ifdef IMG_BUILD_ENCODER_png
00091 // png
00092 #include "nsPNGEncoder.h"
00093 #endif
00094 #ifdef IMG_BUILD_ENCODER_jpeg
00095 // jpeg
00096 #include "nsJPEGEncoder.h"
00097 #endif
00098 
00099 
00100 // objects that just require generic constructors
00101 
00102 NS_GENERIC_FACTORY_CONSTRUCTOR(imgCache)
00103 NS_GENERIC_FACTORY_CONSTRUCTOR(imgContainer)
00104 NS_GENERIC_FACTORY_CONSTRUCTOR(imgLoader)
00105 NS_GENERIC_FACTORY_CONSTRUCTOR(imgRequestProxy)
00106 
00107 #ifdef IMG_BUILD_DECODER_gif
00108 // gif
00109 NS_GENERIC_FACTORY_CONSTRUCTOR(imgContainerGIF)
00110 NS_GENERIC_FACTORY_CONSTRUCTOR(nsGIFDecoder2)
00111 #endif
00112 
00113 #ifdef IMG_BUILD_DECODER_jpeg
00114 // jpeg
00115 NS_GENERIC_FACTORY_CONSTRUCTOR(nsJPEGDecoder)
00116 #endif
00117 #ifdef IMG_BUILD_ENCODER_jpeg
00118 // jpeg
00119 NS_GENERIC_FACTORY_CONSTRUCTOR(nsJPEGEncoder)
00120 #endif
00121 
00122 #ifdef IMG_BUILD_DECODER_bmp
00123 // bmp
00124 NS_GENERIC_FACTORY_CONSTRUCTOR(nsICODecoder)
00125 NS_GENERIC_FACTORY_CONSTRUCTOR(nsBMPDecoder)
00126 #endif
00127 
00128 #ifdef IMG_BUILD_DECODER_png
00129 // png
00130 NS_GENERIC_FACTORY_CONSTRUCTOR(nsPNGDecoder)
00131 #endif
00132 #ifdef IMG_BUILD_ENCODER_png
00133 // png
00134 NS_GENERIC_FACTORY_CONSTRUCTOR(nsPNGEncoder)
00135 #endif
00136 
00137 #ifdef IMG_BUILD_DECODER_xbm
00138 // xbm
00139 NS_GENERIC_FACTORY_CONSTRUCTOR(nsXBMDecoder)
00140 #endif
00141 
00142 static const char* gImageMimeTypes[] = {
00143 #ifdef IMG_BUILD_DECODER_gif
00144   "image/gif",
00145 #endif
00146 #ifdef IMG_BUILD_DECODER_jpeg
00147   "image/jpeg",
00148   "image/pjpeg",
00149   "image/jpg",
00150 #endif
00151 #ifdef IMG_BUILD_DECODER_bmp
00152   "image/x-icon",
00153   "image/vnd.microsoft.icon",
00154   "image/bmp",
00155 #endif
00156 #ifdef IMG_BUILD_DECODER_png
00157   "image/png",
00158   "image/x-png",
00159 #endif
00160 #ifdef IMG_BUILD_DECODER_xbm
00161   "image/x-xbitmap",
00162   "image/x-xbm",
00163   "image/xbm"
00164 #endif
00165 };
00166 
00167 static NS_METHOD ImageRegisterProc(nsIComponentManager *aCompMgr,
00168                                    nsIFile *aPath,
00169                                    const char *registryLocation,
00170                                    const char *componentType,
00171                                    const nsModuleComponentInfo *info) {
00172   nsresult rv;
00173   nsCOMPtr<nsICategoryManager> catMan(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv));
00174   if (NS_FAILED(rv))
00175     return rv;
00176   for (unsigned i = 0; i < sizeof(gImageMimeTypes)/sizeof(*gImageMimeTypes); i++) {
00177     catMan->AddCategoryEntry("Gecko-Content-Viewers", gImageMimeTypes[i],
00178                              "@mozilla.org/content/document-loader-factory;1",
00179                              PR_TRUE, PR_TRUE, nsnull);
00180   }
00181 
00182   catMan->AddCategoryEntry("content-sniffing-services", "@mozilla.org/image/loader;1",
00183                            "@mozilla.org/image/loader;1", PR_TRUE, PR_TRUE,
00184                            nsnull);
00185   return NS_OK;
00186 }
00187 
00188 static NS_METHOD ImageUnregisterProc(nsIComponentManager *aCompMgr,
00189                                      nsIFile *aPath,
00190                                      const char *registryLocation,
00191                                      const nsModuleComponentInfo *info) {
00192   nsresult rv;
00193   nsCOMPtr<nsICategoryManager> catMan(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv));
00194   if (NS_FAILED(rv))
00195     return rv;
00196   for (unsigned i = 0; i < sizeof(gImageMimeTypes)/sizeof(*gImageMimeTypes); i++)
00197     catMan->DeleteCategoryEntry("Gecko-Content-Viewers", gImageMimeTypes[i], PR_TRUE);
00198 
00199   return NS_OK;
00200 }
00201 
00202 static const nsModuleComponentInfo components[] =
00203 {
00204   { "image cache",
00205     NS_IMGCACHE_CID,
00206     "@mozilla.org/image/cache;1",
00207     imgCacheConstructor, },
00208   { "image container",
00209     NS_IMGCONTAINER_CID,
00210     "@mozilla.org/image/container;1",
00211     imgContainerConstructor, },
00212   { "image loader",
00213     NS_IMGLOADER_CID,
00214     "@mozilla.org/image/loader;1",
00215     imgLoaderConstructor,
00216     ImageRegisterProc, /* register the decoder mime types here */
00217     ImageUnregisterProc, },
00218   { "image request proxy",
00219     NS_IMGREQUESTPROXY_CID,
00220     "@mozilla.org/image/request;1",
00221     imgRequestProxyConstructor, },
00222 
00223 #ifdef IMG_BUILD_DECODER_gif
00224   // gif
00225   { "GIF image container",
00226     NS_GIFCONTAINER_CID,
00227     "@mozilla.org/image/container;1?type=image/gif",
00228     imgContainerGIFConstructor, },
00229   { "GIF Decoder",
00230      NS_GIFDECODER2_CID,
00231      "@mozilla.org/image/decoder;2?type=image/gif",
00232      nsGIFDecoder2Constructor, },
00233 #endif
00234 
00235 #ifdef IMG_BUILD_DECODER_jpeg
00236   // jpeg
00237   { "JPEG decoder",
00238     NS_JPEGDECODER_CID,
00239     "@mozilla.org/image/decoder;2?type=image/jpeg",
00240     nsJPEGDecoderConstructor, },
00241   { "JPEG decoder",
00242     NS_JPEGDECODER_CID,
00243     "@mozilla.org/image/decoder;2?type=image/pjpeg",
00244     nsJPEGDecoderConstructor, },
00245   { "JPEG decoder",
00246     NS_JPEGDECODER_CID,
00247     "@mozilla.org/image/decoder;2?type=image/jpg",
00248     nsJPEGDecoderConstructor, },
00249 #endif
00250 #ifdef IMG_BUILD_ENCODER_jpeg
00251   // jpeg (encoder)
00252   { "JPEG Encoder",
00253     NS_JPEGENCODER_CID,
00254     "@mozilla.org/image/encoder;2?type=image/jpeg",
00255     nsJPEGEncoderConstructor, },
00256 #endif
00257 
00258 #ifdef IMG_BUILD_DECODER_bmp
00259   // bmp
00260   { "ICO Decoder",
00261      NS_ICODECODER_CID,
00262      "@mozilla.org/image/decoder;2?type=image/x-icon",
00263      nsICODecoderConstructor, },
00264   { "ICO Decoder",
00265      NS_ICODECODER_CID,
00266      "@mozilla.org/image/decoder;2?type=image/vnd.microsoft.icon",
00267      nsICODecoderConstructor, },
00268   { "BMP Decoder",
00269      NS_BMPDECODER_CID,
00270      "@mozilla.org/image/decoder;2?type=image/bmp",
00271      nsBMPDecoderConstructor, },
00272 #endif
00273 
00274 #ifdef IMG_BUILD_DECODER_png
00275   // png
00276   { "PNG Decoder",
00277     NS_PNGDECODER_CID,
00278     "@mozilla.org/image/decoder;2?type=image/png",
00279     nsPNGDecoderConstructor, },
00280   { "PNG Decoder",
00281     NS_PNGDECODER_CID,
00282     "@mozilla.org/image/decoder;2?type=image/x-png",
00283     nsPNGDecoderConstructor, },
00284 #endif
00285 #ifdef IMG_BUILD_ENCODER_png
00286   // png
00287   { "PNG Encoder",
00288     NS_PNGENCODER_CID,
00289     "@mozilla.org/image/encoder;2?type=image/png",
00290     nsPNGEncoderConstructor, },
00291 #endif
00292 
00293 #ifdef IMG_BUILD_DECODER_xbm
00294   // xbm
00295   { "XBM Decoder",
00296      NS_XBMDECODER_CID,
00297      "@mozilla.org/image/decoder;2?type=image/x-xbitmap",
00298      nsXBMDecoderConstructor, },
00299   { "XBM Decoder",
00300      NS_XBMDECODER_CID,
00301      "@mozilla.org/image/decoder;2?type=image/x-xbm",
00302      nsXBMDecoderConstructor, },
00303   { "XBM Decoder",
00304      NS_XBMDECODER_CID,
00305      "@mozilla.org/image/decoder;2?type=image/xbm",
00306      nsXBMDecoderConstructor, },
00307 #endif
00308 };
00309 
00310 PR_STATIC_CALLBACK(nsresult)
00311 imglib_Initialize(nsIModule* aSelf)
00312 {
00313   imgCache::Init();
00314   return NS_OK;
00315 }
00316 
00317 PR_STATIC_CALLBACK(void)
00318 imglib_Shutdown(nsIModule* aSelf)
00319 {
00320   imgCache::Shutdown();
00321 #ifdef IMG_BUILD_DECODER_gif
00322   nsGifShutdown();
00323 #endif
00324 }
00325 
00326 NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(nsImageLib2Module, components,
00327                                    imglib_Initialize, imglib_Shutdown)
00328