File: mipmap.c

package info (click to toggle)
glide 2002.04.10ds1-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 59,540 kB
  • sloc: ansic: 290,125; asm: 23,305; sh: 8,089; pascal: 3,854; makefile: 1,276; perl: 73
file content (115 lines) | stat: -rw-r--r-- 3,574 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

/*
** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE 
** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com). 
** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
** FULL TEXT OF THE NON-WARRANTY PROVISIONS.  
** 
** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
** THE UNITED STATES.  
** 
** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
**
** $Revision: 1.2 $
** $Date: 2000/06/15 00:11:40 $
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "texusint.h"

#define B0(x)   ((x>>24)&0xFF)
#define B1(x)   ((x>>16)&0xFF)
#define B2(x)   ((x>>8)&0xFF)
#define B3(x)   ((x>>0)&0xFF)

static void
_txImgHalve(int *outdata, int width, int height, int *indata)
{
    unsigned int i,j,k;
    unsigned int w,h, *p,sum,*q;

    if ((outdata == NULL)       ||
        (width   <= 0   )       ||
        (height  <= 0   )       ||
        (width & (width-1)) ||
        (height & (height-1)) ||
        ((width == 1) && (height==1))) return;

    w = width>>1;
    h = height>>1;
    p = (unsigned int *) outdata;
    q = (unsigned int *) indata;

    if ((w == 0) || (h == 0)) {
        // Input and output are a single span each (row or column)
        for (j=0; j<w; j++) {
                sum = B0(q[0]) + B0(q[1]); sum = (sum + 1) >> 1; k = sum;
                sum = B1(q[0]) + B1(q[1]); sum = (sum + 1) >> 1; k = (k << 8) + sum;
                sum = B2(q[0]) + B2(q[1]); sum = (sum + 1) >> 1; k = (k << 8) + sum;
                sum = B3(q[0]) + B3(q[1]); sum = (sum + 1) >> 1; k = (k << 8) + sum;
                *p++ = k;
                q += 2;
        }
        return;
    }

    for (i=0; i<h; i++) {
    for (j=0; j<w; j++) {
        sum = B0(q[0]) + B0(q[1]) + B0(q[width]) + B0(q[width+1]);
        sum = (sum + 2) >> 2;   // add 2 to round, then divide by 4
        k = sum;
        sum = B1(q[0]) + B1(q[1]) + B1(q[width]) + B1(q[width+1]);
        sum = (sum + 2) >> 2;   // add 2 to round, then divide by 4
        k = (k<<8) + sum;
        sum = B2(q[0]) + B2(q[1]) + B2(q[width]) + B2(q[width+1]);
        sum = (sum + 2) >> 2;   // add 2 to round, then divide by 4
        k = (k<<8) + sum;
        sum = B3(q[0]) + B3(q[1]) + B3(q[width]) + B3(q[width+1]);
        sum = (sum + 2) >> 2;   // add 2 to round, then divide by 4
        k = (k<<8) + sum;
        *p++ = k;
        q += 2;
    }
    q += width;
    }
}

void
txMipMipmap(TxMip *txMip)
{
    int         i, w, h;

    w = txMip->width;
    h = txMip->height;

    if( txVerbose )
      {
        printf("Mipmapping: .."); fflush(stdout);
        printf(" %dx%d", w, h);
      }
    for (i=1; i< txMip->depth; i++) {
        _txImgHalve (txMip->data[i], w, h, txMip->data[i-1]);
        if (w > 1) w >>= 1;
        if (h > 1) h >>= 1;
        if( txVerbose )
          {
            printf(" %dx%d", w, h); fflush(stdout);
          }
    }
    if( txVerbose )
      {
        printf(".\n");
      }
}