[ create a new paste ] login | about

Link: http://codepad.org/KFdifqun    [ raw code | fork ]

C++, pasted on Mar 19:
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//hq4x filter demo program
//----------------------------------------------------------
//Copyright (C) 2003 MaxSt ( maxst@hiend3d.com )

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this program; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "Image.h"

static uint32_t   RGBtoYUV[1 << 24];
static uint32_t   YUV1, YUV2;
const  uint32_t   Ymask = 0x00FF0000;
const  uint32_t   Umask = 0x0000FF00;
const  uint32_t   Vmask = 0x000000FF;
const  uint32_t   trY   = 0x00300000;
const  uint32_t   trU   = 0x00000700;
const  uint32_t   trV   = 0x00000006;

inline void Interp1(uint32_t * pc, uint32_t c1, uint32_t c2)
{
    *pc = (c1*3+c2) >> 2;
}

inline void Interp2(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3)
{
    *pc = (c1*2+c2+c3) >> 2;
}

inline void Interp3(uint32_t * pc, uint32_t c1, uint32_t c2)
{
    //*pc = (c1*7+c2)/8;

    *pc = ((((c1 & 0x00FF00)*7 + (c2 & 0x00FF00) ) & 0x0007F800) +
            (((c1 & 0xFF00FF)*7 + (c2 & 0xFF00FF) ) & 0x07F807F8)) >> 3;
}

inline void Interp5(uint32_t * pc, uint32_t c1, uint32_t c2)
{
    *pc = (c1+c2) >> 1;
}

inline void Interp6(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3)
{
    //*pc = (c1*5+c2*2+c3)/8;

    *pc = ((((c1 & 0x00FF00)*5 + (c2 & 0x00FF00)*2 + (c3 & 0x00FF00) ) & 0x0007F800) +
            (((c1 & 0xFF00FF)*5 + (c2 & 0xFF00FF)*2 + (c3 & 0xFF00FF) ) & 0x07F807F8)) >> 3;
}

inline void Interp7(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3)
{
    //*pc = (c1*6+c2+c3)/8;

    *pc = ((((c1 & 0x00FF00)*6 + (c2 & 0x00FF00) + (c3 & 0x00FF00) ) & 0x0007F800) +
            (((c1 & 0xFF00FF)*6 + (c2 & 0xFF00FF) + (c3 & 0xFF00FF) ) & 0x07F807F8)) >> 3;
}

inline void Interp8(uint32_t * pc, uint32_t c1, uint32_t c2)
{
    //*pc = (c1*5+c2*3)/8;

    *pc = ((((c1 & 0x00FF00)*5 + (c2 & 0x00FF00)*3 ) & 0x0007F800) +
            (((c1 & 0xFF00FF)*5 + (c2 & 0xFF00FF)*3 ) & 0x07F807F8)) >> 3;
}

#define PIXEL00_0     *dp = w[5];
#define PIXEL00_11    Interp1(dp, w[5], w[4]);
#define PIXEL00_12    Interp1(dp, w[5], w[2]);
#define PIXEL00_20    Interp2(dp, w[5], w[2], w[4]);
#define PIXEL00_50    Interp5(dp, w[2], w[4]);
#define PIXEL00_80    Interp8(dp, w[5], w[1]);
#define PIXEL00_81    Interp8(dp, w[5], w[4]);
#define PIXEL00_82    Interp8(dp, w[5], w[2]);
#define PIXEL01_0     *(dp+1) = w[5];
#define PIXEL01_10    Interp1(dp+1, w[5], w[1]);
#define PIXEL01_12    Interp1(dp+1, w[5], w[2]);
#define PIXEL01_14    Interp1(dp+1, w[2], w[5]);
#define PIXEL01_21    Interp2(dp+1, w[2], w[5], w[4]);
#define PIXEL01_31    Interp3(dp+1, w[5], w[4]);
#define PIXEL01_50    Interp5(dp+1, w[2], w[5]);
#define PIXEL01_60    Interp6(dp+1, w[5], w[2], w[4]);
#define PIXEL01_61    Interp6(dp+1, w[5], w[2], w[1]);
#define PIXEL01_82    Interp8(dp+1, w[5], w[2]);
#define PIXEL01_83    Interp8(dp+1, w[2], w[4]);
#define PIXEL02_0     *(dp+2) = w[5];
#define PIXEL02_10    Interp1(dp+2, w[5], w[3]);
#define PIXEL02_11    Interp1(dp+2, w[5], w[2]);
#define PIXEL02_13    Interp1(dp+2, w[2], w[5]);
#define PIXEL02_21    Interp2(dp+2, w[2], w[5], w[6]);
#define PIXEL02_32    Interp3(dp+2, w[5], w[6]);
#define PIXEL02_50    Interp5(dp+2, w[2], w[5]);
#define PIXEL02_60    Interp6(dp+2, w[5], w[2], w[6]);
#define PIXEL02_61    Interp6(dp+2, w[5], w[2], w[3]);
#define PIXEL02_81    Interp8(dp+2, w[5], w[2]);
#define PIXEL02_83    Interp8(dp+2, w[2], w[6]);
#define PIXEL03_0     *(dp+3) = w[5];
#define PIXEL03_11    Interp1(dp+3, w[5], w[2]);
#define PIXEL03_12    Interp1(dp+3, w[5], w[6]);
#define PIXEL03_20    Interp2(dp+3, w[5], w[2], w[6]);
#define PIXEL03_50    Interp5(dp+3, w[2], w[6]);
#define PIXEL03_80    Interp8(dp+3, w[5], w[3]);
#define PIXEL03_81    Interp8(dp+3, w[5], w[2]);
#define PIXEL03_82    Interp8(dp+3, w[5], w[6]);
#define PIXEL10_0     *(dp+dpL) = w[5];
#define PIXEL10_10    Interp1(dp+dpL, w[5], w[1]);
#define PIXEL10_11    Interp1(dp+dpL, w[5], w[4]);
#define PIXEL10_13    Interp1(dp+dpL, w[4], w[5]);
#define PIXEL10_21    Interp2(dp+dpL, w[4], w[5], w[2]);
#define PIXEL10_32    Interp3(dp+dpL, w[5], w[2]);
#define PIXEL10_50    Interp5(dp+dpL, w[4], w[5]);
#define PIXEL10_60    Interp6(dp+dpL, w[5], w[4], w[2]);
#define PIXEL10_61    Interp6(dp+dpL, w[5], w[4], w[1]);
#define PIXEL10_81    Interp8(dp+dpL, w[5], w[4]);
#define PIXEL10_83    Interp8(dp+dpL, w[4], w[2]);
#define PIXEL11_0     *(dp+dpL+1) = w[5];
#define PIXEL11_30    Interp3(dp+dpL+1, w[5], w[1]);
#define PIXEL11_31    Interp3(dp+dpL+1, w[5], w[4]);
#define PIXEL11_32    Interp3(dp+dpL+1, w[5], w[2]);
#define PIXEL11_70    Interp7(dp+dpL+1, w[5], w[4], w[2]);
#define PIXEL12_0     *(dp+dpL+2) = w[5];
#define PIXEL12_30    Interp3(dp+dpL+2, w[5], w[3]);
#define PIXEL12_31    Interp3(dp+dpL+2, w[5], w[2]);
#define PIXEL12_32    Interp3(dp+dpL+2, w[5], w[6]);
#define PIXEL12_70    Interp7(dp+dpL+2, w[5], w[6], w[2]);
#define PIXEL13_0     *(dp+dpL+3) = w[5];
#define PIXEL13_10    Interp1(dp+dpL+3, w[5], w[3]);
#define PIXEL13_12    Interp1(dp+dpL+3, w[5], w[6]);
#define PIXEL13_14    Interp1(dp+dpL+3, w[6], w[5]);
#define PIXEL13_21    Interp2(dp+dpL+3, w[6], w[5], w[2]);
#define PIXEL13_31    Interp3(dp+dpL+3, w[5], w[2]);
#define PIXEL13_50    Interp5(dp+dpL+3, w[6], w[5]);
#define PIXEL13_60    Interp6(dp+dpL+3, w[5], w[6], w[2]);
#define PIXEL13_61    Interp6(dp+dpL+3, w[5], w[6], w[3]);
#define PIXEL13_82    Interp8(dp+dpL+3, w[5], w[6]);
#define PIXEL13_83    Interp8(dp+dpL+3, w[6], w[2]);
#define PIXEL20_0     *(dp+dpL+dpL) = w[5];
#define PIXEL20_10    Interp1(dp+dpL+dpL, w[5], w[7]);
#define PIXEL20_12    Interp1(dp+dpL+dpL, w[5], w[4]);
#define PIXEL20_14    Interp1(dp+dpL+dpL, w[4], w[5]);
#define PIXEL20_21    Interp2(dp+dpL+dpL, w[4], w[5], w[8]);
#define PIXEL20_31    Interp3(dp+dpL+dpL, w[5], w[8]);
#define PIXEL20_50    Interp5(dp+dpL+dpL, w[4], w[5]);
#define PIXEL20_60    Interp6(dp+dpL+dpL, w[5], w[4], w[8]);
#define PIXEL20_61    Interp6(dp+dpL+dpL, w[5], w[4], w[7]);
#define PIXEL20_82    Interp8(dp+dpL+dpL, w[5], w[4]);
#define PIXEL20_83    Interp8(dp+dpL+dpL, w[4], w[8]);
#define PIXEL21_0     *(dp+dpL+dpL+1) = w[5];
#define PIXEL21_30    Interp3(dp+dpL+dpL+1, w[5], w[7]);
#define PIXEL21_31    Interp3(dp+dpL+dpL+1, w[5], w[8]);
#define PIXEL21_32    Interp3(dp+dpL+dpL+1, w[5], w[4]);
#define PIXEL21_70    Interp7(dp+dpL+dpL+1, w[5], w[4], w[8]);
#define PIXEL22_0     *(dp+dpL+dpL+2) = w[5];
#define PIXEL22_30    Interp3(dp+dpL+dpL+2, w[5], w[9]);
#define PIXEL22_31    Interp3(dp+dpL+dpL+2, w[5], w[6]);
#define PIXEL22_32    Interp3(dp+dpL+dpL+2, w[5], w[8]);
#define PIXEL22_70    Interp7(dp+dpL+dpL+2, w[5], w[6], w[8]);
#define PIXEL23_0     *(dp+dpL+dpL+3) = w[5];
#define PIXEL23_10    Interp1(dp+dpL+dpL+3, w[5], w[9]);
#define PIXEL23_11    Interp1(dp+dpL+dpL+3, w[5], w[6]);
#define PIXEL23_13    Interp1(dp+dpL+dpL+3, w[6], w[5]);
#define PIXEL23_21    Interp2(dp+dpL+dpL+3, w[6], w[5], w[8]);
#define PIXEL23_32    Interp3(dp+dpL+dpL+3, w[5], w[8]);
#define PIXEL23_50    Interp5(dp+dpL+dpL+3, w[6], w[5]);
#define PIXEL23_60    Interp6(dp+dpL+dpL+3, w[5], w[6], w[8]);
#define PIXEL23_61    Interp6(dp+dpL+dpL+3, w[5], w[6], w[9]);
#define PIXEL23_81    Interp8(dp+dpL+dpL+3, w[5], w[6]);
#define PIXEL23_83    Interp8(dp+dpL+dpL+3, w[6], w[8]);
#define PIXEL30_0     *(dp+dpL+dpL+dpL) = w[5];
#define PIXEL30_11    Interp1(dp+dpL+dpL+dpL, w[5], w[8]);
#define PIXEL30_12    Interp1(dp+dpL+dpL+dpL, w[5], w[4]);
#define PIXEL30_20    Interp2(dp+dpL+dpL+dpL, w[5], w[8], w[4]);
#define PIXEL30_50    Interp5(dp+dpL+dpL+dpL, w[8], w[4]);
#define PIXEL30_80    Interp8(dp+dpL+dpL+dpL, w[5], w[7]);
#define PIXEL30_81    Interp8(dp+dpL+dpL+dpL, w[5], w[8]);
#define PIXEL30_82    Interp8(dp+dpL+dpL+dpL, w[5], w[4]);
#define PIXEL31_0     *(dp+dpL+dpL+dpL+1) = w[5];
#define PIXEL31_10    Interp1(dp+dpL+dpL+dpL+1, w[5], w[7]);
#define PIXEL31_11    Interp1(dp+dpL+dpL+dpL+1, w[5], w[8]);
#define PIXEL31_13    Interp1(dp+dpL+dpL+dpL+1, w[8], w[5]);
#define PIXEL31_21    Interp2(dp+dpL+dpL+dpL+1, w[8], w[5], w[4]);
#define PIXEL31_32    Interp3(dp+dpL+dpL+dpL+1, w[5], w[4]);
#define PIXEL31_50    Interp5(dp+dpL+dpL+dpL+1, w[8], w[5]);
#define PIXEL31_60    Interp6(dp+dpL+dpL+dpL+1, w[5], w[8], w[4]);
#define PIXEL31_61    Interp6(dp+dpL+dpL+dpL+1, w[5], w[8], w[7]);
#define PIXEL31_81    Interp8(dp+dpL+dpL+dpL+1, w[5], w[8]);
#define PIXEL31_83    Interp8(dp+dpL+dpL+dpL+1, w[8], w[4]);
#define PIXEL32_0     *(dp+dpL+dpL+dpL+2) = w[5];
#define PIXEL32_10    Interp1(dp+dpL+dpL+dpL+2, w[5], w[9]);
#define PIXEL32_12    Interp1(dp+dpL+dpL+dpL+2, w[5], w[8]);
#define PIXEL32_14    Interp1(dp+dpL+dpL+dpL+2, w[8], w[5]);
#define PIXEL32_21    Interp2(dp+dpL+dpL+dpL+2, w[8], w[5], w[6]);
#define PIXEL32_31    Interp3(dp+dpL+dpL+dpL+2, w[5], w[6]);
#define PIXEL32_50    Interp5(dp+dpL+dpL+dpL+2, w[8], w[5]);
#define PIXEL32_60    Interp6(dp+dpL+dpL+dpL+2, w[5], w[8], w[6]);
#define PIXEL32_61    Interp6(dp+dpL+dpL+dpL+2, w[5], w[8], w[9]);
#define PIXEL32_82    Interp8(dp+dpL+dpL+dpL+2, w[5], w[8]);
#define PIXEL32_83    Interp8(dp+dpL+dpL+dpL+2, w[8], w[6]);
#define PIXEL33_0     *(dp+dpL+dpL+dpL+3) = w[5];
#define PIXEL33_11    Interp1(dp+dpL+dpL+dpL+3, w[5], w[6]);
#define PIXEL33_12    Interp1(dp+dpL+dpL+dpL+3, w[5], w[8]);
#define PIXEL33_20    Interp2(dp+dpL+dpL+dpL+3, w[5], w[8], w[6]);
#define PIXEL33_50    Interp5(dp+dpL+dpL+dpL+3, w[8], w[6]);
#define PIXEL33_80    Interp8(dp+dpL+dpL+dpL+3, w[5], w[9]);
#define PIXEL33_81    Interp8(dp+dpL+dpL+dpL+3, w[5], w[6]);
#define PIXEL33_82    Interp8(dp+dpL+dpL+dpL+3, w[5], w[8]);



inline bool Diff(uint32_t w1, uint32_t w2)
{
    YUV1 = RGBtoYUV[w1];
    YUV2 = RGBtoYUV[w2];
    return ( ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) ||
            ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) ||
            ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) );
}

void hq4x_32( uint8_t * pIn, uint8_t * pOut, int Xres, int Yres, int dpL )
{
    int  i, j, k;
    int  prevline, nextline;
    int  w[10];
    
    uint32_t *sp = (uint32_t *) pIn;
    uint32_t *dp = (uint32_t *) pOut;

    //   +----+----+----+
    //   |    |    |    |
    //   | w1 | w2 | w3 |
    //   +----+----+----+
    //   |    |    |    |
    //   | w4 | w5 | w6 |
    //   +----+----+----+
    //   |    |    |    |
    //   | w7 | w8 | w9 |
    //   +----+----+----+

    for (j=0; j<Yres; j++)
    {
        if (j>0)      prevline = -Xres; else prevline = 0;
        if (j<Yres-1) nextline =  Xres; else nextline = 0;

        for (i=0; i<Xres; i++)
        {
            w[2] = *(sp + prevline);
            w[5] = *sp;
            w[8] = *(sp + nextline);

            if (i>0)
            {
                w[1] = *(sp + prevline - 1);
                w[4] = *(sp - 1);
                w[7] = *(sp + nextline - 1);
            }
            else
            {
                w[1] = w[2];
                w[4] = w[5];
                w[7] = w[8];
            }

            if (i<Xres-1)
            {
                w[3] = *(sp + prevline + 1);
                w[6] = *(sp + 1);
                w[9] = *(sp + nextline + 1);
            }
            else
            {
                w[3] = w[2];
                w[6] = w[5];
                w[9] = w[8];
            }

            int pattern = 0;
            int flag = 1;

            YUV1 = RGBtoYUV[w[5]];

            for (k=1; k<=9; k++)
            {
                if (k==5) continue;

                if ( w[k] != w[5] )
                {
                    YUV2 = RGBtoYUV[w[k]];
                    if ( ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) ||
                            ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) ||
                            ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) )
                        pattern |= flag;
                }
                flag <<= 1;
            }

            switch (pattern)
            {
            // omitted for brevity
            }
            sp++;
            dp += 4;
        }
        dp += (dpL * 3);
    }
}

void InitLUTs(void)
{
    int r, g, b, Y, u, v;

    for (r = 0; r < 256; r++) {
        for (g = 0; g < 256; g++) {
            for (b = 0; b < 256; b++) {
                Y = (int) (0.299*r + 0.587*g + 0.114*b);
                u = (int) (128 - 0.169*r - 0.331*g + 0.5*b); // cb
                v = (int) (128 + 0.5*r - 0.419*g - 0.081*b); // cr
                //Y = (int)( 0.256788f*r + 0.504129f*g + 0.097906f*b + 0.5f) + 16;
        		//u = (int)(-0.148223f*r - 0.290993f*g + 0.439216f*b + 0.5f) + 128;
		        //v = (int)( 0.439216f*r - 0.367788f*g - 0.071427f*b + 0.5f) + 128;
                RGBtoYUV[ (r << 16) + (g << 8) + b ] = (Y << 16) + (u << 8) + v;
            }
        }
    }
}

int main(int argc, char* argv[])
{
    int         nRes;
    CImage      ImageIn;
    CImage      ImageOut;
    char      * szFilenameIn;
    char      * szFilenameOut;

    if (argc <= 2)
    {
        printf("\nUsage: hq4x.exe input.bmp output.bmp\n");
        printf("supports .bmp and .tga formats\n");
        return 1;
    }

    szFilenameIn = argv[1];
    szFilenameOut = argv[2];

    struct stat st;
    if ( stat( szFilenameIn, &st ) == -1 )
    {
        printf( "ERROR: file '%s'\n not found", szFilenameIn );
        return 1;
    }

    if ( ImageIn.Load( szFilenameIn ) != 0 )
    {
        printf( "ERROR: can't load '%s'\n", szFilenameIn );
        return 1;
    }

    printf( "\n%s is %ix%ix%i\n", szFilenameIn, ImageIn.m_Xres, ImageIn.m_Yres, ImageIn.m_BitPerPixel );

    if ( ImageIn.m_BitPerPixel != 32 )
    {
        if ( ImageIn.ConvertTo32() != 0 )
        {
            printf( "ERROR: '%s' conversion to 16 bit failed\n", szFilenameIn );
            return 1;
        }
    }

    if ( ImageOut.Init( ImageIn.m_Xres*4, ImageIn.m_Yres*4, 32 ) != 0 )
    {
        printf( "ERROR: ImageOut.Init()\n" );
        return 1;
    };

    InitLUTs();

    hq4x_32( ImageIn.m_pBitmap, ImageOut.m_pBitmap, ImageIn.m_Xres, ImageIn.m_Yres, ImageOut.m_Xres );

    nRes = ImageOut.Save( szFilenameOut );
    if ( nRes != 0 )
    {
        printf( "ERROR %i: ImageOut.Save(\"%s\")\n", nRes, szFilenameOut );
        return nRes;
    }
    printf( "%s is %ix%ix%i\n", szFilenameOut, ImageOut.m_Xres, ImageOut.m_Yres, ImageOut.m_BitPerPixel );

    printf( "\nOK\n" );
    return 0;
}


Create a new paste based on this one


Comments: