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