"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/colormodels.c" (11 Jul 2008, 13005 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * This library is free software; you can redistribute it and/or modify it
    3  * under the terms of the GNU Lesser General Public License as published
    4  * by the Free Software Foundation; either version 2 of the License, or
    5  * (at your option) any later version.
    6  * 
    7  * This library is distributed in the hope that it will be useful, but
    8  * WITHOUT ANY WARRANTY; without even the implied warranty of
    9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   10  * Lesser General Public License for more details.
   11  * 
   12  * You should have received a copy of the GNU Lesser General Public
   13  * License along with this library; if not, write to the Free Software
   14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 
   15  * USA
   16  */
   17 
   18 
   19 
   20 #include "colormodels.h"
   21 #include <stdlib.h>
   22 #include <string.h>
   23 
   24 
   25 cmodel_yuv_t *yuv_table = 0;
   26 
   27 // Compression coefficients straight out of jpeglib
   28 #define R_TO_Y    0.29900
   29 #define G_TO_Y    0.58700
   30 #define B_TO_Y    0.11400
   31 
   32 #define R_TO_U    -0.16874
   33 #define G_TO_U    -0.33126
   34 #define B_TO_U    0.50000
   35 
   36 #define R_TO_V    0.50000
   37 #define G_TO_V    -0.41869
   38 #define B_TO_V    -0.08131
   39 
   40 // Decompression coefficients straight out of jpeglib
   41 #define V_TO_R    1.40200
   42 #define V_TO_G    -0.71414
   43 
   44 #define U_TO_G    -0.34414
   45 #define U_TO_B    1.77200
   46 
   47 
   48 
   49 
   50 
   51 
   52 void cmodel_init_yuv(cmodel_yuv_t *yuv_table)
   53 {
   54     int i;
   55 
   56 /* compression */
   57     for(i = 0; i < 0x100; i++)
   58     {
   59         yuv_table->rtoy_tab[i] = (int)(R_TO_Y * 0x10000 * i);
   60         yuv_table->rtou_tab[i] = (int)(R_TO_U * 0x10000 * i);
   61         yuv_table->rtov_tab[i] = (int)(R_TO_V * 0x10000 * i);
   62 
   63         yuv_table->gtoy_tab[i] = (int)(G_TO_Y * 0x10000 * i);
   64         yuv_table->gtou_tab[i] = (int)(G_TO_U * 0x10000 * i);
   65         yuv_table->gtov_tab[i] = (int)(G_TO_V * 0x10000 * i);
   66 
   67         yuv_table->btoy_tab[i] = (int)(B_TO_Y * 0x10000 * i);
   68         yuv_table->btou_tab[i] = (int)(B_TO_U * 0x10000 * i) + 0x800000;
   69         yuv_table->btov_tab[i] = (int)(B_TO_V * 0x10000 * i) + 0x800000;
   70     }
   71 
   72 /* compression */
   73     for(i = 0; i < 0x10000; i++)
   74     {
   75         yuv_table->rtoy_tab16[i] = (int)(R_TO_Y * 0x100 * i);
   76         yuv_table->rtou_tab16[i] = (int)(R_TO_U * 0x100 * i);
   77         yuv_table->rtov_tab16[i] = (int)(R_TO_V * 0x100 * i);
   78 
   79         yuv_table->gtoy_tab16[i] = (int)(G_TO_Y * 0x100 * i);
   80         yuv_table->gtou_tab16[i] = (int)(G_TO_U * 0x100 * i);
   81         yuv_table->gtov_tab16[i] = (int)(G_TO_V * 0x100 * i);
   82 
   83         yuv_table->btoy_tab16[i] = (int)(B_TO_Y * 0x100 * i);
   84         yuv_table->btou_tab16[i] = (int)(B_TO_U * 0x100 * i) + 0x800000;
   85         yuv_table->btov_tab16[i] = (int)(B_TO_V * 0x100 * i) + 0x800000;
   86     }
   87 
   88 
   89 
   90 
   91 /* decompression */
   92     yuv_table->vtor = &(yuv_table->vtor_tab[0x80]);
   93     yuv_table->vtog = &(yuv_table->vtog_tab[0x80]);
   94     yuv_table->utog = &(yuv_table->utog_tab[0x80]);
   95     yuv_table->utob = &(yuv_table->utob_tab[0x80]);
   96     for(i = -0x80; i < 0x80; i++)
   97     {
   98         yuv_table->vtor[i] = (int)(V_TO_R * 0x10000 * i);
   99         yuv_table->vtog[i] = (int)(V_TO_G * 0x10000 * i);
  100 
  101         yuv_table->utog[i] = (int)(U_TO_G * 0x10000 * i);
  102         yuv_table->utob[i] = (int)(U_TO_B * 0x10000 * i);
  103     }
  104 
  105 
  106 /* decompression */
  107     yuv_table->vtor_float = &(yuv_table->vtor_float_tab[0x80]);
  108     yuv_table->vtog_float = &(yuv_table->vtog_float_tab[0x80]);
  109     yuv_table->utog_float = &(yuv_table->utog_float_tab[0x80]);
  110     yuv_table->utob_float = &(yuv_table->utob_float_tab[0x80]);
  111     for(i = -0x80; i < 0x80; i++)
  112     {
  113         yuv_table->vtor_float[i] = V_TO_R * i / 0xff;
  114         yuv_table->vtog_float[i] = V_TO_G * i / 0xff;
  115 
  116         yuv_table->utog_float[i] = U_TO_G * i / 0xff;
  117         yuv_table->utob_float[i] = U_TO_B * i / 0xff;
  118     }
  119 
  120 
  121 /* decompression */
  122     yuv_table->vtor16 = &(yuv_table->vtor_tab16[0x8000]);
  123     yuv_table->vtog16 = &(yuv_table->vtog_tab16[0x8000]);
  124     yuv_table->utog16 = &(yuv_table->utog_tab16[0x8000]);
  125     yuv_table->utob16 = &(yuv_table->utob_tab16[0x8000]);
  126     for(i = -0x8000; i < 0x8000; i++)
  127     {
  128         yuv_table->vtor16[i] = (int)(V_TO_R * 0x100 * i);
  129         yuv_table->vtog16[i] = (int)(V_TO_G * 0x100 * i);
  130 
  131         yuv_table->utog16[i] = (int)(U_TO_G * 0x100 * i);
  132         yuv_table->utob16[i] = (int)(U_TO_B * 0x100 * i);
  133     }
  134 
  135 
  136 /* decompression */
  137     yuv_table->v16tor_float = &(yuv_table->v16tor_float_tab[0x8000]);
  138     yuv_table->v16tog_float = &(yuv_table->v16tog_float_tab[0x8000]);
  139     yuv_table->u16tog_float = &(yuv_table->u16tog_float_tab[0x8000]);
  140     yuv_table->u16tob_float = &(yuv_table->u16tob_float_tab[0x8000]);
  141     for(i = -0x8000; i < 0x8000; i++)
  142     {
  143         yuv_table->v16tor_float[i] = V_TO_R * i / 0xffff;
  144         yuv_table->v16tog_float[i] = V_TO_G * i / 0xffff;
  145 
  146         yuv_table->u16tog_float[i] = U_TO_G * i / 0xffff;
  147         yuv_table->u16tob_float[i] = U_TO_B * i / 0xffff;
  148     }
  149 }
  150 
  151 
  152 void cmodel_delete_yuv(cmodel_yuv_t *yuv_table)
  153 {
  154 }
  155 
  156 int cmodel_is_planar(int colormodel)
  157 {
  158     switch(colormodel)
  159     {
  160         case BC_YUV420P:      return 1; break;
  161         case BC_YUV422P:      return 1; break;
  162         case BC_YUV444P:      return 1; break;
  163         case BC_YUV411P:      return 1; break;
  164     }
  165     return 0;
  166 }
  167 
  168 int cmodel_components(int colormodel)
  169 {
  170     switch(colormodel)
  171     {
  172         case BC_A8:           return 1; break;
  173         case BC_A16:          return 1; break;
  174         case BC_A_FLOAT:      return 1; break;
  175         case BC_RGB888:       return 3; break;
  176         case BC_RGBA8888:     return 4; break;
  177         case BC_RGB161616:    return 3; break;
  178         case BC_RGBA16161616: return 4; break;
  179         case BC_YUV888:       return 3; break;
  180         case BC_YUVA8888:     return 4; break;
  181         case BC_YUV161616:    return 3; break;
  182         case BC_YUVA16161616: return 4; break;
  183         case BC_YUV101010:    return 3; break;
  184         case BC_RGB_FLOAT:    return 3; break;
  185         case BC_RGBA_FLOAT:   return 4; break;
  186     }
  187 }
  188 
  189 int cmodel_calculate_pixelsize(int colormodel)
  190 {
  191     switch(colormodel)
  192     {
  193         case BC_A8:           return 1; break;
  194         case BC_A16:          return 2; break;
  195         case BC_A_FLOAT:      return 4; break;
  196         case BC_TRANSPARENCY: return 1; break;
  197         case BC_COMPRESSED:   return 1; break;
  198         case BC_RGB8:         return 1; break;
  199         case BC_RGB565:       return 2; break;
  200         case BC_BGR565:       return 2; break;
  201         case BC_BGR888:       return 3; break;
  202         case BC_BGR8888:      return 4; break;
  203 // Working bitmaps are packed to simplify processing
  204         case BC_RGB888:       return 3; break;
  205         case BC_ARGB8888:     return 4; break;
  206         case BC_ABGR8888:     return 4; break;
  207         case BC_RGBA8888:     return 4; break;
  208         case BC_RGB161616:    return 6; break;
  209         case BC_RGBA16161616: return 8; break;
  210         case BC_YUV888:       return 3; break;
  211         case BC_YUVA8888:     return 4; break;
  212         case BC_YUV161616:    return 6; break;
  213         case BC_YUVA16161616: return 8; break;
  214         case BC_YUV101010:    return 4; break;
  215         case BC_VYU888:       return 3; break;
  216         case BC_UYVA8888:     return 4; break;
  217         case BC_RGB_FLOAT:    return 12; break;
  218         case BC_RGBA_FLOAT:   return 16; break;
  219 // Planar
  220         case BC_YUV420P:      return 1; break;
  221         case BC_YUV422P:      return 1; break;
  222         case BC_YUV444P:      return 1; break;
  223         case BC_YUV422:       return 2; break;
  224         case BC_YUV411P:      return 1; break;
  225         case BC_YUV9P:        return 1; break;
  226     }
  227     return 0;
  228 }
  229 
  230 int cmodel_calculate_max(int colormodel)
  231 {
  232     switch(colormodel)
  233     {
  234 // Working bitmaps are packed to simplify processing
  235         case BC_A8:           return 0xff; break;
  236         case BC_A16:          return 0xffff; break;
  237         case BC_A_FLOAT:      return 1; break;
  238         case BC_RGB888:       return 0xff; break;
  239         case BC_RGBA8888:     return 0xff; break;
  240         case BC_RGB161616:    return 0xffff; break;
  241         case BC_RGBA16161616: return 0xffff; break;
  242         case BC_YUV888:       return 0xff; break;
  243         case BC_YUVA8888:     return 0xff; break;
  244         case BC_YUV161616:    return 0xffff; break;
  245         case BC_YUVA16161616: return 0xffff; break;
  246         case BC_RGB_FLOAT:    return 1; break;
  247         case BC_RGBA_FLOAT:   return 1; break;
  248     }
  249     return 0;
  250 }
  251 
  252 int cmodel_calculate_datasize(int w, int h, int bytes_per_line, int color_model)
  253 {
  254     if(bytes_per_line < 0) bytes_per_line = w * 
  255         cmodel_calculate_pixelsize(color_model);
  256     switch(color_model)
  257     {
  258         case BC_YUV420P:
  259         case BC_YUV411P:
  260             return w * h + w * h / 2 + 4;
  261             break;
  262 
  263         case BC_YUV422P:
  264             return w * h * 2 + 4;
  265             break;
  266 
  267         case BC_YUV444P:
  268             return w * h * 3 + 4;
  269             break;
  270 
  271         default:
  272             return h * bytes_per_line + 4;
  273             break;
  274     }
  275     return 0;
  276 }
  277 
  278 
  279 static void get_scale_tables(int **column_table, 
  280     int **row_table, 
  281     int in_x1, 
  282     int in_y1, 
  283     int in_x2, 
  284     int in_y2,
  285     int out_x1, 
  286     int out_y1, 
  287     int out_x2, 
  288     int out_y2)
  289 {
  290     int y_out, i;
  291     float w_in = in_x2 - in_x1;
  292     float h_in = in_y2 - in_y1;
  293     int w_out = out_x2 - out_x1;
  294     int h_out = out_y2 - out_y1;
  295 
  296     float hscale = w_in / w_out;
  297     float vscale = h_in / h_out;
  298 
  299 /* + 1 so we don't overflow when calculating in advance */
  300     (*column_table) = malloc(sizeof(int) * (w_out + 1));
  301     (*row_table) = malloc(sizeof(int) * h_out);
  302     for(i = 0; i < w_out; i++)
  303     {
  304         (*column_table)[i] = (int)(hscale * i) + in_x1;
  305     }
  306 
  307     for(i = 0; i < h_out; i++)
  308     {
  309         (*row_table)[i] = (int)(vscale * i) + in_y1;
  310 //printf("get_scale_tables %d %d\n", (*row_table)[i], i);
  311     }
  312 }
  313 
  314 void cmodel_transfer(unsigned char **output_rows, 
  315     unsigned char **input_rows,
  316     unsigned char *out_y_plane,
  317     unsigned char *out_u_plane,
  318     unsigned char *out_v_plane,
  319     unsigned char *in_y_plane,
  320     unsigned char *in_u_plane,
  321     unsigned char *in_v_plane,
  322     int in_x, 
  323     int in_y, 
  324     int in_w, 
  325     int in_h,
  326     int out_x, 
  327     int out_y, 
  328     int out_w, 
  329     int out_h,
  330     int in_colormodel, 
  331     int out_colormodel,
  332     int bg_color,
  333     int in_rowspan,
  334     int out_rowspan)
  335 {
  336     int *column_table;
  337     int *row_table;
  338     int scale;
  339     int bg_r, bg_g, bg_b;
  340     int in_pixelsize = cmodel_calculate_pixelsize(in_colormodel);
  341     int out_pixelsize = cmodel_calculate_pixelsize(out_colormodel);
  342 
  343     bg_r = (bg_color & 0xff0000) >> 16;
  344     bg_g = (bg_color & 0xff00) >> 8;
  345     bg_b = (bg_color & 0xff);
  346 
  347 // Initialize tables
  348     if(yuv_table == 0)
  349     {
  350         yuv_table = calloc(1, sizeof(cmodel_yuv_t));
  351         cmodel_init_yuv(yuv_table);
  352     }
  353 
  354 // Get scaling
  355     scale = (out_w != in_w) || (in_x != 0);
  356     get_scale_tables(&column_table, &row_table, 
  357         in_x, in_y, in_x + in_w, in_y + in_h,
  358         out_x, out_y, out_x + out_w, out_y + out_h);
  359 
  360 /*
  361  * printf("cmodel_transfer 1 %d %d %d,%d %d,%d %d,%d %d,%d\n", 
  362  * in_colormodel, 
  363  * out_colormodel, 
  364  * out_x, 
  365  * out_y, 
  366  * out_w, 
  367  * out_h, 
  368  * in_x, 
  369  * in_y, 
  370  * in_w, 
  371  * in_h);
  372  */
  373 
  374 
  375 #define PERMUTATION_VALUES \
  376     output_rows,  \
  377     input_rows, \
  378     out_y_plane, \
  379     out_u_plane, \
  380     out_v_plane, \
  381     in_y_plane, \
  382     in_u_plane, \
  383     in_v_plane, \
  384     in_x,  \
  385     in_y,  \
  386     in_w,  \
  387     in_h, \
  388     out_x,  \
  389     out_y,  \
  390     out_w,  \
  391     out_h, \
  392     in_colormodel,  \
  393     out_colormodel, \
  394     bg_color, \
  395     in_rowspan, \
  396     out_rowspan, \
  397     scale, \
  398     out_pixelsize, \
  399     in_pixelsize, \
  400     row_table, \
  401     column_table, \
  402     bg_r, \
  403     bg_g, \
  404     bg_b
  405 
  406 // Handle planar cmodels separately
  407     switch(in_colormodel)
  408     {
  409         case BC_RGB_FLOAT:
  410         case BC_RGBA_FLOAT:
  411             cmodel_float(PERMUTATION_VALUES);
  412             break;
  413 
  414         case BC_YUV420P:
  415         case BC_YUV422P:
  416             cmodel_yuv420p(PERMUTATION_VALUES);
  417             break;
  418 
  419         case BC_YUV9P:
  420             cmodel_yuv9p(PERMUTATION_VALUES);
  421             break;
  422 
  423         case BC_YUV444P:
  424             cmodel_yuv444p(PERMUTATION_VALUES);
  425             break;
  426 
  427         case BC_YUV422:
  428             cmodel_yuv422(PERMUTATION_VALUES);
  429             break;
  430 
  431         default:
  432             cmodel_default(PERMUTATION_VALUES);
  433             break;
  434     }
  435 
  436 /*
  437  * printf("cmodel_transfer 100 %d %d\n", 
  438  * in_colormodel, 
  439  * out_colormodel);
  440  */
  441 
  442     free(column_table);
  443     free(row_table);
  444 }
  445 
  446 int cmodel_bc_to_x(int color_model)
  447 {
  448     switch(color_model)
  449     {
  450         case BC_YUV420P:
  451             return FOURCC_YV12;
  452             break;
  453         case BC_YUV422:
  454             return FOURCC_YUV2;
  455             break;
  456     }
  457     return -1;
  458 }
  459 
  460 void cmodel_to_text(char *string, int cmodel)
  461 {
  462     switch(cmodel)
  463     {
  464         case BC_RGB888:       strcpy(string, "RGB-8 Bit");   break;
  465         case BC_RGBA8888:     strcpy(string, "RGBA-8 Bit");  break;
  466         case BC_RGB161616:    strcpy(string, "RGB-16 Bit");  break;
  467         case BC_RGBA16161616: strcpy(string, "RGBA-16 Bit"); break;
  468         case BC_YUV888:       strcpy(string, "YUV-8 Bit");   break;
  469         case BC_YUVA8888:     strcpy(string, "YUVA-8 Bit");  break;
  470         case BC_YUV161616:    strcpy(string, "YUV-16 Bit");  break;
  471         case BC_YUVA16161616: strcpy(string, "YUVA-16 Bit"); break;
  472         case BC_RGB_FLOAT:    strcpy(string, "RGB-FLOAT");   break;
  473         case BC_RGBA_FLOAT:   strcpy(string, "RGBA-FLOAT");  break;
  474         default: strcpy(string, "RGB-8 Bit"); break;
  475     }
  476 }
  477 
  478 int cmodel_from_text(const char *text)
  479 {
  480     if(!strcasecmp(text, "RGB-8 Bit"))   return BC_RGB888;
  481     if(!strcasecmp(text, "RGBA-8 Bit"))  return BC_RGBA8888;
  482     if(!strcasecmp(text, "RGB-16 Bit"))  return BC_RGB161616;
  483     if(!strcasecmp(text, "RGBA-16 Bit")) return BC_RGBA16161616;
  484     if(!strcasecmp(text, "RGB-FLOAT"))   return BC_RGB_FLOAT;
  485     if(!strcasecmp(text, "RGBA-FLOAT"))  return BC_RGBA_FLOAT;
  486     if(!strcasecmp(text, "YUV-8 Bit"))   return BC_YUV888;
  487     if(!strcasecmp(text, "YUVA-8 Bit"))  return BC_YUVA8888;
  488     if(!strcasecmp(text, "YUV-16 Bit"))  return BC_YUV161616;
  489     if(!strcasecmp(text, "YUVA-16 Bit")) return BC_YUVA16161616;
  490     return BC_RGB888;
  491 }
  492 
  493 int cmodel_is_yuv(int colormodel)
  494 {
  495     switch(colormodel)
  496     {
  497         case BC_YUV888:
  498         case BC_YUVA8888:
  499         case BC_YUV161616:
  500         case BC_YUVA16161616:
  501         case BC_YUV422:
  502         case BC_YUV420P:
  503         case BC_YUV422P:
  504         case BC_YUV444P:
  505         case BC_YUV411P:
  506             return 1;
  507             break;
  508         
  509         default:
  510             return 0;
  511             break;
  512     }
  513 }
  514 
  515 
  516 
  517 
  518