1 | /****************************************************************************** |
---|
2 | * Fast DXT - a realtime DXT compression tool |
---|
3 | * |
---|
4 | * Author : Luc Renambot |
---|
5 | * |
---|
6 | * Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
7 | * University of Illinois at Chicago |
---|
8 | * |
---|
9 | * All rights reserved. |
---|
10 | * |
---|
11 | * Redistribution and use in source and binary forms, with or without |
---|
12 | * modification, are permitted provided that the following conditions are met: |
---|
13 | * |
---|
14 | * * Redistributions of source code must retain the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer. |
---|
16 | * * Redistributions in binary form must reproduce the above |
---|
17 | * copyright notice, this list of conditions and the following disclaimer |
---|
18 | * in the documentation and/or other materials provided with the distribution. |
---|
19 | * * Neither the name of the University of Illinois at Chicago nor |
---|
20 | * the names of its contributors may be used to endorse or promote |
---|
21 | * products derived from this software without specific prior written permission. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
27 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
29 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
34 | * |
---|
35 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
36 | * |
---|
37 | *****************************************************************************/ |
---|
38 | |
---|
39 | #include "libdxt.h" |
---|
40 | |
---|
41 | #if defined(__APPLE__) |
---|
42 | #define memalign(x,y) malloc((y)) |
---|
43 | #else |
---|
44 | #include <malloc.h> |
---|
45 | #endif |
---|
46 | |
---|
47 | #include <pthread.h> |
---|
48 | |
---|
49 | typedef struct _work_t { |
---|
50 | int width, height; |
---|
51 | int nbb; |
---|
52 | byte *in, *out; |
---|
53 | } work_t; |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | void *slave1(void *arg) |
---|
58 | { |
---|
59 | work_t *param = (work_t*) arg; |
---|
60 | int nbbytes = 0; |
---|
61 | CompressImageDXT1( param->in, param->out, param->width, param->height, nbbytes); |
---|
62 | param->nbb = nbbytes; |
---|
63 | return NULL; |
---|
64 | } |
---|
65 | |
---|
66 | void *slave5(void *arg) |
---|
67 | { |
---|
68 | work_t *param = (work_t*) arg; |
---|
69 | int nbbytes = 0; |
---|
70 | CompressImageDXT5( param->in, param->out, param->width, param->height, nbbytes); |
---|
71 | param->nbb = nbbytes; |
---|
72 | return NULL; |
---|
73 | } |
---|
74 | |
---|
75 | void *slave5ycocg(void *arg) |
---|
76 | { |
---|
77 | work_t *param = (work_t*) arg; |
---|
78 | int nbbytes = 0; |
---|
79 | CompressImageDXT5YCoCg( param->in, param->out, param->width, param->height, nbbytes); |
---|
80 | param->nbb = nbbytes; |
---|
81 | return NULL; |
---|
82 | } |
---|
83 | |
---|
84 | int CompressDXT(const byte *in, byte *out, int width, int height, int format, int numthreads) |
---|
85 | { |
---|
86 | pthread_t *pid; |
---|
87 | work_t *job; |
---|
88 | int nbbytes; |
---|
89 | |
---|
90 | if ( (numthreads!=1) && (numthreads!=2) && (numthreads!=4) ) { |
---|
91 | fprintf(stderr, "DXT> Errror, number of thread should be 1, 2 or 4\n"); |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | job = new work_t[numthreads]; |
---|
96 | pid = new pthread_t[numthreads]; |
---|
97 | |
---|
98 | for (int k=0;k<numthreads;k++) |
---|
99 | { |
---|
100 | job[k].width = width; |
---|
101 | job[k].height = height/numthreads; |
---|
102 | job[k].nbb = 0; |
---|
103 | job[k].in = (byte*)in + (k*width*4*height/numthreads); |
---|
104 | if (format == FORMAT_DXT1) |
---|
105 | job[k].out = out + (k*width*4*height/(numthreads*8)); |
---|
106 | else |
---|
107 | job[k].out = out + (k*width*4*height/(numthreads*4)); |
---|
108 | |
---|
109 | switch (format) { |
---|
110 | case FORMAT_DXT1: |
---|
111 | pthread_create(&pid[k], NULL, slave1, &job[k]); |
---|
112 | break; |
---|
113 | case FORMAT_DXT5: |
---|
114 | pthread_create(&pid[k], NULL, slave5, &job[k]); |
---|
115 | break; |
---|
116 | case FORMAT_DXT5YCOCG: |
---|
117 | pthread_create(&pid[k], NULL, slave5ycocg, &job[k]); |
---|
118 | break; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | // Join all the threads |
---|
123 | nbbytes = 0; |
---|
124 | for (int k=0;k<numthreads;k++) |
---|
125 | { |
---|
126 | pthread_join(pid[k], NULL); |
---|
127 | nbbytes += job[k].nbb; |
---|
128 | } |
---|
129 | return nbbytes; |
---|
130 | } |
---|