source: trunk/src/testing/app/FileViewer/ImageViewer/imgToDxt.cpp @ 4

Revision 4, 6.2 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/*****************************************************************************************
2 * imgToDxt: converts a single image to a dxt-compressed image
3 *
4 * Copyright (C) 2007 Electronic Visualization Laboratory,
5 * University of Illinois at Chicago
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above
15 *    copyright notice, this list of conditions and the following disclaimer
16 *    in the documentation and/or other materials provided with the distribution.
17 *  * Neither the name of the University of Illinois at Chicago nor
18 *    the names of its contributors may be used to endorse or promote
19 *    products derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Direct questions, comments etc to www.evl.uic.edu/cavern/forum
34 *
35 * Author: Ratko Jagodic
36 *
37 *****************************************************************************************/
38
39
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <string>
44#include <wand/magick-wand.h>
45
46//#if MagickLibVersion >= 0x645
47//#define MagickGetImagePixels MagickGetAuthenticPixels
48//#endif
49
50// for dxt compression
51#include "libdxt.h"
52
53
54using namespace std;
55
56
57#define ThrowWandException(wand)                \
58  {                                             \
59    char                                        \
60      *description;                             \
61                                                \
62    ExceptionType                               \
63      severity;                                 \
64                                                    \
65    description=MagickGetException(wand,&severity);                     \
66    (void) fprintf(stderr,"%s %s %ld %s\n",GetMagickModule(),description); \
67    description=(char *) MagickRelinquishMemory(description);           \
68    exit(-1);                                                           \
69  }
70
71
72// -----------------------------------------------------------------------------
73
74
75
76void writeDXT(string fileName, byte *buffer, unsigned int width,
77              unsigned int height, unsigned int numBytes)
78{
79    fprintf(stderr, "\nImgToDxt: Writing DXT to file... %u bytes", numBytes);
80
81    unsigned long r;
82    FILE *f = fopen(fileName.data(), "wb");
83
84    if (f != NULL)
85    {
86        // write the size of the image in the first 8 bytes
87        r = fwrite(&width, sizeof(unsigned int), 1, f);
88        r = fwrite(&height, sizeof(unsigned int), 1, f);
89        r = fwrite(&numBytes, sizeof(unsigned int), 1, f);
90       
91        // write the buffer out to the file
92        r = fwrite(buffer, 1, numBytes, f);
93        fclose(f);
94    }
95    else
96        fprintf(stderr, "\nImgToDxt: ERROR: Unable to write DXT file. Check dir permissions.");
97}
98
99
100
101void getRGBA(string fileName, byte **rgba, unsigned int &width,
102              unsigned int &height)
103{
104    // use ImageMagick to read all other formats
105    MagickBooleanType status; 
106    MagickWand *wand;
107   
108    // read file
109    wand=NewMagickWand(); 
110    status=MagickReadImage(wand, fileName.data());
111    if (status == MagickFalse)
112        ThrowWandException(wand);
113
114    // get the image size
115    width = MagickGetImageWidth(wand);
116    height = MagickGetImageHeight(wand);
117
118    if (width%4 != 0 || height%4 != 0)
119    {
120        fprintf(stderr, "\nImgToDxt: Image cropped a few pixels to be a multiple of 4 for dxt");
121        width -= width%4;
122        height -= height%4;
123    }
124       
125    // flip the image to have the correct orientation for dxt
126    MagickFlipImage(wand);
127   
128    // get the pixels
129    *rgba = (byte*) memalign(16, width*height*4);
130    memset(*rgba, 0, width*height*4);
131    MagickGetImagePixels(wand, 0, 0, width, height, "RGBA", CharPixel, *rgba);
132    DestroyMagickWand(wand);
133}
134
135
136void rgbaToDXT(string fileName, byte **rgba, byte **dxt, unsigned int width,
137               unsigned int height)
138{
139    unsigned int numBytes;
140
141    // compress into DXT
142    *dxt = (byte*) memalign(16, width*height*4/8);
143    memset(*dxt, 0, width*height*4/8);
144    numBytes = CompressDXT(*rgba, *dxt, width, height, FORMAT_DXT1, 1);
145
146    // write this DXT out to a file (change extension to .dxt)
147    string dxtFileName = string(fileName);
148    dxtFileName.resize(fileName.rfind("."));
149    dxtFileName += ".dxt";
150    writeDXT(dxtFileName, *dxt, width, height, numBytes);
151}
152
153
154bool dxtFileExists(string fileName)
155{
156    // replace the extension with .dxt
157    string dxtFileName = string(fileName);
158    dxtFileName.resize(fileName.rfind("."));
159    dxtFileName += ".dxt";
160
161    // check whether the file exists by trying to open it
162    FILE *dxtFile = fopen(dxtFileName.data(), "r");
163    if ( dxtFile == NULL)
164    {
165        fprintf(stderr, "\nImgToDxt: DXT file for %s doesn't exist yet.", fileName.data());
166        return false;
167    }
168    else
169    {
170        fclose(dxtFile);
171        return true;
172    }
173}
174
175
176// -----------------------------------------------------------------------------
177
178
179
180int main(int argc,char **argv)
181{
182    byte *dxt = NULL;   
183    byte *rgba = NULL;
184    unsigned int width, height;  // image size
185     
186   
187    // parse command line arguments
188    if (argc < 2){
189        fprintf(stderr, "\n\nUSAGE: imgToDxt filename\n");
190        return 0;
191    }
192
193    // check file extension
194    string fileName, fileExt;
195    fileName = string(argv[1]);
196    fileExt = fileName.substr(fileName.rfind("."));
197
198    // if image is in DXT load it directly, otherwise compress and load
199    if(fileExt.compare(".dxt") == 0 || dxtFileExists(fileName))   // DXT already exists
200        return(0);
201    else                               // all other image formats
202    {
203        getRGBA(fileName, &rgba, width, height);
204        rgbaToDXT(fileName, &rgba, &dxt, width, height);
205        free(dxt);
206        free(rgba);
207    }
208
209    return(0);
210}
Note: See TracBrowser for help on using the repository browser.