1 | /* $Id: mipmap.c,v 1.2 2001-09-05 14:30:45 bird Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | ** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
|
---|
5 | ** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
|
---|
6 | ** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
|
---|
7 | ** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE
|
---|
8 | ** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com).
|
---|
9 | ** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
|
---|
10 | ** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
|
---|
11 | ** FULL TEXT OF THE NON-WARRANTY PROVISIONS.
|
---|
12 | **
|
---|
13 | ** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
|
---|
14 | ** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
|
---|
15 | ** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
|
---|
16 | ** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
|
---|
17 | ** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
|
---|
18 | ** THE UNITED STATES.
|
---|
19 | **
|
---|
20 | ** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
|
---|
21 | **
|
---|
22 | ** $Revision: 1.2 $
|
---|
23 | ** $Date: 2001-09-05 14:30:45 $
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <math.h>
|
---|
30 |
|
---|
31 | #include "texusint.h"
|
---|
32 |
|
---|
33 | #define B0(x) ((x>>24)&0xFF)
|
---|
34 | #define B1(x) ((x>>16)&0xFF)
|
---|
35 | #define B2(x) ((x>>8)&0xFF)
|
---|
36 | #define B3(x) ((x>>0)&0xFF)
|
---|
37 |
|
---|
38 | static void
|
---|
39 | _txImgHalve(long *outdata, int width, int height, long *indata)
|
---|
40 | {
|
---|
41 | unsigned int i,j,k;
|
---|
42 | unsigned int w,h, *p,sum,*q;
|
---|
43 |
|
---|
44 | if ((outdata == NULL) ||
|
---|
45 | (width <= 0 ) ||
|
---|
46 | (height <= 0 ) ||
|
---|
47 | (width & (width-1)) ||
|
---|
48 | (height & (height-1)) ||
|
---|
49 | ((width == 1) && (height==1))) return;
|
---|
50 |
|
---|
51 | w = width>>1;
|
---|
52 | h = height>>1;
|
---|
53 | p = (unsigned int *) outdata;
|
---|
54 | q = (unsigned int *) indata;
|
---|
55 |
|
---|
56 | if ((w == 0) || (h == 0)) {
|
---|
57 | // Input and output are a single span each (row or column)
|
---|
58 | for (j=0; j<w; j++) {
|
---|
59 | sum = B0(q[0]) + B0(q[1]); sum = (sum + 1) >> 1; k = sum;
|
---|
60 | sum = B1(q[0]) + B1(q[1]); sum = (sum + 1) >> 1; k = (k << 8) + sum;
|
---|
61 | sum = B2(q[0]) + B2(q[1]); sum = (sum + 1) >> 1; k = (k << 8) + sum;
|
---|
62 | sum = B3(q[0]) + B3(q[1]); sum = (sum + 1) >> 1; k = (k << 8) + sum;
|
---|
63 | *p++ = k;
|
---|
64 | q += 2;
|
---|
65 | }
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | for (i=0; i<h; i++) {
|
---|
70 | for (j=0; j<w; j++) {
|
---|
71 | sum = B0(q[0]) + B0(q[1]) + B0(q[width]) + B0(q[width+1]);
|
---|
72 | sum = (sum + 2) >> 2; // add 2 to round, then divide by 4
|
---|
73 | k = sum;
|
---|
74 | sum = B1(q[0]) + B1(q[1]) + B1(q[width]) + B1(q[width+1]);
|
---|
75 | sum = (sum + 2) >> 2; // add 2 to round, then divide by 4
|
---|
76 | k = (k<<8) + sum;
|
---|
77 | sum = B2(q[0]) + B2(q[1]) + B2(q[width]) + B2(q[width+1]);
|
---|
78 | sum = (sum + 2) >> 2; // add 2 to round, then divide by 4
|
---|
79 | k = (k<<8) + sum;
|
---|
80 | sum = B3(q[0]) + B3(q[1]) + B3(q[width]) + B3(q[width+1]);
|
---|
81 | sum = (sum + 2) >> 2; // add 2 to round, then divide by 4
|
---|
82 | k = (k<<8) + sum;
|
---|
83 | *p++ = k;
|
---|
84 | q += 2;
|
---|
85 | }
|
---|
86 | q += width;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void
|
---|
91 | txMipMipmap(TxMip *txMip)
|
---|
92 | {
|
---|
93 | int i, w, h;
|
---|
94 |
|
---|
95 | w = txMip->width;
|
---|
96 | h = txMip->height;
|
---|
97 |
|
---|
98 | if( txVerbose )
|
---|
99 | {
|
---|
100 | printf("Mipmapping: .."); fflush(stdout);
|
---|
101 | printf(" %dx%d", w, h);
|
---|
102 | }
|
---|
103 | for (i=1; i< txMip->depth; i++) {
|
---|
104 | _txImgHalve (txMip->data[i], w, h, txMip->data[i-1]);
|
---|
105 | if (w > 1) w >>= 1;
|
---|
106 | if (h > 1) h >>= 1;
|
---|
107 | if( txVerbose )
|
---|
108 | {
|
---|
109 | printf(" %dx%d", w, h); fflush(stdout);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | if( txVerbose )
|
---|
113 | {
|
---|
114 | printf(".\n");
|
---|
115 | }
|
---|
116 | }
|
---|