summaryrefslogtreecommitdiff
path: root/indra/llimage
diff options
context:
space:
mode:
authorTofu Linden <tofu.linden@lindenlab.com>2010-04-21 10:29:03 +0100
committerTofu Linden <tofu.linden@lindenlab.com>2010-04-21 10:29:03 +0100
commitf464c663c6a2dfb602bc75ce329313c3539c75d6 (patch)
treef031169999bdba72e442faea7a6179fe58efed8d /indra/llimage
parentc34bb6f62b0ce623074378173fa9693d938eeb89 (diff)
parentea64706e796e90d0cd775c2113c949c2b56c898c (diff)
merge from PE's viewer-trunk
Diffstat (limited to 'indra/llimage')
-rw-r--r--indra/llimage/CMakeLists.txt2
-rw-r--r--indra/llimage/llimagedimensionsinfo.cpp139
-rw-r--r--indra/llimage/llimagedimensionsinfo.h139
3 files changed, 280 insertions, 0 deletions
diff --git a/indra/llimage/CMakeLists.txt b/indra/llimage/CMakeLists.txt
index 22be4078a1..a69621a57b 100644
--- a/indra/llimage/CMakeLists.txt
+++ b/indra/llimage/CMakeLists.txt
@@ -21,6 +21,7 @@ include_directories(
set(llimage_SOURCE_FILES
llimagebmp.cpp
llimage.cpp
+ llimagedimensionsinfo.cpp
llimagedxt.cpp
llimagej2c.cpp
llimagejpeg.cpp
@@ -35,6 +36,7 @@ set(llimage_HEADER_FILES
llimage.h
llimagebmp.h
+ llimagedimensionsinfo.h
llimagedxt.h
llimagej2c.h
llimagejpeg.h
diff --git a/indra/llimage/llimagedimensionsinfo.cpp b/indra/llimage/llimagedimensionsinfo.cpp
new file mode 100644
index 0000000000..890b49b50a
--- /dev/null
+++ b/indra/llimage/llimagedimensionsinfo.cpp
@@ -0,0 +1,139 @@
+/**
+ * @file llimagedimensionsinfo.cpp
+ *
+ * $LicenseInfo:firstyear=2002&license=viewergpl$
+ *
+ * Copyright (c) 2002-2009, Linden Research, Inc.
+ *
+ * Second Life Viewer Source Code
+ * The source code in this file ("Source Code") is provided by Linden Lab
+ * to you under the terms of the GNU General Public License, version 2.0
+ * ("GPL"), unless you have obtained a separate licensing agreement
+ * ("Other License"), formally executed by you and Linden Lab. Terms of
+ * the GPL can be found in doc/GPL-license.txt in this distribution, or
+ * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
+ *
+ * There are special exceptions to the terms and conditions of the GPL as
+ * it is applied to this Source Code. View the full text of the exception
+ * in the file doc/FLOSS-exception.txt in this software distribution, or
+ * online at
+ * http://secondlifegrid.net/programs/open_source/licensing/flossexception
+ *
+ * By copying, modifying or distributing this software, you acknowledge
+ * that you have read and understood your obligations described above,
+ * and agree to abide by those obligations.
+ *
+ * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
+ * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
+ * COMPLETENESS OR PERFORMANCE.
+ * $/LicenseInfo$
+ */
+
+#include "linden_common.h"
+#include "stdtypes.h"
+
+#include "llimagejpeg.h"
+
+#include "llimagedimensionsinfo.h"
+
+bool LLImageDimensionsInfo::load(const std::string& src_filename,U32 codec)
+{
+ clean();
+
+ mSrcFilename = src_filename;
+
+ S32 file_size = 0;
+ apr_status_t s = mInfile.open(src_filename, LL_APR_RB, NULL, &file_size);
+
+ if (s != APR_SUCCESS)
+ {
+ setLastError("Unable to open file for reading", src_filename);
+ return false;
+ }
+
+ if (file_size == 0)
+ {
+ setLastError("File is empty",src_filename);
+ return false;
+ }
+
+ switch (codec)
+ {
+ case IMG_CODEC_BMP:
+ return getImageDimensionsBmp();
+ case IMG_CODEC_TGA:
+ return getImageDimensionsTga();
+ case IMG_CODEC_JPEG:
+ return getImageDimensionsJpeg();
+ case IMG_CODEC_PNG:
+ return getImageDimensionsPng();
+ default:
+ return false;
+
+ }
+}
+
+
+bool LLImageDimensionsInfo::getImageDimensionsBmp()
+{
+ const S32 BMP_FILE_HEADER_SIZE = 14;
+
+ mInfile.seek(APR_CUR,BMP_FILE_HEADER_SIZE+4);
+ mWidth = read_reverse_s32();
+ mHeight = read_reverse_s32();
+
+ return true;
+}
+
+bool LLImageDimensionsInfo::getImageDimensionsTga()
+{
+ const S32 TGA_FILE_HEADER_SIZE = 12;
+
+ mInfile.seek(APR_CUR,TGA_FILE_HEADER_SIZE);
+ mWidth = read_byte() | read_byte() << 8;
+ mHeight = read_byte() | read_byte() << 8;
+
+ return true;
+}
+
+bool LLImageDimensionsInfo::getImageDimensionsPng()
+{
+ const S32 PNG_FILE_MARKER_SIZE = 8;
+
+ mInfile.seek(APR_CUR,PNG_FILE_MARKER_SIZE + 8/*header offset+chunk length+chunk type*/);
+ mWidth = read_s32();
+ mHeight = read_s32();
+
+ return true;
+}
+
+
+bool LLImageDimensionsInfo::getImageDimensionsJpeg()
+{
+ clean();
+ FILE *fp = fopen (mSrcFilename.c_str(), "rb");
+ if (fp == NULL)
+ {
+ setLastError("Unable to open file for reading", mSrcFilename);
+ return false;
+ }
+ /* Init jpeg */
+ jpeg_error_mgr jerr;
+ jpeg_decompress_struct cinfo;
+ cinfo.err = jpeg_std_error(&jerr);
+
+ jpeg_create_decompress (&cinfo);
+ jpeg_stdio_src (&cinfo, fp);
+ jpeg_read_header (&cinfo, TRUE);
+ cinfo.out_color_space = JCS_RGB;
+ jpeg_start_decompress (&cinfo);
+
+ mHeight = cinfo.output_width;
+ mHeight = cinfo.output_height;
+
+ jpeg_destroy_decompress(&cinfo);
+ fclose(fp);
+
+ return true;
+}
+
diff --git a/indra/llimage/llimagedimensionsinfo.h b/indra/llimage/llimagedimensionsinfo.h
new file mode 100644
index 0000000000..9e69635c1e
--- /dev/null
+++ b/indra/llimage/llimagedimensionsinfo.h
@@ -0,0 +1,139 @@
+/**
+ * @file llimagedimentionsinfo.h
+ *
+ * $LicenseInfo:firstyear=2001&license=viewergpl$
+ *
+ * Copyright (c) 2001-2009, Linden Research, Inc.
+ *
+ * Second Life Viewer Source Code
+ * The source code in this file ("Source Code") is provided by Linden Lab
+ * to you under the terms of the GNU General Public License, version 2.0
+ * ("GPL"), unless you have obtained a separate licensing agreement
+ * ("Other License"), formally executed by you and Linden Lab. Terms of
+ * the GPL can be found in doc/GPL-license.txt in this distribution, or
+ * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
+ *
+ * There are special exceptions to the terms and conditions of the GPL as
+ * it is applied to this Source Code. View the full text of the exception
+ * in the file doc/FLOSS-exception.txt in this software distribution, or
+ * online at
+ * http://secondlifegrid.net/programs/open_source/licensing/flossexception
+ *
+ * By copying, modifying or distributing this software, you acknowledge
+ * that you have read and understood your obligations described above,
+ * and agree to abide by those obligations.
+ *
+ * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
+ * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
+ * COMPLETENESS OR PERFORMANCE.
+ * $/LicenseInfo$
+ */
+
+
+#ifndef LL_LLIMAGEDIMENSIONSINFO_H
+#define LL_LLIMAGEDIMENSIONSINFO_H
+
+//-----------------------------------------------------------------------------
+// LLImageDimensionsInfo
+// helper class to get image dimensions WITHOUT loading image to memore
+// usefull when image may be too large...
+//-----------------------------------------------------------------------------
+class LLImageDimensionsInfo
+{
+public:
+ LLImageDimensionsInfo():
+ mData(NULL)
+ ,mHeight(0)
+ ,mWidth(0)
+ {}
+ ~LLImageDimensionsInfo()
+ {
+ clean();
+ }
+
+ bool load(const std::string& src_filename,U32 codec);
+ S32 getWidth() const { return mWidth;}
+ S32 getHeight() const { return mHeight;}
+
+ const std::string& getLastError()
+ {
+ return mLastError;
+ }
+protected:
+
+ void clean()
+ {
+ mInfile.close();
+ delete[] mData;
+ mData = NULL;
+ mWidth = 0;
+ mHeight = 0;
+ }
+
+ U8* getData()
+ {
+ return mData;
+ }
+
+
+ void setLastError(const std::string& message, const std::string& filename)
+ {
+ std::string error = message;
+ if (!filename.empty())
+ error += std::string(" FILE: ") + filename;
+ mLastError = error;
+ }
+
+
+ bool getImageDimensionsBmp();
+ bool getImageDimensionsTga();
+ bool getImageDimensionsPng();
+ bool getImageDimensionsJpeg();
+
+ S32 read_s32()
+ {
+ char p[4];
+ mInfile.read(&p[0],4);
+ S32 temp = (((S32)p[3]) & 0x000000FF) |
+ (((S32)p[2] << 8 ) & 0x0000FF00) |
+ (((S32)p[1] << 16) & 0x00FF0000) |
+ (((S32)p[0] << 24) & 0xFF000000);
+
+ return temp;
+ }
+ S32 read_reverse_s32()
+ {
+ char p[4];
+ mInfile.read(&p[0],4);
+ S32 temp = (((S32)p[0]) & 0x000000FF) |
+ (((S32)p[1] << 8 ) & 0x0000FF00) |
+ (((S32)p[2] << 16) & 0x00FF0000) |
+ (((S32)p[3] << 24) & 0xFF000000);
+
+ return temp;
+ }
+
+ U8 read_byte()
+ {
+ U8 bt;
+ mInfile.read(&bt,1);
+ return bt;
+ }
+
+ U16 read_short()
+ {
+ return read_byte() << 8 | read_byte();
+ }
+
+protected:
+ LLAPRFile mInfile ;
+ std::string mSrcFilename;
+
+ std::string mLastError;
+
+ U8* mData;
+
+ S32 mWidth;
+ S32 mHeight;
+};
+#endif