forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnsImageManager.cpp
144 lines (123 loc) · 3.27 KB
/
nsImageManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsIImageManager.h"
#include "libimg.h"
#include "il_strm.h"
#include "nsCRT.h"
#include "nsImageNet.h"
static NS_DEFINE_IID(kIImageManagerIID, NS_IIMAGEMANAGER_IID);
class ImageManagerImpl : public nsIImageManager {
public:
ImageManagerImpl();
virtual ~ImageManagerImpl();
nsresult Init();
NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
NS_DECL_ISUPPORTS
virtual void SetCacheSize(PRInt32 aCacheSize);
virtual PRInt32 GetCacheSize(void);
virtual PRInt32 ShrinkCache(void);
virtual nsImageType GetImageType(const char *buf, PRInt32 length);
private:
ilISystemServices *mSS;
};
// The singleton image manager
// XXX make this a service
static ImageManagerImpl* gImageManager;
ImageManagerImpl::ImageManagerImpl()
{
NS_NewImageSystemServices(&mSS);
NS_ADDREF(mSS);
IL_Init(mSS);
IL_SetCacheSize(2048L * 1024L);
}
ImageManagerImpl::~ImageManagerImpl()
{
IL_Shutdown();
NS_RELEASE(mSS);
// gImageManager = nsnull;
}
NS_IMPL_ADDREF(ImageManagerImpl)
NS_IMPL_QUERY_INTERFACE(ImageManagerImpl, kIImageManagerIID)
nsrefcnt ImageManagerImpl::Release(void)
{
if (--mRefCnt == 0) {
NS_DELETEXPCOM(this);
return 0;
}
return mRefCnt;
}
nsresult
ImageManagerImpl::Init()
{
return NS_OK;
}
void
ImageManagerImpl::SetCacheSize(PRInt32 aCacheSize)
{
IL_SetCacheSize(aCacheSize);
}
PRInt32
ImageManagerImpl::GetCacheSize()
{
return IL_GetCacheSize();
}
PRInt32
ImageManagerImpl::ShrinkCache(void)
{
return IL_ShrinkCache();
}
nsImageType
ImageManagerImpl::GetImageType(const char *buf, PRInt32 length)
{
int ret;
NS_PRECONDITION(nsnull != buf, "null ptr");
ret = IL_Type(buf, length);
switch(ret) {
case(IL_GIF):
return nsImageType_kGIF;
case(IL_XBM):
return nsImageType_kXBM;
case(IL_JPEG):
return nsImageType_kJPEG;
case(IL_PPM):
return nsImageType_kPPM;
case(IL_PNG):
return nsImageType_kPNG;
case(IL_ART):
return nsImageType_kART;
default:
return nsImageType_kUnknown;
}
}
extern "C" NS_GFX_(nsresult)
NS_NewImageManager(nsIImageManager **aInstancePtrResult)
{
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
if (nsnull == aInstancePtrResult) {
return NS_ERROR_NULL_POINTER;
}
if (nsnull == gImageManager) {
gImageManager = new ImageManagerImpl();
NS_IF_ADDREF(gImageManager);
}
if (nsnull == gImageManager) {
return NS_ERROR_OUT_OF_MEMORY;
}
return gImageManager->QueryInterface(kIImageManagerIID,
(void **)aInstancePtrResult);
}