diff options
Diffstat (limited to 'linden/indra/llimage/llimagepng.cpp')
-rw-r--r-- | linden/indra/llimage/llimagepng.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/linden/indra/llimage/llimagepng.cpp b/linden/indra/llimage/llimagepng.cpp new file mode 100644 index 0000000..d6b62a8 --- /dev/null +++ b/linden/indra/llimage/llimagepng.cpp | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | * @file llimagepng.cpp | ||
3 | * @brief LLImageFormatted glue to encode / decode PNG files. | ||
4 | * | ||
5 | * Copyright (c) 2007 Peekay Semyorka. | ||
6 | * Copyright (c) 2007-2007, Linden Research, Inc. | ||
7 | * | ||
8 | * Second Life Viewer Source Code | ||
9 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
10 | * to you under the terms of the GNU General Public License, version 2.0 | ||
11 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
12 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
13 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
14 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
15 | * | ||
16 | * There are special exceptions to the terms and conditions of the GPL as | ||
17 | * it is applied to this Source Code. View the full text of the exception | ||
18 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
19 | * online at http://secondlife.com/developers/opensource/flossexception | ||
20 | * | ||
21 | * By copying, modifying or distributing this software, you acknowledge | ||
22 | * that you have read and understood your obligations described above, | ||
23 | * and agree to abide by those obligations. | ||
24 | * | ||
25 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
26 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
27 | * COMPLETENESS OR PERFORMANCE. | ||
28 | */ | ||
29 | |||
30 | #include "linden_common.h" | ||
31 | #include "stdtypes.h" | ||
32 | #include "llerror.h" | ||
33 | |||
34 | #include "llimage.h" | ||
35 | #include "llpngwrapper.h" | ||
36 | #include "llimagepng.h" | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | // LLImagePNG | ||
40 | // --------------------------------------------------------------------------- | ||
41 | LLImagePNG::LLImagePNG() | ||
42 | : LLImageFormatted(IMG_CODEC_PNG), | ||
43 | mTmpWriteBuffer(NULL) | ||
44 | { | ||
45 | } | ||
46 | |||
47 | LLImagePNG::~LLImagePNG() | ||
48 | { | ||
49 | if (mTmpWriteBuffer) | ||
50 | { | ||
51 | delete[] mTmpWriteBuffer; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | // Virtual | ||
56 | // Parse PNG image information and set the appropriate | ||
57 | // width, height and component (channel) information. | ||
58 | BOOL LLImagePNG::updateData() | ||
59 | { | ||
60 | resetLastError(); | ||
61 | |||
62 | // Check to make sure that this instance has been initialized with data | ||
63 | if (!getData() || (0 == getDataSize())) | ||
64 | { | ||
65 | setLastError("Uninitialized instance of LLImagePNG"); | ||
66 | return FALSE; | ||
67 | } | ||
68 | |||
69 | // Decode the PNG data and extract sizing information | ||
70 | LLPngWrapper pngWrapper; | ||
71 | LLPngWrapper::ImageInfo infop; | ||
72 | if (! pngWrapper.readPng(getData(), NULL, &infop)) | ||
73 | { | ||
74 | setLastError(pngWrapper.getErrorMessage()); | ||
75 | return FALSE; | ||
76 | } | ||
77 | |||
78 | setSize(infop.mWidth, infop.mHeight, infop.mComponents); | ||
79 | |||
80 | return TRUE; | ||
81 | } | ||
82 | |||
83 | // Virtual | ||
84 | // Decode an in-memory PNG image into the raw RGB or RGBA format | ||
85 | // used within SecondLife. | ||
86 | BOOL LLImagePNG::decode(LLImageRaw* raw_image, F32 decode_time) | ||
87 | { | ||
88 | llassert_always(raw_image); | ||
89 | |||
90 | resetLastError(); | ||
91 | |||
92 | // Check to make sure that this instance has been initialized with data | ||
93 | if (!getData() || (0 == getDataSize())) | ||
94 | { | ||
95 | setLastError("LLImagePNG trying to decode an image with no data!"); | ||
96 | return FALSE; | ||
97 | } | ||
98 | |||
99 | // Decode the PNG data into the raw image | ||
100 | LLPngWrapper pngWrapper; | ||
101 | if (! pngWrapper.readPng(getData(), raw_image)) | ||
102 | { | ||
103 | setLastError(pngWrapper.getErrorMessage()); | ||
104 | return FALSE; | ||
105 | } | ||
106 | |||
107 | return TRUE; | ||
108 | } | ||
109 | |||
110 | // Virtual | ||
111 | // Encode the in memory RGB image into PNG format. | ||
112 | BOOL LLImagePNG::encode(const LLImageRaw* raw_image, F32 encode_time) | ||
113 | { | ||
114 | llassert_always(raw_image); | ||
115 | |||
116 | resetLastError(); | ||
117 | |||
118 | // Image logical size | ||
119 | setSize(raw_image->getWidth(), raw_image->getHeight(), raw_image->getComponents()); | ||
120 | |||
121 | // Temporary buffer to hold the encoded image. Note: the final image | ||
122 | // size should be much smaller due to compression. | ||
123 | if (mTmpWriteBuffer) | ||
124 | { | ||
125 | delete[] mTmpWriteBuffer; | ||
126 | } | ||
127 | U32 bufferSize = getWidth() * getHeight() * getComponents() + 1024; | ||
128 | U8* mTmpWriteBuffer = new U8[ bufferSize ]; | ||
129 | |||
130 | // Delegate actual encoding work to wrapper | ||
131 | LLPngWrapper pngWrapper; | ||
132 | if (! pngWrapper.writePng(raw_image, mTmpWriteBuffer)) | ||
133 | { | ||
134 | setLastError(pngWrapper.getErrorMessage()); | ||
135 | return FALSE; | ||
136 | } | ||
137 | |||
138 | // Resize internal buffer and copy from temp | ||
139 | U32 encodedSize = pngWrapper.getFinalSize(); | ||
140 | allocateData(encodedSize); | ||
141 | memcpy(getData(), mTmpWriteBuffer, encodedSize); | ||
142 | |||
143 | delete[] mTmpWriteBuffer; | ||
144 | |||
145 | return TRUE; | ||
146 | } | ||
147 | |||