src/video/SDL_yuv_sw.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 2859 99210400e8b9
parent 2828 7e5ff6cd05bf
child 3144 0d8d1f870964
permissions -rw-r--r--
Updated copyright date
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1895
    24
/* This is the software implementation of the YUV texture support */
slouken@0
    25
slouken@0
    26
/* This code was derived from code carrying the following copyright notices:
slouken@0
    27
slouken@0
    28
 * Copyright (c) 1995 The Regents of the University of California.
slouken@0
    29
 * All rights reserved.
slouken@0
    30
 * 
slouken@0
    31
 * Permission to use, copy, modify, and distribute this software and its
slouken@0
    32
 * documentation for any purpose, without fee, and without written agreement is
slouken@0
    33
 * hereby granted, provided that the above copyright notice and the following
slouken@0
    34
 * two paragraphs appear in all copies of this software.
slouken@0
    35
 * 
slouken@0
    36
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
slouken@0
    37
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
slouken@0
    38
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
slouken@0
    39
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
slouken@0
    40
 * 
slouken@0
    41
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
slouken@0
    42
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
slouken@0
    43
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
slouken@0
    44
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
slouken@0
    45
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
slouken@0
    46
slouken@0
    47
 * Copyright (c) 1995 Erik Corry
slouken@0
    48
 * All rights reserved.
slouken@0
    49
 * 
slouken@0
    50
 * Permission to use, copy, modify, and distribute this software and its
slouken@0
    51
 * documentation for any purpose, without fee, and without written agreement is
slouken@0
    52
 * hereby granted, provided that the above copyright notice and the following
slouken@0
    53
 * two paragraphs appear in all copies of this software.
slouken@0
    54
 * 
slouken@0
    55
 * IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
slouken@0
    56
 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
slouken@0
    57
 * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN ADVISED
slouken@0
    58
 * OF THE POSSIBILITY OF SUCH DAMAGE.
slouken@0
    59
 * 
slouken@0
    60
 * ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
slouken@0
    61
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
slouken@0
    62
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
slouken@0
    63
 * BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
slouken@0
    64
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
slouken@0
    65
slouken@0
    66
 * Portions of this software Copyright (c) 1995 Brown University.
slouken@0
    67
 * All rights reserved.
slouken@0
    68
 * 
slouken@0
    69
 * Permission to use, copy, modify, and distribute this software and its
slouken@0
    70
 * documentation for any purpose, without fee, and without written agreement
slouken@0
    71
 * is hereby granted, provided that the above copyright notice and the
slouken@0
    72
 * following two paragraphs appear in all copies of this software.
slouken@0
    73
 * 
slouken@0
    74
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
slouken@0
    75
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
slouken@0
    76
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
slouken@0
    77
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
slouken@0
    78
 * 
slouken@0
    79
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
slouken@0
    80
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
slouken@0
    81
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
slouken@0
    82
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
slouken@0
    83
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
slouken@0
    84
 */
slouken@0
    85
slouken@0
    86
#include "SDL_video.h"
slouken@739
    87
#include "SDL_cpuinfo.h"
slouken@0
    88
#include "SDL_yuv_sw_c.h"
slouken@0
    89
slouken@1895
    90
slouken@1895
    91
struct SDL_SW_YUVTexture
slouken@1895
    92
{
slouken@2786
    93
    Uint32 format;
slouken@1895
    94
    Uint32 target_format;
slouken@2786
    95
    int w, h;
slouken@1895
    96
    Uint8 *pixels;
slouken@1895
    97
    int *colortab;
slouken@1895
    98
    Uint32 *rgb_2_pix;
slouken@1895
    99
    void (*Display1X) (int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   100
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   101
                       unsigned char *cb, unsigned char *out,
slouken@1895
   102
                       int rows, int cols, int mod);
slouken@1895
   103
    void (*Display2X) (int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   104
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   105
                       unsigned char *cb, unsigned char *out,
slouken@1895
   106
                       int rows, int cols, int mod);
slouken@1895
   107
slouken@1895
   108
    /* These are just so we don't have to allocate them separately */
slouken@1895
   109
    Uint16 pitches[3];
slouken@1895
   110
    Uint8 *planes[3];
slouken@1895
   111
slouken@1895
   112
    /* This is a temporary surface in case we have to stretch copy */
slouken@1895
   113
    SDL_Surface *stretch;
slouken@1895
   114
    SDL_Surface *display;
slouken@0
   115
};
slouken@0
   116
slouken@0
   117
/* The colorspace conversion functions */
slouken@0
   118
icculus@2197
   119
#if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
slouken@1895
   120
extern void Color565DitherYV12MMX1X(int *colortab, Uint32 * rgb_2_pix,
slouken@0
   121
                                    unsigned char *lum, unsigned char *cr,
slouken@0
   122
                                    unsigned char *cb, unsigned char *out,
slouken@1895
   123
                                    int rows, int cols, int mod);
slouken@1895
   124
extern void ColorRGBDitherYV12MMX1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   125
                                    unsigned char *lum, unsigned char *cr,
slouken@1895
   126
                                    unsigned char *cb, unsigned char *out,
slouken@1895
   127
                                    int rows, int cols, int mod);
slouken@1895
   128
#endif
slouken@1895
   129
slouken@1895
   130
static void
slouken@1895
   131
Color16DitherYV12Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   132
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   133
                       unsigned char *cb, unsigned char *out,
slouken@1895
   134
                       int rows, int cols, int mod)
slouken@0
   135
{
slouken@1895
   136
    unsigned short *row1;
slouken@1895
   137
    unsigned short *row2;
slouken@1895
   138
    unsigned char *lum2;
slouken@0
   139
    int x, y;
slouken@0
   140
    int cr_r;
slouken@0
   141
    int crb_g;
slouken@0
   142
    int cb_b;
slouken@0
   143
    int cols_2 = cols / 2;
slouken@0
   144
slouken@1895
   145
    row1 = (unsigned short *) out;
slouken@0
   146
    row2 = row1 + cols + mod;
slouken@0
   147
    lum2 = lum + cols;
slouken@0
   148
slouken@0
   149
    mod += cols + mod;
slouken@0
   150
slouken@0
   151
    y = rows / 2;
slouken@1895
   152
    while (y--) {
slouken@0
   153
        x = cols_2;
slouken@1895
   154
        while (x--) {
slouken@0
   155
            register int L;
slouken@0
   156
slouken@1895
   157
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   158
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   159
                + colortab[*cb + 2 * 256];
slouken@1895
   160
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   161
            ++cr;
slouken@1895
   162
            ++cb;
slouken@0
   163
slouken@0
   164
            L = *lum++;
slouken@1895
   165
            *row1++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   166
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   167
                                        rgb_2_pix[L + cb_b]);
slouken@0
   168
slouken@0
   169
            L = *lum++;
slouken@1895
   170
            *row1++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   171
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   172
                                        rgb_2_pix[L + cb_b]);
slouken@0
   173
slouken@0
   174
slouken@0
   175
            /* Now, do second row.  */
slouken@0
   176
slouken@0
   177
            L = *lum2++;
slouken@1895
   178
            *row2++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   179
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   180
                                        rgb_2_pix[L + cb_b]);
slouken@0
   181
slouken@0
   182
            L = *lum2++;
slouken@1895
   183
            *row2++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   184
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   185
                                        rgb_2_pix[L + cb_b]);
slouken@0
   186
        }
slouken@0
   187
slouken@0
   188
        /*
slouken@0
   189
         * These values are at the start of the next line, (due
slouken@0
   190
         * to the ++'s above),but they need to be at the start
slouken@0
   191
         * of the line after that.
slouken@0
   192
         */
slouken@1895
   193
        lum += cols;
slouken@0
   194
        lum2 += cols;
slouken@0
   195
        row1 += mod;
slouken@0
   196
        row2 += mod;
slouken@0
   197
    }
slouken@0
   198
}
slouken@0
   199
slouken@1895
   200
static void
slouken@1895
   201
Color24DitherYV12Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   202
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   203
                       unsigned char *cb, unsigned char *out,
slouken@1895
   204
                       int rows, int cols, int mod)
slouken@0
   205
{
slouken@0
   206
    unsigned int value;
slouken@1895
   207
    unsigned char *row1;
slouken@1895
   208
    unsigned char *row2;
slouken@1895
   209
    unsigned char *lum2;
slouken@0
   210
    int x, y;
slouken@0
   211
    int cr_r;
slouken@0
   212
    int crb_g;
slouken@0
   213
    int cb_b;
slouken@0
   214
    int cols_2 = cols / 2;
slouken@0
   215
slouken@0
   216
    row1 = out;
slouken@1895
   217
    row2 = row1 + cols * 3 + mod * 3;
slouken@0
   218
    lum2 = lum + cols;
slouken@0
   219
slouken@0
   220
    mod += cols + mod;
slouken@0
   221
    mod *= 3;
slouken@0
   222
slouken@0
   223
    y = rows / 2;
slouken@1895
   224
    while (y--) {
slouken@0
   225
        x = cols_2;
slouken@1895
   226
        while (x--) {
slouken@0
   227
            register int L;
slouken@0
   228
slouken@1895
   229
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   230
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   231
                + colortab[*cb + 2 * 256];
slouken@1895
   232
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   233
            ++cr;
slouken@1895
   234
            ++cb;
slouken@0
   235
slouken@0
   236
            L = *lum++;
slouken@1895
   237
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   238
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   239
            *row1++ = (value) & 0xFF;
slouken@1895
   240
            *row1++ = (value >> 8) & 0xFF;
slouken@0
   241
            *row1++ = (value >> 16) & 0xFF;
slouken@0
   242
slouken@0
   243
            L = *lum++;
slouken@1895
   244
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   245
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   246
            *row1++ = (value) & 0xFF;
slouken@1895
   247
            *row1++ = (value >> 8) & 0xFF;
slouken@0
   248
            *row1++ = (value >> 16) & 0xFF;
slouken@0
   249
slouken@0
   250
slouken@0
   251
            /* Now, do second row.  */
slouken@0
   252
slouken@0
   253
            L = *lum2++;
slouken@1895
   254
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   255
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   256
            *row2++ = (value) & 0xFF;
slouken@1895
   257
            *row2++ = (value >> 8) & 0xFF;
slouken@0
   258
            *row2++ = (value >> 16) & 0xFF;
slouken@0
   259
slouken@0
   260
            L = *lum2++;
slouken@1895
   261
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   262
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   263
            *row2++ = (value) & 0xFF;
slouken@1895
   264
            *row2++ = (value >> 8) & 0xFF;
slouken@0
   265
            *row2++ = (value >> 16) & 0xFF;
slouken@0
   266
        }
slouken@0
   267
slouken@0
   268
        /*
slouken@0
   269
         * These values are at the start of the next line, (due
slouken@0
   270
         * to the ++'s above),but they need to be at the start
slouken@0
   271
         * of the line after that.
slouken@0
   272
         */
slouken@1895
   273
        lum += cols;
slouken@0
   274
        lum2 += cols;
slouken@0
   275
        row1 += mod;
slouken@0
   276
        row2 += mod;
slouken@0
   277
    }
slouken@0
   278
}
slouken@0
   279
slouken@1895
   280
static void
slouken@1895
   281
Color32DitherYV12Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   282
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   283
                       unsigned char *cb, unsigned char *out,
slouken@1895
   284
                       int rows, int cols, int mod)
slouken@0
   285
{
slouken@1895
   286
    unsigned int *row1;
slouken@1895
   287
    unsigned int *row2;
slouken@1895
   288
    unsigned char *lum2;
slouken@0
   289
    int x, y;
slouken@0
   290
    int cr_r;
slouken@0
   291
    int crb_g;
slouken@0
   292
    int cb_b;
slouken@0
   293
    int cols_2 = cols / 2;
slouken@0
   294
slouken@1895
   295
    row1 = (unsigned int *) out;
slouken@0
   296
    row2 = row1 + cols + mod;
slouken@0
   297
    lum2 = lum + cols;
slouken@0
   298
slouken@0
   299
    mod += cols + mod;
slouken@0
   300
slouken@0
   301
    y = rows / 2;
slouken@1895
   302
    while (y--) {
slouken@0
   303
        x = cols_2;
slouken@1895
   304
        while (x--) {
slouken@0
   305
            register int L;
slouken@0
   306
slouken@1895
   307
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   308
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   309
                + colortab[*cb + 2 * 256];
slouken@1895
   310
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   311
            ++cr;
slouken@1895
   312
            ++cb;
slouken@0
   313
slouken@0
   314
            L = *lum++;
slouken@1895
   315
            *row1++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   316
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   317
slouken@0
   318
            L = *lum++;
slouken@1895
   319
            *row1++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   320
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   321
slouken@0
   322
slouken@0
   323
            /* Now, do second row.  */
slouken@0
   324
slouken@0
   325
            L = *lum2++;
slouken@1895
   326
            *row2++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   327
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   328
slouken@0
   329
            L = *lum2++;
slouken@1895
   330
            *row2++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   331
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   332
        }
slouken@0
   333
slouken@0
   334
        /*
slouken@0
   335
         * These values are at the start of the next line, (due
slouken@0
   336
         * to the ++'s above),but they need to be at the start
slouken@0
   337
         * of the line after that.
slouken@0
   338
         */
slouken@1895
   339
        lum += cols;
slouken@0
   340
        lum2 += cols;
slouken@0
   341
        row1 += mod;
slouken@0
   342
        row2 += mod;
slouken@0
   343
    }
slouken@0
   344
}
slouken@0
   345
slouken@0
   346
/*
slouken@0
   347
 * In this function I make use of a nasty trick. The tables have the lower
slouken@0
   348
 * 16 bits replicated in the upper 16. This means I can write ints and get
slouken@0
   349
 * the horisontal doubling for free (almost).
slouken@0
   350
 */
slouken@1895
   351
static void
slouken@1895
   352
Color16DitherYV12Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   353
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   354
                       unsigned char *cb, unsigned char *out,
slouken@1895
   355
                       int rows, int cols, int mod)
slouken@0
   356
{
slouken@1895
   357
    unsigned int *row1 = (unsigned int *) out;
slouken@1895
   358
    const int next_row = cols + (mod / 2);
slouken@1895
   359
    unsigned int *row2 = row1 + 2 * next_row;
slouken@1895
   360
    unsigned char *lum2;
slouken@0
   361
    int x, y;
slouken@0
   362
    int cr_r;
slouken@0
   363
    int crb_g;
slouken@0
   364
    int cb_b;
slouken@0
   365
    int cols_2 = cols / 2;
slouken@0
   366
slouken@0
   367
    lum2 = lum + cols;
slouken@0
   368
slouken@1895
   369
    mod = (next_row * 3) + (mod / 2);
slouken@0
   370
slouken@0
   371
    y = rows / 2;
slouken@1895
   372
    while (y--) {
slouken@0
   373
        x = cols_2;
slouken@1895
   374
        while (x--) {
slouken@0
   375
            register int L;
slouken@0
   376
slouken@1895
   377
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   378
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   379
                + colortab[*cb + 2 * 256];
slouken@1895
   380
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   381
            ++cr;
slouken@1895
   382
            ++cb;
slouken@0
   383
slouken@0
   384
            L = *lum++;
slouken@1895
   385
            row1[0] = row1[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   386
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   387
                                        rgb_2_pix[L + cb_b]);
slouken@0
   388
            row1++;
slouken@0
   389
slouken@0
   390
            L = *lum++;
slouken@1895
   391
            row1[0] = row1[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   392
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   393
                                        rgb_2_pix[L + cb_b]);
slouken@0
   394
            row1++;
slouken@0
   395
slouken@0
   396
slouken@0
   397
            /* Now, do second row. */
slouken@0
   398
slouken@0
   399
            L = *lum2++;
slouken@1895
   400
            row2[0] = row2[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   401
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   402
                                        rgb_2_pix[L + cb_b]);
slouken@0
   403
            row2++;
slouken@0
   404
slouken@0
   405
            L = *lum2++;
slouken@1895
   406
            row2[0] = row2[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   407
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   408
                                        rgb_2_pix[L + cb_b]);
slouken@0
   409
            row2++;
slouken@0
   410
        }
slouken@0
   411
slouken@0
   412
        /*
slouken@0
   413
         * These values are at the start of the next line, (due
slouken@0
   414
         * to the ++'s above),but they need to be at the start
slouken@0
   415
         * of the line after that.
slouken@0
   416
         */
slouken@1895
   417
        lum += cols;
slouken@0
   418
        lum2 += cols;
slouken@0
   419
        row1 += mod;
slouken@0
   420
        row2 += mod;
slouken@0
   421
    }
slouken@0
   422
}
slouken@0
   423
slouken@1895
   424
static void
slouken@1895
   425
Color24DitherYV12Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   426
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   427
                       unsigned char *cb, unsigned char *out,
slouken@1895
   428
                       int rows, int cols, int mod)
slouken@0
   429
{
slouken@0
   430
    unsigned int value;
slouken@1895
   431
    unsigned char *row1 = out;
slouken@1895
   432
    const int next_row = (cols * 2 + mod) * 3;
slouken@1895
   433
    unsigned char *row2 = row1 + 2 * next_row;
slouken@1895
   434
    unsigned char *lum2;
slouken@0
   435
    int x, y;
slouken@0
   436
    int cr_r;
slouken@0
   437
    int crb_g;
slouken@0
   438
    int cb_b;
slouken@0
   439
    int cols_2 = cols / 2;
slouken@0
   440
slouken@0
   441
    lum2 = lum + cols;
slouken@0
   442
slouken@1895
   443
    mod = next_row * 3 + mod * 3;
slouken@0
   444
slouken@0
   445
    y = rows / 2;
slouken@1895
   446
    while (y--) {
slouken@0
   447
        x = cols_2;
slouken@1895
   448
        while (x--) {
slouken@0
   449
            register int L;
slouken@0
   450
slouken@1895
   451
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   452
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   453
                + colortab[*cb + 2 * 256];
slouken@1895
   454
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   455
            ++cr;
slouken@1895
   456
            ++cb;
slouken@0
   457
slouken@0
   458
            L = *lum++;
slouken@1895
   459
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   460
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   461
            row1[0 + 0] = row1[3 + 0] = row1[next_row + 0] =
slouken@1895
   462
                row1[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   463
            row1[0 + 1] = row1[3 + 1] = row1[next_row + 1] =
slouken@1895
   464
                row1[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   465
            row1[0 + 2] = row1[3 + 2] = row1[next_row + 2] =
slouken@1895
   466
                row1[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   467
            row1 += 2 * 3;
slouken@0
   468
slouken@0
   469
            L = *lum++;
slouken@1895
   470
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   471
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   472
            row1[0 + 0] = row1[3 + 0] = row1[next_row + 0] =
slouken@1895
   473
                row1[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   474
            row1[0 + 1] = row1[3 + 1] = row1[next_row + 1] =
slouken@1895
   475
                row1[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   476
            row1[0 + 2] = row1[3 + 2] = row1[next_row + 2] =
slouken@1895
   477
                row1[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   478
            row1 += 2 * 3;
slouken@0
   479
slouken@0
   480
slouken@0
   481
            /* Now, do second row. */
slouken@0
   482
slouken@0
   483
            L = *lum2++;
slouken@1895
   484
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   485
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   486
            row2[0 + 0] = row2[3 + 0] = row2[next_row + 0] =
slouken@1895
   487
                row2[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   488
            row2[0 + 1] = row2[3 + 1] = row2[next_row + 1] =
slouken@1895
   489
                row2[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   490
            row2[0 + 2] = row2[3 + 2] = row2[next_row + 2] =
slouken@1895
   491
                row2[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   492
            row2 += 2 * 3;
slouken@0
   493
slouken@0
   494
            L = *lum2++;
slouken@1895
   495
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   496
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   497
            row2[0 + 0] = row2[3 + 0] = row2[next_row + 0] =
slouken@1895
   498
                row2[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   499
            row2[0 + 1] = row2[3 + 1] = row2[next_row + 1] =
slouken@1895
   500
                row2[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   501
            row2[0 + 2] = row2[3 + 2] = row2[next_row + 2] =
slouken@1895
   502
                row2[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   503
            row2 += 2 * 3;
slouken@0
   504
        }
slouken@0
   505
slouken@0
   506
        /*
slouken@0
   507
         * These values are at the start of the next line, (due
slouken@0
   508
         * to the ++'s above),but they need to be at the start
slouken@0
   509
         * of the line after that.
slouken@0
   510
         */
slouken@1895
   511
        lum += cols;
slouken@0
   512
        lum2 += cols;
slouken@0
   513
        row1 += mod;
slouken@0
   514
        row2 += mod;
slouken@0
   515
    }
slouken@0
   516
}
slouken@0
   517
slouken@1895
   518
static void
slouken@1895
   519
Color32DitherYV12Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   520
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   521
                       unsigned char *cb, unsigned char *out,
slouken@1895
   522
                       int rows, int cols, int mod)
slouken@0
   523
{
slouken@1895
   524
    unsigned int *row1 = (unsigned int *) out;
slouken@1895
   525
    const int next_row = cols * 2 + mod;
slouken@1895
   526
    unsigned int *row2 = row1 + 2 * next_row;
slouken@1895
   527
    unsigned char *lum2;
slouken@0
   528
    int x, y;
slouken@0
   529
    int cr_r;
slouken@0
   530
    int crb_g;
slouken@0
   531
    int cb_b;
slouken@0
   532
    int cols_2 = cols / 2;
slouken@0
   533
slouken@0
   534
    lum2 = lum + cols;
slouken@0
   535
slouken@0
   536
    mod = (next_row * 3) + mod;
slouken@0
   537
slouken@0
   538
    y = rows / 2;
slouken@1895
   539
    while (y--) {
slouken@0
   540
        x = cols_2;
slouken@1895
   541
        while (x--) {
slouken@0
   542
            register int L;
slouken@0
   543
slouken@1895
   544
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   545
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   546
                + colortab[*cb + 2 * 256];
slouken@1895
   547
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   548
            ++cr;
slouken@1895
   549
            ++cb;
slouken@0
   550
slouken@0
   551
            L = *lum++;
slouken@1895
   552
            row1[0] = row1[1] = row1[next_row] = row1[next_row + 1] =
slouken@1895
   553
                (rgb_2_pix[L + cr_r] |
slouken@1895
   554
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   555
            row1 += 2;
slouken@0
   556
slouken@0
   557
            L = *lum++;
slouken@1895
   558
            row1[0] = row1[1] = row1[next_row] = row1[next_row + 1] =
slouken@1895
   559
                (rgb_2_pix[L + cr_r] |
slouken@1895
   560
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   561
            row1 += 2;
slouken@0
   562
slouken@0
   563
slouken@0
   564
            /* Now, do second row. */
slouken@0
   565
slouken@0
   566
            L = *lum2++;
slouken@1895
   567
            row2[0] = row2[1] = row2[next_row] = row2[next_row + 1] =
slouken@1895
   568
                (rgb_2_pix[L + cr_r] |
slouken@1895
   569
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   570
            row2 += 2;
slouken@0
   571
slouken@0
   572
            L = *lum2++;
slouken@1895
   573
            row2[0] = row2[1] = row2[next_row] = row2[next_row + 1] =
slouken@1895
   574
                (rgb_2_pix[L + cr_r] |
slouken@1895
   575
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   576
            row2 += 2;
slouken@0
   577
        }
slouken@0
   578
slouken@0
   579
        /*
slouken@0
   580
         * These values are at the start of the next line, (due
slouken@0
   581
         * to the ++'s above),but they need to be at the start
slouken@0
   582
         * of the line after that.
slouken@0
   583
         */
slouken@1895
   584
        lum += cols;
slouken@0
   585
        lum2 += cols;
slouken@0
   586
        row1 += mod;
slouken@0
   587
        row2 += mod;
slouken@0
   588
    }
slouken@0
   589
}
slouken@0
   590
slouken@1895
   591
static void
slouken@1895
   592
Color16DitherYUY2Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   593
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   594
                       unsigned char *cb, unsigned char *out,
slouken@1895
   595
                       int rows, int cols, int mod)
slouken@0
   596
{
slouken@1895
   597
    unsigned short *row;
slouken@0
   598
    int x, y;
slouken@0
   599
    int cr_r;
slouken@0
   600
    int crb_g;
slouken@0
   601
    int cb_b;
slouken@0
   602
    int cols_2 = cols / 2;
slouken@0
   603
slouken@1895
   604
    row = (unsigned short *) out;
slouken@0
   605
slouken@0
   606
    y = rows;
slouken@1895
   607
    while (y--) {
slouken@0
   608
        x = cols_2;
slouken@1895
   609
        while (x--) {
slouken@0
   610
            register int L;
slouken@0
   611
slouken@1895
   612
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   613
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   614
                + colortab[*cb + 2 * 256];
slouken@1895
   615
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   616
            cr += 4;
slouken@1895
   617
            cb += 4;
slouken@0
   618
slouken@1895
   619
            L = *lum;
slouken@1895
   620
            lum += 2;
slouken@1895
   621
            *row++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   622
                                       rgb_2_pix[L + crb_g] |
slouken@1895
   623
                                       rgb_2_pix[L + cb_b]);
slouken@0
   624
slouken@1895
   625
            L = *lum;
slouken@1895
   626
            lum += 2;
slouken@1895
   627
            *row++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   628
                                       rgb_2_pix[L + crb_g] |
slouken@1895
   629
                                       rgb_2_pix[L + cb_b]);
slouken@0
   630
slouken@0
   631
        }
slouken@0
   632
slouken@0
   633
        row += mod;
slouken@0
   634
    }
slouken@0
   635
}
slouken@0
   636
slouken@1895
   637
static void
slouken@1895
   638
Color24DitherYUY2Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   639
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   640
                       unsigned char *cb, unsigned char *out,
slouken@1895
   641
                       int rows, int cols, int mod)
slouken@0
   642
{
slouken@0
   643
    unsigned int value;
slouken@1895
   644
    unsigned char *row;
slouken@0
   645
    int x, y;
slouken@0
   646
    int cr_r;
slouken@0
   647
    int crb_g;
slouken@0
   648
    int cb_b;
slouken@0
   649
    int cols_2 = cols / 2;
slouken@0
   650
slouken@1895
   651
    row = (unsigned char *) out;
slouken@0
   652
    mod *= 3;
slouken@0
   653
    y = rows;
slouken@1895
   654
    while (y--) {
slouken@0
   655
        x = cols_2;
slouken@1895
   656
        while (x--) {
slouken@0
   657
            register int L;
slouken@0
   658
slouken@1895
   659
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   660
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   661
                + colortab[*cb + 2 * 256];
slouken@1895
   662
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   663
            cr += 4;
slouken@1895
   664
            cb += 4;
slouken@0
   665
slouken@1895
   666
            L = *lum;
slouken@1895
   667
            lum += 2;
slouken@1895
   668
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   669
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   670
            *row++ = (value) & 0xFF;
slouken@1895
   671
            *row++ = (value >> 8) & 0xFF;
slouken@0
   672
            *row++ = (value >> 16) & 0xFF;
slouken@0
   673
slouken@1895
   674
            L = *lum;
slouken@1895
   675
            lum += 2;
slouken@1895
   676
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   677
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   678
            *row++ = (value) & 0xFF;
slouken@1895
   679
            *row++ = (value >> 8) & 0xFF;
slouken@0
   680
            *row++ = (value >> 16) & 0xFF;
slouken@0
   681
slouken@0
   682
        }
slouken@0
   683
        row += mod;
slouken@0
   684
    }
slouken@0
   685
}
slouken@0
   686
slouken@1895
   687
static void
slouken@1895
   688
Color32DitherYUY2Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   689
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   690
                       unsigned char *cb, unsigned char *out,
slouken@1895
   691
                       int rows, int cols, int mod)
slouken@0
   692
{
slouken@1895
   693
    unsigned int *row;
slouken@0
   694
    int x, y;
slouken@0
   695
    int cr_r;
slouken@0
   696
    int crb_g;
slouken@0
   697
    int cb_b;
slouken@0
   698
    int cols_2 = cols / 2;
slouken@0
   699
slouken@1895
   700
    row = (unsigned int *) out;
slouken@0
   701
    y = rows;
slouken@1895
   702
    while (y--) {
slouken@0
   703
        x = cols_2;
slouken@1895
   704
        while (x--) {
slouken@0
   705
            register int L;
slouken@0
   706
slouken@1895
   707
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   708
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   709
                + colortab[*cb + 2 * 256];
slouken@1895
   710
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   711
            cr += 4;
slouken@1895
   712
            cb += 4;
slouken@0
   713
slouken@1895
   714
            L = *lum;
slouken@1895
   715
            lum += 2;
slouken@1895
   716
            *row++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   717
                      rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   718
slouken@1895
   719
            L = *lum;
slouken@1895
   720
            lum += 2;
slouken@1895
   721
            *row++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   722
                      rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   723
slouken@0
   724
slouken@0
   725
        }
slouken@0
   726
        row += mod;
slouken@0
   727
    }
slouken@0
   728
}
slouken@0
   729
slouken@0
   730
/*
slouken@0
   731
 * In this function I make use of a nasty trick. The tables have the lower
slouken@0
   732
 * 16 bits replicated in the upper 16. This means I can write ints and get
slouken@0
   733
 * the horisontal doubling for free (almost).
slouken@0
   734
 */
slouken@1895
   735
static void
slouken@1895
   736
Color16DitherYUY2Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   737
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   738
                       unsigned char *cb, unsigned char *out,
slouken@1895
   739
                       int rows, int cols, int mod)
slouken@0
   740
{
slouken@1895
   741
    unsigned int *row = (unsigned int *) out;
slouken@1895
   742
    const int next_row = cols + (mod / 2);
slouken@0
   743
    int x, y;
slouken@0
   744
    int cr_r;
slouken@0
   745
    int crb_g;
slouken@0
   746
    int cb_b;
slouken@0
   747
    int cols_2 = cols / 2;
slouken@0
   748
slouken@0
   749
    y = rows;
slouken@1895
   750
    while (y--) {
slouken@0
   751
        x = cols_2;
slouken@1895
   752
        while (x--) {
slouken@0
   753
            register int L;
slouken@0
   754
slouken@1895
   755
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   756
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   757
                + colortab[*cb + 2 * 256];
slouken@1895
   758
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   759
            cr += 4;
slouken@1895
   760
            cb += 4;
slouken@0
   761
slouken@1895
   762
            L = *lum;
slouken@1895
   763
            lum += 2;
slouken@1895
   764
            row[0] = row[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   765
                                      rgb_2_pix[L + crb_g] |
slouken@1895
   766
                                      rgb_2_pix[L + cb_b]);
slouken@0
   767
            row++;
slouken@0
   768
slouken@1895
   769
            L = *lum;
slouken@1895
   770
            lum += 2;
slouken@1895
   771
            row[0] = row[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   772
                                      rgb_2_pix[L + crb_g] |
slouken@1895
   773
                                      rgb_2_pix[L + cb_b]);
slouken@0
   774
            row++;
slouken@0
   775
slouken@0
   776
        }
slouken@0
   777
        row += next_row;
slouken@0
   778
    }
slouken@0
   779
}
slouken@0
   780
slouken@1895
   781
static void
slouken@1895
   782
Color24DitherYUY2Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   783
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   784
                       unsigned char *cb, unsigned char *out,
slouken@1895
   785
                       int rows, int cols, int mod)
slouken@0
   786
{
slouken@0
   787
    unsigned int value;
slouken@1895
   788
    unsigned char *row = out;
slouken@1895
   789
    const int next_row = (cols * 2 + mod) * 3;
slouken@0
   790
    int x, y;
slouken@0
   791
    int cr_r;
slouken@0
   792
    int crb_g;
slouken@0
   793
    int cb_b;
slouken@0
   794
    int cols_2 = cols / 2;
slouken@0
   795
    y = rows;
slouken@1895
   796
    while (y--) {
slouken@0
   797
        x = cols_2;
slouken@1895
   798
        while (x--) {
slouken@0
   799
            register int L;
slouken@0
   800
slouken@1895
   801
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   802
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   803
                + colortab[*cb + 2 * 256];
slouken@1895
   804
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   805
            cr += 4;
slouken@1895
   806
            cb += 4;
slouken@0
   807
slouken@1895
   808
            L = *lum;
slouken@1895
   809
            lum += 2;
slouken@1895
   810
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   811
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   812
            row[0 + 0] = row[3 + 0] = row[next_row + 0] =
slouken@1895
   813
                row[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   814
            row[0 + 1] = row[3 + 1] = row[next_row + 1] =
slouken@1895
   815
                row[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   816
            row[0 + 2] = row[3 + 2] = row[next_row + 2] =
slouken@1895
   817
                row[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   818
            row += 2 * 3;
slouken@0
   819
slouken@1895
   820
            L = *lum;
slouken@1895
   821
            lum += 2;
slouken@1895
   822
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   823
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   824
            row[0 + 0] = row[3 + 0] = row[next_row + 0] =
slouken@1895
   825
                row[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   826
            row[0 + 1] = row[3 + 1] = row[next_row + 1] =
slouken@1895
   827
                row[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   828
            row[0 + 2] = row[3 + 2] = row[next_row + 2] =
slouken@1895
   829
                row[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   830
            row += 2 * 3;
slouken@0
   831
slouken@0
   832
        }
slouken@0
   833
        row += next_row;
slouken@0
   834
    }
slouken@0
   835
}
slouken@0
   836
slouken@1895
   837
static void
slouken@1895
   838
Color32DitherYUY2Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   839
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   840
                       unsigned char *cb, unsigned char *out,
slouken@1895
   841
                       int rows, int cols, int mod)
slouken@0
   842
{
slouken@1895
   843
    unsigned int *row = (unsigned int *) out;
slouken@1895
   844
    const int next_row = cols * 2 + mod;
slouken@0
   845
    int x, y;
slouken@0
   846
    int cr_r;
slouken@0
   847
    int crb_g;
slouken@0
   848
    int cb_b;
slouken@0
   849
    int cols_2 = cols / 2;
slouken@1895
   850
    mod += mod;
slouken@0
   851
    y = rows;
slouken@1895
   852
    while (y--) {
slouken@0
   853
        x = cols_2;
slouken@1895
   854
        while (x--) {
slouken@0
   855
            register int L;
slouken@0
   856
slouken@1895
   857
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   858
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   859
                + colortab[*cb + 2 * 256];
slouken@1895
   860
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   861
            cr += 4;
slouken@1895
   862
            cb += 4;
slouken@0
   863
slouken@1895
   864
            L = *lum;
slouken@1895
   865
            lum += 2;
slouken@1895
   866
            row[0] = row[1] = row[next_row] = row[next_row + 1] =
slouken@1895
   867
                (rgb_2_pix[L + cr_r] |
slouken@1895
   868
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   869
            row += 2;
slouken@0
   870
slouken@1895
   871
            L = *lum;
slouken@1895
   872
            lum += 2;
slouken@1895
   873
            row[0] = row[1] = row[next_row] = row[next_row + 1] =
slouken@1895
   874
                (rgb_2_pix[L + cr_r] |
slouken@1895
   875
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   876
            row += 2;
slouken@0
   877
slouken@0
   878
slouken@0
   879
        }
slouken@0
   880
slouken@0
   881
        row += next_row;
slouken@0
   882
    }
slouken@0
   883
}
slouken@0
   884
slouken@0
   885
/*
slouken@0
   886
 * How many 1 bits are there in the Uint32.
slouken@0
   887
 * Low performance, do not call often.
slouken@0
   888
 */
slouken@1895
   889
static int
slouken@1895
   890
number_of_bits_set(Uint32 a)
slouken@0
   891
{
slouken@1895
   892
    if (!a)
slouken@1895
   893
        return 0;
slouken@1895
   894
    if (a & 1)
slouken@1895
   895
        return 1 + number_of_bits_set(a >> 1);
slouken@1895
   896
    return (number_of_bits_set(a >> 1));
slouken@0
   897
}
slouken@0
   898
slouken@0
   899
/*
slouken@0
   900
 * How many 0 bits are there at least significant end of Uint32.
slouken@0
   901
 * Low performance, do not call often.
slouken@0
   902
 */
slouken@1895
   903
static int
slouken@1895
   904
free_bits_at_bottom(Uint32 a)
slouken@0
   905
{
slouken@1895
   906
    /* assume char is 8 bits */
slouken@1895
   907
    if (!a)
slouken@1895
   908
        return sizeof(Uint32) * 8;
slouken@1895
   909
    if (((Sint32) a) & 1l)
slouken@1895
   910
        return 0;
slouken@1895
   911
    return 1 + free_bits_at_bottom(a >> 1);
slouken@0
   912
}
slouken@0
   913
slouken@1895
   914
static int
slouken@1895
   915
SDL_SW_SetupYUVDisplay(SDL_SW_YUVTexture * swdata, Uint32 target_format)
slouken@1895
   916
{
slouken@1895
   917
    Uint32 *r_2_pix_alloc;
slouken@1895
   918
    Uint32 *g_2_pix_alloc;
slouken@1895
   919
    Uint32 *b_2_pix_alloc;
slouken@1895
   920
    int i;
slouken@1895
   921
    int bpp;
slouken@1895
   922
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@0
   923
slouken@1895
   924
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   925
        (target_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask) || bpp < 15) {
slouken@1895
   926
        SDL_SetError("Unsupported YUV destination format");
slouken@1895
   927
        return -1;
slouken@1895
   928
    }
slouken@0
   929
slouken@1895
   930
    swdata->target_format = target_format;
slouken@1895
   931
    r_2_pix_alloc = &swdata->rgb_2_pix[0 * 768];
slouken@1895
   932
    g_2_pix_alloc = &swdata->rgb_2_pix[1 * 768];
slouken@1895
   933
    b_2_pix_alloc = &swdata->rgb_2_pix[2 * 768];
slouken@0
   934
slouken@1895
   935
    /* 
slouken@1895
   936
     * Set up entries 0-255 in rgb-to-pixel value tables.
slouken@1895
   937
     */
slouken@1895
   938
    for (i = 0; i < 256; ++i) {
slouken@1895
   939
        r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Rmask));
slouken@1895
   940
        r_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Rmask);
slouken@2795
   941
        r_2_pix_alloc[i + 256] |= Amask;
slouken@1895
   942
        g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Gmask));
slouken@1895
   943
        g_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Gmask);
slouken@2795
   944
        g_2_pix_alloc[i + 256] |= Amask;
slouken@1895
   945
        b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Bmask));
slouken@1895
   946
        b_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Bmask);
slouken@2795
   947
        b_2_pix_alloc[i + 256] |= Amask;
slouken@1895
   948
    }
slouken@0
   949
slouken@1895
   950
    /*
slouken@1895
   951
     * If we have 16-bit output depth, then we double the value
slouken@1895
   952
     * in the top word. This means that we can write out both
slouken@1895
   953
     * pixels in the pixel doubling mode with one op. It is 
slouken@1895
   954
     * harmless in the normal case as storing a 32-bit value
slouken@1895
   955
     * through a short pointer will lose the top bits anyway.
slouken@1895
   956
     */
slouken@1895
   957
    if (SDL_BYTESPERPIXEL(target_format) == 2) {
slouken@1895
   958
        for (i = 0; i < 256; ++i) {
slouken@1895
   959
            r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 16;
slouken@1895
   960
            g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 16;
slouken@1895
   961
            b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 16;
slouken@1895
   962
        }
slouken@1895
   963
    }
slouken@0
   964
slouken@1895
   965
    /*
slouken@1895
   966
     * Spread out the values we have to the rest of the array so that
slouken@1895
   967
     * we do not need to check for overflow.
slouken@1895
   968
     */
slouken@1895
   969
    for (i = 0; i < 256; ++i) {
slouken@1895
   970
        r_2_pix_alloc[i] = r_2_pix_alloc[256];
slouken@1895
   971
        r_2_pix_alloc[i + 512] = r_2_pix_alloc[511];
slouken@1895
   972
        g_2_pix_alloc[i] = g_2_pix_alloc[256];
slouken@1895
   973
        g_2_pix_alloc[i + 512] = g_2_pix_alloc[511];
slouken@1895
   974
        b_2_pix_alloc[i] = b_2_pix_alloc[256];
slouken@1895
   975
        b_2_pix_alloc[i + 512] = b_2_pix_alloc[511];
slouken@1895
   976
    }
slouken@0
   977
slouken@1895
   978
    /* You have chosen wisely... */
slouken@2781
   979
    switch (swdata->format) {
slouken@1965
   980
    case SDL_PIXELFORMAT_YV12:
slouken@1965
   981
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
   982
        if (SDL_BYTESPERPIXEL(target_format) == 2) {
icculus@2197
   983
#if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
slouken@1895
   984
            /* inline assembly functions */
slouken@1895
   985
            if (SDL_HasMMX() && (Rmask == 0xF800) &&
slouken@2172
   986
                (Gmask == 0x07E0) && (Bmask == 0x001F)
slouken@2781
   987
                && (swdata->w & 15) == 0) {
slouken@1895
   988
/*printf("Using MMX 16-bit 565 dither\n");*/
slouken@1895
   989
                swdata->Display1X = Color565DitherYV12MMX1X;
slouken@1895
   990
            } else {
slouken@1895
   991
/*printf("Using C 16-bit dither\n");*/
slouken@1895
   992
                swdata->Display1X = Color16DitherYV12Mod1X;
slouken@1895
   993
            }
slouken@1895
   994
#else
slouken@1895
   995
            swdata->Display1X = Color16DitherYV12Mod1X;
slouken@1895
   996
#endif
slouken@1895
   997
            swdata->Display2X = Color16DitherYV12Mod2X;
slouken@1895
   998
        }
slouken@1895
   999
        if (SDL_BYTESPERPIXEL(target_format) == 3) {
slouken@1895
  1000
            swdata->Display1X = Color24DitherYV12Mod1X;
slouken@1895
  1001
            swdata->Display2X = Color24DitherYV12Mod2X;
slouken@1895
  1002
        }
slouken@1895
  1003
        if (SDL_BYTESPERPIXEL(target_format) == 4) {
icculus@2197
  1004
#if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
slouken@1895
  1005
            /* inline assembly functions */
slouken@1895
  1006
            if (SDL_HasMMX() && (Rmask == 0x00FF0000) &&
slouken@1895
  1007
                (Gmask == 0x0000FF00) &&
slouken@2781
  1008
                (Bmask == 0x000000FF) && (swdata->w & 15) == 0) {
slouken@1895
  1009
/*printf("Using MMX 32-bit dither\n");*/
slouken@1895
  1010
                swdata->Display1X = ColorRGBDitherYV12MMX1X;
slouken@1895
  1011
            } else {
slouken@1895
  1012
/*printf("Using C 32-bit dither\n");*/
slouken@1895
  1013
                swdata->Display1X = Color32DitherYV12Mod1X;
slouken@1895
  1014
            }
slouken@1895
  1015
#else
slouken@1895
  1016
            swdata->Display1X = Color32DitherYV12Mod1X;
slouken@1895
  1017
#endif
slouken@1895
  1018
            swdata->Display2X = Color32DitherYV12Mod2X;
slouken@1895
  1019
        }
slouken@1895
  1020
        break;
slouken@1965
  1021
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1022
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1023
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1024
        if (SDL_BYTESPERPIXEL(target_format) == 2) {
slouken@1895
  1025
            swdata->Display1X = Color16DitherYUY2Mod1X;
slouken@1895
  1026
            swdata->Display2X = Color16DitherYUY2Mod2X;
slouken@1895
  1027
        }
slouken@1895
  1028
        if (SDL_BYTESPERPIXEL(target_format) == 3) {
slouken@1895
  1029
            swdata->Display1X = Color24DitherYUY2Mod1X;
slouken@1895
  1030
            swdata->Display2X = Color24DitherYUY2Mod2X;
slouken@1895
  1031
        }
slouken@1895
  1032
        if (SDL_BYTESPERPIXEL(target_format) == 4) {
slouken@1895
  1033
            swdata->Display1X = Color32DitherYUY2Mod1X;
slouken@1895
  1034
            swdata->Display2X = Color32DitherYUY2Mod2X;
slouken@1895
  1035
        }
slouken@1895
  1036
        break;
slouken@1895
  1037
    default:
slouken@1895
  1038
        /* We should never get here (caught above) */
slouken@1895
  1039
        break;
slouken@1895
  1040
    }
slouken@0
  1041
slouken@1895
  1042
    if (swdata->display) {
slouken@1895
  1043
        SDL_FreeSurface(swdata->display);
slouken@1895
  1044
        swdata->display = NULL;
slouken@1895
  1045
    }
slouken@1895
  1046
    return 0;
slouken@0
  1047
}
slouken@0
  1048
slouken@1895
  1049
SDL_SW_YUVTexture *
slouken@2781
  1050
SDL_SW_CreateYUVTexture(Uint32 format, int w, int h)
slouken@0
  1051
{
slouken@1895
  1052
    SDL_SW_YUVTexture *swdata;
slouken@1895
  1053
    int *Cr_r_tab;
slouken@1895
  1054
    int *Cr_g_tab;
slouken@1895
  1055
    int *Cb_g_tab;
slouken@1895
  1056
    int *Cb_b_tab;
slouken@1895
  1057
    int i;
slouken@1895
  1058
    int CR, CB;
slouken@1895
  1059
slouken@1920
  1060
    swdata = (SDL_SW_YUVTexture *) SDL_calloc(1, sizeof(*swdata));
slouken@1895
  1061
    if (!swdata) {
slouken@1895
  1062
        SDL_OutOfMemory();
slouken@1895
  1063
        return NULL;
slouken@1895
  1064
    }
slouken@1895
  1065
slouken@2781
  1066
    switch (format) {
slouken@1965
  1067
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1068
    case SDL_PIXELFORMAT_IYUV:
slouken@1965
  1069
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1070
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1071
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1072
        break;
slouken@1895
  1073
    default:
slouken@1895
  1074
        SDL_SetError("Unsupported YUV format");
slouken@1895
  1075
        return NULL;
slouken@1895
  1076
    }
slouken@1895
  1077
slouken@2781
  1078
    swdata->format = format;
slouken@1965
  1079
    swdata->target_format = SDL_PIXELFORMAT_UNKNOWN;
slouken@2786
  1080
    swdata->w = w;
slouken@2786
  1081
    swdata->h = h;
slouken@2781
  1082
    swdata->pixels = (Uint8 *) SDL_malloc(w * h * 2);
slouken@1895
  1083
    swdata->colortab = (int *) SDL_malloc(4 * 256 * sizeof(int));
slouken@1895
  1084
    swdata->rgb_2_pix = (Uint32 *) SDL_malloc(3 * 768 * sizeof(Uint32));
slouken@1895
  1085
    if (!swdata->pixels || !swdata->colortab || !swdata->rgb_2_pix) {
slouken@1895
  1086
        SDL_OutOfMemory();
slouken@1895
  1087
        SDL_SW_DestroyYUVTexture(swdata);
slouken@1895
  1088
        return NULL;
slouken@1895
  1089
    }
slouken@1895
  1090
slouken@1895
  1091
    /* Generate the tables for the display surface */
slouken@1895
  1092
    Cr_r_tab = &swdata->colortab[0 * 256];
slouken@1895
  1093
    Cr_g_tab = &swdata->colortab[1 * 256];
slouken@1895
  1094
    Cb_g_tab = &swdata->colortab[2 * 256];
slouken@1895
  1095
    Cb_b_tab = &swdata->colortab[3 * 256];
slouken@1895
  1096
    for (i = 0; i < 256; i++) {
slouken@1895
  1097
        /* Gamma correction (luminescence table) and chroma correction
slouken@1895
  1098
           would be done here.  See the Berkeley mpeg_play sources.
slouken@1895
  1099
         */
slouken@1895
  1100
        CB = CR = (i - 128);
slouken@1895
  1101
        Cr_r_tab[i] = (int) ((0.419 / 0.299) * CR);
slouken@1895
  1102
        Cr_g_tab[i] = (int) (-(0.299 / 0.419) * CR);
slouken@1895
  1103
        Cb_g_tab[i] = (int) (-(0.114 / 0.331) * CB);
slouken@1895
  1104
        Cb_b_tab[i] = (int) ((0.587 / 0.331) * CB);
slouken@1895
  1105
    }
slouken@1895
  1106
slouken@1895
  1107
    /* Find the pitch and offset values for the overlay */
slouken@2781
  1108
    switch (format) {
slouken@1965
  1109
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1110
    case SDL_PIXELFORMAT_IYUV:
slouken@2781
  1111
        swdata->pitches[0] = w;
slouken@1895
  1112
        swdata->pitches[1] = swdata->pitches[0] / 2;
slouken@1895
  1113
        swdata->pitches[2] = swdata->pitches[0] / 2;
slouken@1895
  1114
        swdata->planes[0] = swdata->pixels;
slouken@2786
  1115
        swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
slouken@2786
  1116
        swdata->planes[2] = swdata->planes[1] + swdata->pitches[1] * h / 2;
slouken@1895
  1117
        break;
slouken@1965
  1118
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1119
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1120
    case SDL_PIXELFORMAT_YVYU:
slouken@2781
  1121
        swdata->pitches[0] = w * 2;
slouken@1895
  1122
        swdata->planes[0] = swdata->pixels;
slouken@1895
  1123
        break;
slouken@1895
  1124
    default:
slouken@1895
  1125
        /* We should never get here (caught above) */
slouken@1895
  1126
        break;
slouken@1895
  1127
    }
slouken@1895
  1128
slouken@1895
  1129
    /* We're all done.. */
slouken@1895
  1130
    return (swdata);
slouken@0
  1131
}
slouken@0
  1132
slouken@1895
  1133
int
slouken@1895
  1134
SDL_SW_QueryYUVTexturePixels(SDL_SW_YUVTexture * swdata, void **pixels,
slouken@1895
  1135
                             int *pitch)
slouken@0
  1136
{
slouken@1895
  1137
    *pixels = swdata->planes[0];
slouken@1895
  1138
    *pitch = swdata->pitches[0];
slouken@1895
  1139
    return 0;
slouken@0
  1140
}
slouken@0
  1141
slouken@1895
  1142
int
slouken@1895
  1143
SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
slouken@1895
  1144
                        const void *pixels, int pitch)
slouken@0
  1145
{
slouken@2781
  1146
    switch (swdata->format) {
slouken@1965
  1147
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1148
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
  1149
        if (rect
slouken@2781
  1150
            && (rect->x != 0 || rect->y != 0 || rect->w != swdata->w
slouken@2781
  1151
                || rect->h != swdata->h)) {
slouken@1895
  1152
            SDL_SetError
slouken@1895
  1153
                ("YV12 and IYUV textures only support full surface updates");
slouken@1895
  1154
            return -1;
slouken@1895
  1155
        }
slouken@2781
  1156
        SDL_memcpy(swdata->pixels, pixels, swdata->h * swdata->w * 2);
slouken@1895
  1157
        break;
slouken@1965
  1158
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1159
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1160
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1161
        {
slouken@1895
  1162
            Uint8 *src, *dst;
slouken@1895
  1163
            int row;
slouken@1895
  1164
            size_t length;
slouken@0
  1165
slouken@1895
  1166
            src = (Uint8 *) pixels;
slouken@1895
  1167
            dst =
slouken@1895
  1168
                swdata->planes[0] + rect->y * swdata->pitches[0] +
slouken@1895
  1169
                rect->x * 2;
slouken@1895
  1170
            length = rect->w * 2;
slouken@1895
  1171
            for (row = 0; row < rect->h; ++row) {
slouken@1895
  1172
                SDL_memcpy(dst, src, length);
slouken@1895
  1173
                src += pitch;
slouken@1895
  1174
                dst += swdata->pitches[0];
slouken@1895
  1175
            }
slouken@1895
  1176
        }
slouken@1895
  1177
        break;
slouken@1895
  1178
    }
slouken@1895
  1179
    return 0;
slouken@0
  1180
}
slouken@0
  1181
slouken@1895
  1182
int
slouken@1895
  1183
SDL_SW_LockYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
slouken@1895
  1184
                      int markDirty, void **pixels, int *pitch)
slouken@0
  1185
{
slouken@2781
  1186
    switch (swdata->format) {
slouken@1965
  1187
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1188
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
  1189
        if (rect
slouken@2781
  1190
            && (rect->x != 0 || rect->y != 0 || rect->w != swdata->w
slouken@2781
  1191
                || rect->h != swdata->h)) {
slouken@1895
  1192
            SDL_SetError
slouken@1895
  1193
                ("YV12 and IYUV textures only support full surface locks");
slouken@1895
  1194
            return -1;
slouken@1895
  1195
        }
slouken@1895
  1196
        break;
slouken@1895
  1197
    }
slouken@1895
  1198
slouken@1895
  1199
    *pixels = swdata->planes[0] + rect->y * swdata->pitches[0] + rect->x * 2;
slouken@1895
  1200
    *pitch = swdata->pitches[0];
slouken@1895
  1201
    return 0;
slouken@0
  1202
}
slouken@1895
  1203
slouken@1895
  1204
void
slouken@1895
  1205
SDL_SW_UnlockYUVTexture(SDL_SW_YUVTexture * swdata)
slouken@1895
  1206
{
slouken@1895
  1207
}
slouken@1895
  1208
slouken@1895
  1209
int
slouken@1895
  1210
SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture * swdata, const SDL_Rect * srcrect,
slouken@1895
  1211
                    Uint32 target_format, int w, int h, void *pixels,
slouken@1895
  1212
                    int pitch)
slouken@1895
  1213
{
slouken@1895
  1214
    int stretch;
slouken@1895
  1215
    int scale_2x;
slouken@1895
  1216
    Uint8 *lum, *Cr, *Cb;
slouken@1895
  1217
    int mod;
slouken@1895
  1218
slouken@1895
  1219
    /* Make sure we're set up to display in the desired format */
slouken@1895
  1220
    if (target_format != swdata->target_format) {
slouken@1895
  1221
        if (SDL_SW_SetupYUVDisplay(swdata, target_format) < 0) {
slouken@1895
  1222
            return -1;
slouken@1895
  1223
        }
slouken@1895
  1224
    }
slouken@1895
  1225
slouken@1895
  1226
    stretch = 0;
slouken@1895
  1227
    scale_2x = 0;
slouken@2781
  1228
    if (srcrect->x || srcrect->y || srcrect->w < swdata->w
slouken@2781
  1229
        || srcrect->h < swdata->h) {
slouken@1895
  1230
        /* The source rectangle has been clipped.
slouken@1895
  1231
           Using a scratch surface is easier than adding clipped
slouken@1895
  1232
           source support to all the blitters, plus that would
slouken@1895
  1233
           slow them down in the general unclipped case.
slouken@1895
  1234
         */
slouken@1895
  1235
        stretch = 1;
slouken@1895
  1236
    } else if ((srcrect->w != w) || (srcrect->h != h)) {
slouken@1895
  1237
        if ((w == 2 * srcrect->w) && (h == 2 * srcrect->h)) {
slouken@1895
  1238
            scale_2x = 1;
slouken@1895
  1239
        } else {
slouken@1895
  1240
            stretch = 1;
slouken@1895
  1241
        }
slouken@1895
  1242
    }
slouken@1895
  1243
    if (stretch) {
slouken@1895
  1244
        int bpp;
slouken@1895
  1245
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
  1246
slouken@1895
  1247
        if (swdata->display) {
slouken@1895
  1248
            swdata->display->w = w;
slouken@1895
  1249
            swdata->display->h = h;
slouken@1895
  1250
            swdata->display->pixels = pixels;
slouken@1895
  1251
            swdata->display->pitch = pitch;
slouken@1895
  1252
        } else {
slouken@1895
  1253
            /* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
slouken@1895
  1254
            SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
slouken@1895
  1255
                                       &Bmask, &Amask);
slouken@1895
  1256
            swdata->display =
slouken@1895
  1257
                SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch, Rmask,
slouken@1895
  1258
                                         Gmask, Bmask, Amask);
slouken@1895
  1259
            if (!swdata->display) {
slouken@1895
  1260
                return (-1);
slouken@1895
  1261
            }
slouken@1895
  1262
        }
slouken@1895
  1263
        if (!swdata->stretch) {
slouken@1895
  1264
            /* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
slouken@1895
  1265
            SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
slouken@1895
  1266
                                       &Bmask, &Amask);
slouken@1895
  1267
            swdata->stretch =
slouken@2781
  1268
                SDL_CreateRGBSurface(0, swdata->w, swdata->h, bpp, Rmask,
slouken@1895
  1269
                                     Gmask, Bmask, Amask);
slouken@1895
  1270
            if (!swdata->stretch) {
slouken@1895
  1271
                return (-1);
slouken@1895
  1272
            }
slouken@1895
  1273
        }
slouken@1895
  1274
        pixels = swdata->stretch->pixels;
slouken@1895
  1275
        pitch = swdata->stretch->pitch;
slouken@1895
  1276
    }
slouken@2781
  1277
    switch (swdata->format) {
slouken@1965
  1278
    case SDL_PIXELFORMAT_YV12:
slouken@1895
  1279
        lum = swdata->planes[0];
slouken@1895
  1280
        Cr = swdata->planes[1];
slouken@1895
  1281
        Cb = swdata->planes[2];
slouken@1895
  1282
        break;
slouken@1965
  1283
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
  1284
        lum = swdata->planes[0];
slouken@1895
  1285
        Cr = swdata->planes[2];
slouken@1895
  1286
        Cb = swdata->planes[1];
slouken@1895
  1287
        break;
slouken@1965
  1288
    case SDL_PIXELFORMAT_YUY2:
slouken@1895
  1289
        lum = swdata->planes[0];
slouken@1895
  1290
        Cr = lum + 3;
slouken@1895
  1291
        Cb = lum + 1;
slouken@1895
  1292
        break;
slouken@1965
  1293
    case SDL_PIXELFORMAT_UYVY:
slouken@1895
  1294
        lum = swdata->planes[0] + 1;
slouken@1895
  1295
        Cr = lum + 1;
slouken@1895
  1296
        Cb = lum - 1;
slouken@1895
  1297
        break;
slouken@1965
  1298
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1299
        lum = swdata->planes[0];
slouken@1895
  1300
        Cr = lum + 1;
slouken@1895
  1301
        Cb = lum + 3;
slouken@1895
  1302
        break;
slouken@1895
  1303
    default:
slouken@1895
  1304
        SDL_SetError("Unsupported YUV format in copy");
slouken@1895
  1305
        return (-1);
slouken@1895
  1306
    }
slouken@1895
  1307
    mod = (pitch / SDL_BYTESPERPIXEL(target_format));
slouken@1895
  1308
slouken@1895
  1309
    if (scale_2x) {
slouken@2781
  1310
        mod -= (swdata->w * 2);
slouken@1895
  1311
        swdata->Display2X(swdata->colortab, swdata->rgb_2_pix,
slouken@2781
  1312
                          lum, Cr, Cb, pixels, swdata->h, swdata->w, mod);
slouken@1895
  1313
    } else {
slouken@2781
  1314
        mod -= swdata->w;
slouken@1895
  1315
        swdata->Display1X(swdata->colortab, swdata->rgb_2_pix,
slouken@2781
  1316
                          lum, Cr, Cb, pixels, swdata->h, swdata->w, mod);
slouken@1895
  1317
    }
slouken@1895
  1318
    if (stretch) {
slouken@1895
  1319
        SDL_Rect rect = *srcrect;
slouken@1895
  1320
        SDL_SoftStretch(swdata->stretch, &rect, swdata->display, NULL);
slouken@1895
  1321
    }
slouken@1895
  1322
    return 0;
slouken@1895
  1323
}
slouken@1895
  1324
slouken@1895
  1325
void
slouken@1895
  1326
SDL_SW_DestroyYUVTexture(SDL_SW_YUVTexture * swdata)
slouken@1895
  1327
{
slouken@1895
  1328
    if (swdata) {
slouken@1895
  1329
        if (swdata->pixels) {
slouken@1895
  1330
            SDL_free(swdata->pixels);
slouken@1895
  1331
        }
slouken@1895
  1332
        if (swdata->colortab) {
slouken@1895
  1333
            SDL_free(swdata->colortab);
slouken@1895
  1334
        }
slouken@1895
  1335
        if (swdata->rgb_2_pix) {
slouken@1895
  1336
            SDL_free(swdata->rgb_2_pix);
slouken@1895
  1337
        }
slouken@1895
  1338
        if (swdata->stretch) {
slouken@1895
  1339
            SDL_FreeSurface(swdata->stretch);
slouken@1895
  1340
        }
slouken@1895
  1341
        if (swdata->display) {
slouken@1895
  1342
            SDL_FreeSurface(swdata->display);
slouken@1895
  1343
        }
slouken@1895
  1344
        SDL_free(swdata);
slouken@1895
  1345
    }
slouken@1895
  1346
}
slouken@1895
  1347
slouken@1895
  1348
/* vi: set ts=4 sw=4 expandtab: */