jccolor.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 46k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2.  * jccolor.c
  3.  *
  4.  * Copyright (C) 1991-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains input colorspace conversion routines.
  9.  */
  10. #pragma warning( disable : 4799 )
  11. #define JPEG_INTERNALS
  12. #include "jinclude.h"
  13. #include "jpeglib.h"
  14. #ifdef NIFTY
  15. #include <math.h>
  16. #define SCALE_PREC      5
  17. #define SCALE_RND       (1 << (SCALE_PREC - 1))
  18. #define SCALE           (1 << SCALE_PREC)
  19. #define unscale(x)      (((long)(x) + SCALE_RND) >> SCALE_PREC)
  20. #define clip(x)         (((long)(x) & ~0xff) ? (((long)(x) < 0) ? 0 : 255) : (long)(x))
  21. #endif
  22. /* Private subobject */
  23. typedef struct {
  24.   struct jpeg_color_converter pub; /* public fields */
  25.   /* Private state for RGB->YCC conversion */
  26.   INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
  27. } my_color_converter;
  28. typedef my_color_converter * my_cconvert_ptr;
  29. extern void MRGB2YCbCr(
  30. int rows,
  31. int cols,
  32. unsigned char *inRGB,
  33. unsigned char *outY,
  34. unsigned char *outU,
  35. unsigned char *outV);
  36. extern void MRGBA2YCbCrA(
  37. int rows,
  38. int cols,
  39. unsigned char *inRGB,
  40. unsigned char *outY,
  41. unsigned char *outU,
  42. unsigned char *outV,
  43. unsigned char *outA);
  44. extern void MRGBA2YCbCrALegacy(
  45. int rows,
  46. int cols,
  47. unsigned char *inRGB,
  48. unsigned char *outY,
  49. unsigned char *outU,
  50. unsigned char *outV,
  51. unsigned char *outA);
  52. // ******************************************************************
  53. // Macros and Constants
  54. #define INT32 long int
  55. #define FCONVERSION_BITS 15
  56. #define ICONVERSION_BITS  8
  57. const __int64  const_0 = 0x0000000000000000;
  58. const __int64  const_1 = 0x0001000100010001;
  59. const __int64  const_128 = 0x0080008000800080;
  60. // These constants correspond to CCIR 601-1
  61. // Y  = [ (9798*R + 19235*G +  3736*B) / 32768]
  62. // Cb = [(-5529*R - 10855*G + 16384*B) / 32768] + 128
  63. // Cr = [(16384*R - 13720*G -  2664*B) / 32768] + 128
  64. //Conventional floating point equations:
  65. // Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
  66. // Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + 0.5
  67. // Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + 0.5
  68. //Yr = 2646 Yg = 4b23 Yb = 0e98
  69. //Ur = ea67 Ug = d599 Ub = 4000
  70. //Vr = 4000 Vg = ca68 Vb = f598
  71. // constants for RGB->YCrCb
  72. const __int64  const_YR0GR = 0x264600004B232646;
  73. const __int64  const_YBG0B = 0x0E984B2300000E98;
  74. const __int64  const_UR0GR = 0xEA670000D599EA67;
  75. const __int64  const_UBG0B = 0x4000D59900004000;
  76. const __int64  const_VR0GR = 0x40000000CA684000;
  77. const __int64  const_VBG0B = 0xF598CA680000F598;
  78. // constants for RGBA->YCrCbA
  79. const __int64  const2_YGRGR = 0x4B2326464B232646;
  80. const __int64  const2_Y0B0B = 0x00000E9800000E98;
  81. const __int64  const2_UGRGR = 0xD599EA67D599EA67;
  82. const __int64  const2_U0B0B = 0x0000400000004000;
  83. const __int64  const2_VGRGR = 0xCA684000CA684000;
  84. const __int64  const2_V0B0B = 0x0000F5980000F598;
  85. const __int64  const2_A = 0x0001000000010000;
  86. const __int64  const2_Legacy = 0x00FFFFFF00FFFFFF;
  87. // These constants correspond to the original FPX SDK
  88. // ... using 2^15
  89. //Y  = [ (9869*R + 19738*G +  3290*B) / 32768]
  90. //Cb = [(-4935*R -  9869*G + 14739*B) / 32768] + 128
  91. //Cr = [(14312*R - 12336*G -  2056*B) / 32768] + 128
  92. //Conventional floating point equations:
  93. // Y  =  0.30118*R + 0.60235*G + 0.10039*B
  94. // Cb = -0.15059*R - 0.30118*G + 0.44981*B + 0.5
  95. // Cr =  0.43676*R - 0.37647*G - 0.06274*G + 0.5
  96. //Yr = 268d Yg = 4d1a Yb = 0cda
  97. //Ur = ecb9 Ug = d973 Ub = 3993
  98. //Vr = 37e8 Vg = cfd0 Vb = f7f8
  99. // constants for RGB->YCrCb
  100. //const __int64  const_YR0GR = 0x268D00004D1A268D;
  101. //const __int64  const_YBG0B = 0x0CDA4D1A00000CDA;
  102. //const __int64  const_UR0GR = 0xECB90000D973ECB9;
  103. //const __int64  const_UBG0B = 0x3993D97300003993;
  104. //const __int64  const_VR0GR = 0x37E80000CFD037E8;
  105. //const __int64  const_VBG0B = 0xF7F8CFD00000F7F8;
  106. // constants for RGBA->YCrCbA
  107. //const __int64  const2_YGRGR = 0x4D1A268D4D1A268D;
  108. //const __int64  const2_Y0B0B = 0x00000CDA00000CDA;
  109. //const __int64  const2_UGRGR = 0xD973ECB9D973ECB9;
  110. //const __int64  const2_U0B0B = 0x0000399300003993;
  111. //const __int64  const2_VGRGR = 0xCFD037E8CFD037E8;
  112. //const __int64  const2_V0B0B = 0x0000F7F80000F7F8;
  113. //const __int64  const2_A = 0x0001000000010000;
  114. //const __int64  const2_Legacy = 0x00FFFFFF00FFFFFF;
  115. // ... using 2^8
  116. //const __int64  const_X0YY0 = 0x0000010001000000;
  117. //const __int64  const_RVUVU = 0x019A0000019A0000;
  118. //const __int64  const_GVUVU = 0xFF33FFABFF33FFAB;
  119. //const __int64  const_BVUVU = 0x0000020000000200;
  120. __int64  temp0, tempY, tempU, tempV, tempA;
  121. /**************** RGB -> YCbCr conversion: most common case **************/
  122. /*
  123.  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  124.  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  125.  * The conversion equations to be implemented are therefore
  126.  * Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
  127.  * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
  128.  * Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
  129.  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  130.  * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
  131.  * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
  132.  * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
  133.  * were not represented exactly.  Now we sacrifice exact representation of
  134.  * maximum red and maximum blue in order to get exact grayscales.
  135.  *
  136.  * To avoid floating-point arithmetic, we represent the fractional constants
  137.  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  138.  * the products by 2^16, with appropriate rounding, to get the correct answer.
  139.  *
  140.  * For even more speed, we avoid doing any multiplications in the inner loop
  141.  * by precalculating the constants times R,G,B for all possible values.
  142.  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  143.  * for 12-bit samples it is still acceptable.  It's not very reasonable for
  144.  * 16-bit samples, but if you want lossless storage you shouldn't be changing
  145.  * colorspace anyway.
  146.  * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
  147.  * in the tables to save adding them separately in the inner loop.
  148.  */
  149. #define SCALEBITS 16 /* speediest right-shift on some machines */
  150. #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
  151. #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
  152. #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  153. /* We allocate one big table and divide it up into eight parts, instead of
  154.  * doing eight alloc_small requests.  This lets us use a single table base
  155.  * address, which can be held in a register in the inner loops on many
  156.  * machines (more than can hold all eight addresses, anyway).
  157.  */
  158. #define R_Y_OFF 0 /* offset to R => Y section */
  159. #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
  160. #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
  161. #define R_CB_OFF (3*(MAXJSAMPLE+1))
  162. #define G_CB_OFF (4*(MAXJSAMPLE+1))
  163. #define B_CB_OFF (5*(MAXJSAMPLE+1))
  164. #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
  165. #define G_CR_OFF (6*(MAXJSAMPLE+1))
  166. #define B_CR_OFF (7*(MAXJSAMPLE+1))
  167. #define TABLE_SIZE (8*(MAXJSAMPLE+1))
  168. #ifdef NIFTY
  169. /*
  170.  * Initialize for RGB->PhotoYCC colorspace conversion.
  171.  */
  172. METHODDEF (void)
  173. rgb_pycc_start (j_compress_ptr cinfo)
  174. {
  175.  
  176. }
  177. /*
  178.  * RGB->PhotoYCC colorspace convertion.
  179.  */
  180. METHODDEF (void)
  181. rgb_pycc_convert (j_compress_ptr cinfo,
  182.                  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  183.                  JDIMENSION output_row, int num_rows)
  184. {
  185.   my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
  186.   register JSAMPROW inptr;
  187.   register JSAMPROW outptr0, outptr1, outptr2;
  188.   register JDIMENSION col;
  189.   JDIMENSION num_cols = cinfo->image_width;
  190.   unsigned char r, g, b;
  191.  
  192.   while (--num_rows >= 0) {
  193.     inptr = *input_buf++;
  194.     outptr0 = output_buf[0][output_row];
  195.     outptr1 = output_buf[1][output_row];
  196.     outptr2 = output_buf[2][output_row];
  197.     output_row++;
  198.     for (col = 0; col < num_cols; col++) {
  199.       r = GETJSAMPLE(inptr[RGB_RED]);
  200.       g = GETJSAMPLE(inptr[RGB_GREEN]);
  201.       b = GETJSAMPLE(inptr[RGB_BLUE]);
  202.       inptr+=RGB_PIXELSIZE;
  203.  
  204.       /* Y */
  205.       outptr0[col] = (JSAMPLE)((float)((float)r * 0.2200179046) + (float)((float)g * 0.4322754970) + (float)((float)b * 0.0838667868));
  206.       /* C1 */
  207.       outptr1[col] = (JSAMPLE)((float)((float)r * -0.1347546425) - (float)((float)g * 0.2647563169) + (float)((float)b * 0.3995109594) + 156);
  208.       /* C2 */
  209.       outptr2[col] = (JSAMPLE)((float)((float)r * 0.3849177482) - (float)((float)g * 0.3223733380) + (float)((float)b * 0.0625444102) + 137);
  210.     }
  211.   }
  212. }
  213. #endif
  214. /*
  215.  * Initialize for RGB->YCC colorspace conversion.
  216.  */
  217. METHODDEF(void)
  218. rgb_ycc_start (j_compress_ptr cinfo)
  219. {
  220.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  221.   INT32 * rgb_ycc_tab;
  222.   INT32 i;
  223.   /* Allocate and fill in the conversion tables. */
  224.   cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
  225.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  226. (TABLE_SIZE * SIZEOF(INT32)));
  227.   for (i = 0; i <= MAXJSAMPLE; i++) {
  228.     rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  229.     rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  230.     rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
  231.     rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  232.     rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  233.     /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  234.      * This ensures that the maximum output will round to MAXJSAMPLE
  235.      * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
  236.      */
  237.     rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  238. /*  B=>Cb and R=>Cr tables are the same
  239.     rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  240. */
  241.     rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  242.     rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  243.   }
  244. }
  245. /*
  246.  * Convert some rows of samples to the JPEG colorspace.
  247.  *
  248.  * Note that we change from the application's interleaved-pixel format
  249.  * to our internal noninterleaved, one-plane-per-component format.
  250.  * The input buffer is therefore three times as wide as the output buffer.
  251.  *
  252.  * A starting row offset is provided only for the output buffer.  The caller
  253.  * can easily adjust the passed input_buf value to accommodate any row
  254.  * offset required on that side.
  255.  */
  256. METHODDEF(void)
  257. rgb_ycc_convert (j_compress_ptr cinfo,
  258.  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  259.  JDIMENSION output_row, int num_rows)
  260. {
  261.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  262.   register int r, g, b;
  263.   register INT32 * ctab = cconvert->rgb_ycc_tab;
  264.   register JSAMPROW inptr;
  265.   register JSAMPROW outptr0, outptr1, outptr2;
  266.   register JDIMENSION col;
  267.   JDIMENSION num_cols = cinfo->image_width;
  268.   JDIMENSION tail_cols = num_cols&7;
  269.   JDIMENSION mmx_cols=num_cols&~7;
  270.   while (--num_rows >= 0) {
  271.     inptr = *input_buf++;
  272.     outptr0 = output_buf[0][output_row];
  273.     outptr1 = output_buf[1][output_row];
  274.     outptr2 = output_buf[2][output_row];
  275.     output_row++;
  276. //
  277. // Need to add #ifdef for Alpha port
  278. //
  279. #if defined (_X86_)
  280. if (vfMMXMachine)
  281. {
  282. MRGB2YCbCr( (int)(1), mmx_cols, inptr, outptr0, outptr1, outptr2);
  283. inptr += 3*mmx_cols;
  284. for (col = mmx_cols; col < num_cols; col++) {
  285.   r = GETJSAMPLE(inptr[RGB_RED]);
  286.   g = GETJSAMPLE(inptr[RGB_GREEN]);
  287.   b = GETJSAMPLE(inptr[RGB_BLUE]);
  288.       inptr += RGB_PIXELSIZE;
  289.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  290.        * must be too; we do not need an explicit range-limiting operation.
  291.        * Hence the value being shifted is never negative, and we don't
  292.        * need the general RIGHT_SHIFT macro.
  293.        */
  294.       /* Y */
  295.       outptr0[col] = (JSAMPLE)
  296. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  297.  >> SCALEBITS);
  298.       /* Cb */
  299.       outptr1[col] = (JSAMPLE)
  300. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  301.  >> SCALEBITS);
  302.       /* Cr */
  303.       outptr2[col] = (JSAMPLE)
  304. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  305.  >> SCALEBITS);
  306. }
  307. }
  308. else
  309. #endif
  310. {
  311.     for (col = 0; col < num_cols; col++) {
  312.       r = GETJSAMPLE(inptr[RGB_RED]);
  313.       g = GETJSAMPLE(inptr[RGB_GREEN]);
  314.       b = GETJSAMPLE(inptr[RGB_BLUE]);
  315.       inptr += RGB_PIXELSIZE;
  316.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  317.        * must be too; we do not need an explicit range-limiting operation.
  318.        * Hence the value being shifted is never negative, and we don't
  319.        * need the general RIGHT_SHIFT macro.
  320.        */
  321.       /* Y */
  322.       outptr0[col] = (JSAMPLE)
  323. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  324.  >> SCALEBITS);
  325.       /* Cb */
  326.       outptr1[col] = (JSAMPLE)
  327. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  328.  >> SCALEBITS);
  329.       /* Cr */
  330.       outptr2[col] = (JSAMPLE)
  331. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  332.  >> SCALEBITS);
  333.     }
  334. }
  335.   }
  336. }
  337. /**************** Cases other than RGB -> YCbCr **************/
  338. /*
  339.  * Convert some rows of samples to the JPEG colorspace.
  340.  * This version handles RGB->grayscale conversion, which is the same
  341.  * as the RGB->Y portion of RGB->YCbCr.
  342.  * We assume rgb_ycc_start has been called (we only use the Y tables).
  343.  */
  344. METHODDEF(void)
  345. rgb_gray_convert (j_compress_ptr cinfo,
  346.   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  347.   JDIMENSION output_row, int num_rows)
  348. {
  349.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  350.   register int r, g, b;
  351.   register INT32 * ctab = cconvert->rgb_ycc_tab;
  352.   register JSAMPROW inptr;
  353.   register JSAMPROW outptr;
  354.   register JDIMENSION col;
  355.   JDIMENSION num_cols = cinfo->image_width;
  356.   while (--num_rows >= 0) {
  357.     inptr = *input_buf++;
  358.     outptr = output_buf[0][output_row];
  359.     output_row++;
  360.     for (col = 0; col < num_cols; col++) {
  361.       r = GETJSAMPLE(inptr[RGB_RED]);
  362.       g = GETJSAMPLE(inptr[RGB_GREEN]);
  363.       b = GETJSAMPLE(inptr[RGB_BLUE]);
  364.       inptr += RGB_PIXELSIZE;
  365.       /* Y */
  366.       outptr[col] = (JSAMPLE)
  367. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  368.  >> SCALEBITS);
  369.     }
  370.   }
  371. }
  372. #ifdef NIFTY
  373. METHODDEF (void)
  374. rgba_ycbcra_convert (j_compress_ptr cinfo,
  375.    JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  376.    JDIMENSION output_row, int num_rows)
  377. {
  378.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  379.   register int r, g, b;
  380.   register INT32 * ctab = cconvert->rgb_ycc_tab;
  381.   register JSAMPROW inptr;
  382.   register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  383.   register JDIMENSION col;
  384.   JDIMENSION num_cols = cinfo->image_width;
  385.   JDIMENSION tail_cols = num_cols&7;
  386.   JDIMENSION mmx_cols=num_cols&~7;
  387.  
  388.   while (--num_rows >= 0) {
  389.     inptr = *input_buf++;
  390.     outptr0 = output_buf[0][output_row];
  391.     outptr1 = output_buf[1][output_row];
  392. outptr2 = output_buf[2][output_row];
  393.     outptr3 = output_buf[3][output_row];
  394.     output_row++;
  395. //
  396. // Need to add #ifdef for Alpha port
  397. //
  398. #if defined (_X86_)
  399.         if (vfMMXMachine)
  400. {
  401. MRGBA2YCbCrA( (int)(1), mmx_cols, inptr, outptr0, outptr1, outptr2, outptr3);
  402. inptr += 4*mmx_cols;
  403.     for (col = mmx_cols; col < num_cols; col++) {
  404.       r = GETJSAMPLE(inptr[0]);
  405.       g = GETJSAMPLE(inptr[1]);
  406.       b = GETJSAMPLE(inptr[2]);
  407.       /* Alpha passes through as-is */
  408.       outptr3[col] = inptr[3];  /* don't need GETJSAMPLE here */
  409.       inptr += 4;
  410.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  411.        * must be too; we do not need an explicit range-limiting operation.
  412.        * Hence the value being shifted is never negative, and we don't
  413.        * need the general RIGHT_SHIFT macro.
  414.        */
  415.       /* Y */
  416.       outptr0[col] = (JSAMPLE)
  417.                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  418.              >> SCALEBITS);
  419.       /* Cb */
  420.       outptr1[col] = (JSAMPLE)
  421.                 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  422.                  >> SCALEBITS);
  423.       /* Cr */
  424.       outptr2[col] = (JSAMPLE)
  425.                 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  426.                  >> SCALEBITS);
  427.     }
  428. }
  429. else
  430.  #endif // defined (_X86_)
  431.         {
  432.     for (col = 0; col < num_cols; col++) {
  433.       r = GETJSAMPLE(inptr[0]);
  434.       g = GETJSAMPLE(inptr[1]);
  435.       b = GETJSAMPLE(inptr[2]);
  436.       /* Alpha passes through as-is */
  437.       outptr3[col] = inptr[3];  /* don't need GETJSAMPLE here */
  438.       inptr += 4;
  439.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  440.        * must be too; we do not need an explicit range-limiting operation.
  441.        * Hence the value being shifted is never negative, and we don't
  442.        * need the general RIGHT_SHIFT macro.
  443.        */
  444.       /* Y */
  445.       outptr0[col] = (JSAMPLE)
  446.                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  447.              >> SCALEBITS);
  448.       /* Cb */
  449.       outptr1[col] = (JSAMPLE)
  450.                 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  451.                  >> SCALEBITS);
  452.       /* Cr */
  453.       outptr2[col] = (JSAMPLE)
  454.                 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  455.                  >> SCALEBITS);
  456.     }
  457. }
  458.   }
  459. }
  460. METHODDEF (void)
  461. rgba_ycbcralegacy_convert (j_compress_ptr cinfo,
  462.    JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  463.    JDIMENSION output_row, int num_rows)
  464. {
  465.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  466.   register int r, g, b;
  467.   register INT32 * ctab = cconvert->rgb_ycc_tab;
  468.   register JSAMPROW inptr;
  469.   register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  470.   register JDIMENSION col;
  471.   JDIMENSION num_cols = cinfo->image_width;
  472.   JDIMENSION tail_cols = num_cols&7;
  473.   JDIMENSION mmx_cols=num_cols&~7;
  474.  
  475.   while (--num_rows >= 0) {
  476.     inptr = *input_buf++;
  477.     outptr0 = output_buf[0][output_row];
  478.     outptr1 = output_buf[1][output_row];
  479.     outptr2 = output_buf[2][output_row];
  480.     outptr3 = output_buf[3][output_row];
  481.     output_row++;
  482. //
  483. // Need to add #ifdef for Alpha port
  484. //
  485. #if defined (_X86_)
  486. if (vfMMXMachine)
  487. {
  488. MRGBA2YCbCrALegacy( (int)(1), mmx_cols, inptr, outptr0, outptr1, outptr2, outptr3);
  489. inptr += 4*mmx_cols;
  490.     for (col = mmx_cols; col < num_cols; col++) {
  491.       r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  492.       g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  493.       b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  494.       /* Alpha passes through as-is */
  495.       outptr3[col] = inptr[3];  /* don't need GETJSAMPLE here */
  496.       inptr += 4;
  497.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  498.        * must be too; we do not need an explicit range-limiting operation.
  499.        * Hence the value being shifted is never negative, and we don't
  500.        * need the general RIGHT_SHIFT macro.
  501.        */
  502.       /* Y */
  503.       outptr0[col] = (JSAMPLE)
  504.                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  505.                  >> SCALEBITS);
  506.       /* Cb */
  507.       outptr1[col] = (JSAMPLE)
  508.                 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  509.                  >> SCALEBITS);
  510.       /* Cr */
  511.       outptr2[col] = (JSAMPLE)
  512.                 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  513.                  >> SCALEBITS);
  514.     }
  515. }
  516. else
  517. #endif // defined (_X86_)
  518.         {
  519.     for (col = 0; col < num_cols; col++) {
  520.       r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  521.       g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  522.       b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  523.       /* Alpha passes through as-is */
  524.       outptr3[col] = inptr[3];  /* don't need GETJSAMPLE here */
  525.       inptr += 4;
  526.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  527.        * must be too; we do not need an explicit range-limiting operation.
  528.        * Hence the value being shifted is never negative, and we don't
  529.        * need the general RIGHT_SHIFT macro.
  530.        */
  531.       /* Y */
  532.       outptr0[col] = (JSAMPLE)
  533.                 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  534.                  >> SCALEBITS);
  535.       /* Cb */
  536.       outptr1[col] = (JSAMPLE)
  537.                 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  538.                  >> SCALEBITS);
  539.       /* Cr */
  540.       outptr2[col] = (JSAMPLE)
  541.                 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  542.                  >> SCALEBITS);
  543.     }
  544. }
  545.   }
  546. }
  547. #endif
  548. /*
  549.  * Convert some rows of samples to the JPEG colorspace.
  550.  * This version handles Adobe-style CMYK->YCCK conversion,
  551.  * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  552.  * conversion as above, while passing K (black) unchanged.
  553.  * We assume rgb_ycc_start has been called.
  554.  */
  555. METHODDEF(void)
  556. cmyk_ycck_convert (j_compress_ptr cinfo,
  557.    JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  558.    JDIMENSION output_row, int num_rows)
  559. {
  560.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  561.   register int r, g, b;
  562.   register INT32 * ctab = cconvert->rgb_ycc_tab;
  563.   register JSAMPROW inptr;
  564.   register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  565.   register JDIMENSION col;
  566.   JDIMENSION num_cols = cinfo->image_width;
  567.   while (--num_rows >= 0) {
  568.     inptr = *input_buf++;
  569.     outptr0 = output_buf[0][output_row];
  570.     outptr1 = output_buf[1][output_row];
  571.     outptr2 = output_buf[2][output_row];
  572.     outptr3 = output_buf[3][output_row];
  573.     output_row++;
  574.     for (col = 0; col < num_cols; col++) {
  575.       r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  576.       g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  577.       b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  578.       /* K passes through as-is */
  579.       outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
  580.       inptr += 4;
  581.       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  582.        * must be too; we do not need an explicit range-limiting operation.
  583.        * Hence the value being shifted is never negative, and we don't
  584.        * need the general RIGHT_SHIFT macro.
  585.        */
  586.       /* Y */
  587.       outptr0[col] = (JSAMPLE)
  588. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  589.  >> SCALEBITS);
  590.       /* Cb */
  591.       outptr1[col] = (JSAMPLE)
  592. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  593.  >> SCALEBITS);
  594.       /* Cr */
  595.       outptr2[col] = (JSAMPLE)
  596. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  597.  >> SCALEBITS);
  598.     }
  599.   }
  600. }
  601. /*
  602.  * Convert some rows of samples to the JPEG colorspace.
  603.  * This version handles grayscale output with no conversion.
  604.  * The source can be either plain grayscale or YCbCr (since Y == gray).
  605.  */
  606. METHODDEF(void)
  607. grayscale_convert (j_compress_ptr cinfo,
  608.    JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  609.    JDIMENSION output_row, int num_rows)
  610. {
  611.   register JSAMPROW inptr;
  612.   register JSAMPROW outptr;
  613.   register JDIMENSION col;
  614.   JDIMENSION num_cols = cinfo->image_width;
  615.   int instride = cinfo->input_components;
  616.   while (--num_rows >= 0) {
  617.     inptr = *input_buf++;
  618.     outptr = output_buf[0][output_row];
  619.     output_row++;
  620.     for (col = 0; col < num_cols; col++) {
  621.       outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
  622.       inptr += instride;
  623.     }
  624.   }
  625. }
  626. /*
  627.  * Convert some rows of samples to the JPEG colorspace.
  628.  * This version handles multi-component colorspaces without conversion.
  629.  * We assume input_components == num_components.
  630.  */
  631. METHODDEF(void)
  632. null_convert (j_compress_ptr cinfo,
  633.       JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  634.       JDIMENSION output_row, int num_rows)
  635. {
  636.   register JSAMPROW inptr;
  637.   register JSAMPROW outptr;
  638.   register JDIMENSION col;
  639.   register int ci;
  640.   int nc = cinfo->num_components;
  641.   JDIMENSION num_cols = cinfo->image_width;
  642.   while (--num_rows >= 0) {
  643.     /* It seems fastest to make a separate pass for each component. */
  644.     for (ci = 0; ci < nc; ci++) {
  645.       inptr = *input_buf;
  646.       outptr = output_buf[ci][output_row];
  647.       for (col = 0; col < num_cols; col++) {
  648. outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  649. inptr += nc;
  650.       }
  651.     }
  652.     input_buf++;
  653.     output_row++;
  654.   }
  655. }
  656. /*
  657.  * Empty method for start_pass.
  658.  */
  659. METHODDEF(void)
  660. null_method (j_compress_ptr cinfo)
  661. {
  662.   /* no work needed */
  663. }
  664. /*
  665.  * Module initialization routine for input colorspace conversion.
  666.  */
  667. GLOBAL(void)
  668. jinit_color_converter (j_compress_ptr cinfo)
  669. {
  670.   my_cconvert_ptr cconvert;
  671.   cconvert = (my_cconvert_ptr)
  672.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  673. SIZEOF(my_color_converter));
  674.   cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  675.   /* set start_pass to null method until we find out differently */
  676.   cconvert->pub.start_pass = null_method;
  677.   /* Make sure input_components agrees with in_color_space */
  678.   switch (cinfo->in_color_space) {
  679.   case JCS_GRAYSCALE:
  680.     if (cinfo->input_components != 1)
  681.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  682.     break;
  683. #ifdef NIFTY
  684.     case JCS_YCC:
  685.     if (cinfo->input_components != 3)
  686.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  687.     break;
  688.     case JCS_RGBA:
  689.     if (cinfo->input_components != 4)
  690.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  691.     break;
  692.     case JCS_YCbCrA:
  693.     if (cinfo->input_components != 4)
  694.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  695.     break;
  696.     
  697. case JCS_YCbCrALegacy:
  698.     if (cinfo->input_components != 4)
  699.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  700.     break;
  701.     case JCS_YCCA:
  702.     if (cinfo->input_components != 4)
  703.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  704.     break;
  705. #endif
  706. case JCS_RGB:
  707. #if RGB_PIXELSIZE != 3
  708.     if (cinfo->input_components != RGB_PIXELSIZE)
  709.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  710.     break;
  711. #endif /* else share code with YCbCr */
  712.   case JCS_YCbCr:
  713.     if (cinfo->input_components != 3)
  714.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  715.     break;
  716.   case JCS_CMYK:
  717.   case JCS_YCCK:
  718.     if (cinfo->input_components != 4)
  719.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  720.     break;
  721.   default: /* JCS_UNKNOWN can be anything */
  722.     if (cinfo->input_components < 1)
  723.       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  724.     break;
  725.   }
  726.   /* Check num_components, set conversion method based on requested space */
  727.   switch (cinfo->jpeg_color_space) {
  728.   case JCS_GRAYSCALE:
  729.     if (cinfo->num_components != 1)
  730.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  731.     if (cinfo->in_color_space == JCS_GRAYSCALE)
  732.       cconvert->pub.color_convert = grayscale_convert;
  733.     else if (cinfo->in_color_space == JCS_RGB) {
  734.       cconvert->pub.start_pass = rgb_ycc_start;
  735.       cconvert->pub.color_convert = rgb_gray_convert;
  736.     } else if (cinfo->in_color_space == JCS_YCbCr)
  737.       cconvert->pub.color_convert = grayscale_convert;
  738.     else
  739.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  740.     break;
  741. #ifdef NIFTY
  742.   /* Store and compress data as PhotoYCC */
  743.   /* Only current option is to start with PhotoYCC
  744.    * although I do include the function RGB->PhotoYCC
  745.    * in the compressor, I don't think it's a good idea
  746.    * to rotate to PhotoYCC from RGB in this context.
  747.    * If subsampling is required, then just use YCrCb.
  748.    */
  749.   case JCS_YCC:
  750.     if (cinfo->num_components != 3)
  751.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  752.     if (cinfo->in_color_space == JCS_YCC)
  753.       cconvert->pub.color_convert = null_convert;
  754.     else
  755.       if (cinfo->in_color_space == JCS_RGB) {
  756. /* this is where the RGB->PhotoYCC could be called */
  757.         ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  758.       } else {
  759.         ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  760.       }
  761.     break;
  762.   case JCS_YCCA:
  763.     if (cinfo->num_components != 4)
  764.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  765.     if (cinfo->in_color_space == JCS_YCCA)
  766.       cconvert->pub.color_convert = null_convert;
  767.     else
  768.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  769.     break;
  770.   case JCS_RGBA:
  771.     if (cinfo->num_components != 4)
  772.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  773.     if (cinfo->in_color_space == JCS_RGBA) {
  774.       cconvert->pub.color_convert = null_convert;
  775.     } else {
  776.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  777.     }
  778.     break;
  779.   case JCS_YCbCrA:
  780.     if (cinfo->num_components != 4)
  781.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  782.     if (cinfo->in_color_space == JCS_YCbCrA)
  783.       cconvert->pub.color_convert = null_convert;
  784.     else if (cinfo->in_color_space == JCS_RGBA) {
  785.       cconvert->pub.start_pass = rgb_ycc_start;
  786.       cconvert->pub.color_convert = rgba_ycbcra_convert;
  787.     } else
  788.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  789.     break;
  790.   case JCS_YCbCrALegacy:
  791.     if (cinfo->num_components != 4)
  792.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  793.     if (cinfo->in_color_space == JCS_YCbCrALegacy)
  794.       cconvert->pub.color_convert = null_convert;
  795.     else if (cinfo->in_color_space == JCS_RGBA) {
  796.       cconvert->pub.start_pass = rgb_ycc_start;
  797.       cconvert->pub.color_convert = rgba_ycbcralegacy_convert;
  798.     } else
  799.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  800.     break;
  801. #endif
  802.   case JCS_RGB:
  803.     if (cinfo->num_components != 3)
  804.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  805.     if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
  806.       cconvert->pub.color_convert = null_convert;
  807.     else
  808.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  809.     break;
  810.   case JCS_YCbCr:
  811.     if (cinfo->num_components != 3)
  812.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  813.     if (cinfo->in_color_space == JCS_RGB) {
  814.       cconvert->pub.start_pass = rgb_ycc_start;
  815.       cconvert->pub.color_convert = rgb_ycc_convert;
  816.     } else if (cinfo->in_color_space == JCS_YCbCr)
  817.       cconvert->pub.color_convert = null_convert;
  818.     else
  819.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  820.     break;
  821.   case JCS_CMYK:
  822.     if (cinfo->num_components != 4)
  823.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  824.     if (cinfo->in_color_space == JCS_CMYK)
  825.       cconvert->pub.color_convert = null_convert;
  826.     else
  827.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  828.     break;
  829.   case JCS_YCCK:
  830.     if (cinfo->num_components != 4)
  831.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  832.     if (cinfo->in_color_space == JCS_CMYK) {
  833.       cconvert->pub.start_pass = rgb_ycc_start;
  834.       cconvert->pub.color_convert = cmyk_ycck_convert;
  835.     } else if (cinfo->in_color_space == JCS_YCCK)
  836.       cconvert->pub.color_convert = null_convert;
  837.     else
  838.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  839.     break;
  840.   default: /* allow null conversion of JCS_UNKNOWN */
  841.     if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  842. cinfo->num_components != cinfo->input_components)
  843.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  844.     cconvert->pub.color_convert = null_convert;
  845.     break;
  846.   }
  847. }
  848. //
  849. // Need to add #ifdef for Alpha port
  850. //
  851. #if defined (_X86_)
  852. void MRGB2YCbCr(
  853. int rows,
  854. int cols,
  855. unsigned char *inRGB,
  856. unsigned char *outY,
  857. unsigned char *outU,
  858. unsigned char *outV)
  859. {
  860. // make global to ensure proper stack alignment
  861. // __int64  temp0, tempY, tempU, tempV;
  862. __asm {
  863. // initializations
  864. //DS - IJG will always call with rows=1, so don't multiply
  865. // mov eax, rows
  866. // mov ebx, cols
  867. // mul ebx ;number pixels
  868. // reorder to take advantage of v-pipe
  869. mov esi, cols
  870. mov eax, inRGB
  871. shr esi, 3 ;number of loops = (rows*cols)/8
  872. mov edx, outV
  873. mov edi, esi ;loop counter in edi
  874. mov ecx, outU
  875. mov ebx, outY
  876. // top of loop
  877. RGBtoYUV:
  878. movq mm1, [eax] ;load #1 G2R2B1G1R1B0G0R0 -> mm1
  879. pxor mm6, mm6 ;0 -> mm6
  880. movq mm0, mm1 ;G2R2B1G1R1B0G0R0 -> mm0
  881. psrlq mm1, 16 ;00G2R2B1G1R1B0 -> mm1
  882. punpcklbw mm0, const_0 ;R1B0G0R0 -> mm0
  883. movq mm7, mm1 ;00G2R2B1G1R1B0 -> mm7
  884. punpcklbw mm1, const_0 ;B1G1R1B0 -> mm1
  885. movq mm2, mm0 ;R1B0G0R0 -> mm2
  886. pmaddwd mm0, const_YR0GR ;yrR1,ygG0+yrR0 -> mm0
  887. movq mm3, mm1 ;B1G1R1B0 -> mm3
  888. pmaddwd mm1, const_YBG0B ;ybB1+ygG1,ybB0 -> mm1
  889. movq mm4, mm2 ;R1B0G0R0 -> mm4
  890. pmaddwd mm2, const_UR0GR ;urR1,ugG0+urR0 -> mm2
  891. movq mm5, mm3 ;B1G1R1B0 -> mm5
  892. pmaddwd mm3, const_UBG0B ;ubB1+ugG1,ubB0 -> mm3
  893. punpckhbw mm7, mm6 ;00G2R2 -> mm7
  894. pmaddwd mm4, const_VR0GR ;vrR1,vgG0+vrR0 -> mm4
  895. paddd mm0, mm1 ;Y1Y0 -> mm0
  896. pmaddwd mm5, const_VBG0B ;vbB1+vgG1,vbB0 -> mm5
  897. // nop
  898. movq mm1, [eax][8] ;load #2 R5B4G4R4B3G3R3B2 -> mm1
  899. paddd mm2, mm3 ;U1U0 -> mm2
  900. movq mm6, mm1 ;R5B4G4R4B3G3R3B2 -> mm6
  901. // nop
  902. punpcklbw mm1, const_0 ;B3G3R3B2 -> mm1
  903. paddd mm4, mm5 ;V1V0 -> mm4
  904. movq mm5, mm1 ;B3G3R3B2 -> mm5
  905.   psllq mm1, 32 ;R3B200 -> mm1
  906. paddd mm1, mm7 ;R3B200 + 00G2R2 = R3B2G2R2 -> mm1
  907. // nop
  908. punpckhbw mm6, const_0 ;R5B4G4R4 -> mm6
  909. movq mm3, mm1 ;R3B2G2R2 -> mm3
  910. pmaddwd mm1, const_YR0GR ;yrR3,ygG2+yrR2 -> mm1
  911. movq mm7, mm5 ;B3G3R3B2 -> mm7
  912. pmaddwd mm5, const_YBG0B ;ybB3+ygG3,ybB2 -> mm5
  913. psrad mm0, FCONVERSION_BITS ;32-bit scaled Y1Y0 -> mm0
  914. movq temp0, mm6 ;R5B4G4R4 -> temp0
  915. movq mm6, mm3 ;R3B2G2R2 -> mm6
  916. pmaddwd mm6, const_UR0GR ;urR3,ugG2+urR2 -> mm6
  917. psrad mm2, FCONVERSION_BITS ;32-bit scaled U1U0 -> mm2
  918. paddd mm1, mm5 ;Y3Y2 -> mm1
  919. movq mm5, mm7 ;B3G3R3B2 -> mm5
  920. pmaddwd mm7, const_UBG0B ;ubB3+ugG3,ubB2
  921. psrad mm1, FCONVERSION_BITS ;32-bit scaled Y3Y2 -> mm1
  922. pmaddwd mm3, const_VR0GR ;vrR3,vgG2+vgR2
  923. packssdw mm0, mm1 ;Y3Y2Y1Y0 -> mm0
  924. pmaddwd mm5, const_VBG0B ;vbB3+vgG3,vbB2 -> mm5
  925. psrad mm4, FCONVERSION_BITS ;32-bit scaled V1V0 -> mm4
  926. movq mm1, [eax][16] ;load #3 B7G7R7B6G6R6B5G5 -> mm7
  927. paddd mm6, mm7 ;U3U2 -> mm6
  928. movq mm7, mm1 ;B7G7R7B6G6R6B5G5 -> mm1
  929. psrad mm6, FCONVERSION_BITS ;32-bit scaled U3U2 -> mm6
  930. paddd mm3, mm5 ;V3V2 -> mm3
  931. psllq mm7, 16 ;R7B6G6R6B5G500 -> mm7
  932. movq mm5, mm7 ;R7B6G6R6B5G500 -> mm5
  933. psrad mm3, FCONVERSION_BITS ;32-bit scaled V3V2 -> mm3
  934. movq tempY, mm0 ;32-bit scaled Y3Y2Y1Y0 -> tempY
  935. packssdw mm2, mm6 ;32-bit scaled U3U2U1U0 -> mm2
  936. movq mm0, temp0 ;R5B4G4R4 -> mm0
  937. // nop 
  938. punpcklbw mm7, const_0 ;B5G500 -> mm7
  939. movq mm6, mm0 ;R5B4G4R4 -> mm6
  940. movq tempU, mm2 ;32-bit scaled U3U2U1U0 -> tempU
  941. psrlq mm0, 32 ;00R5B4 -> mm0
  942. paddw mm7, mm0 ;B5G5R5B4 -> mm7
  943. movq mm2, mm6 ;B5B4G4R4 -> mm2
  944. pmaddwd mm2, const_YR0GR ;yrR5,ygG4+yrR4 -> mm2
  945. movq mm0, mm7 ;B5G5R5B4 -> mm0
  946. pmaddwd mm7, const_YBG0B ;ybB5+ygG5,ybB4 -> mm7
  947. packssdw mm4, mm3 ;32-bit scaled V3V2V1V0 -> mm4
  948. add eax, 24 ;increment RGB count
  949. // nop ;//JS
  950. movq tempV, mm4 ;32-bit scaled V3V2V1V0 -> tempV
  951. movq mm4, mm6 ;B5B4G4R4 -> mm4
  952. pmaddwd mm6, const_UR0GR ;urR5,ugG4+urR4
  953. movq mm3, mm0 ;B5G5R5B4 -> mm0
  954. pmaddwd mm0, const_UBG0B ;ubB5+ugG5,ubB4
  955. paddd mm2, mm7 ;Y5Y4 -> mm2
  956. pmaddwd mm4, const_VR0GR ;vrR5,vgG4+vrR4 -> mm4
  957. pxor mm7, mm7 ;0 -> mm7
  958. pmaddwd mm3, const_VBG0B ;vbB5+vgG5,vbB4 -> mm3
  959. punpckhbw mm1, mm7 ;B7G7R7B6 -> mm1
  960. paddd mm0, mm6 ;U5U4 -> mm0
  961. movq mm6, mm1 ;B7G7R7B6 -> mm6
  962. pmaddwd mm6, const_YBG0B ;ybB7+ygG7,ybB6 -> mm6
  963. punpckhbw mm5, mm7 ;R7B6G6R6 -> mm5
  964. movq mm7, mm5 ;R7B6G6R6 -> mm7
  965. paddd mm3, mm4 ;V5V4 -> mm3
  966. pmaddwd mm5, const_YR0GR ;yrR7,ygG6+yrR6 -> mm5
  967. movq mm4, mm1 ;B7G7R7B6 -> mm4
  968. pmaddwd mm4,const_UBG0B ;ubB7+ugG7,ubB6 -> mm4
  969. psrad mm0, FCONVERSION_BITS ;32-bit scaled U5U4 -> mm0
  970. psrad mm2, FCONVERSION_BITS ;32-bit scaled Y5Y4 -> mm2
  971. nop ;//JS
  972. paddd mm6, mm5 ;Y7Y6 -> mm6
  973. movq mm5, mm7 ;R7B6G6R6 -> mm5
  974. pmaddwd mm7, const_UR0GR ;urR7,ugG6+ugR6 -> mm7
  975. psrad mm3, FCONVERSION_BITS ;32-bit scaled V5V4 -> mm3
  976. pmaddwd mm1, const_VBG0B ;vbB7+vgG7,vbB6 -> mm1
  977. psrad mm6, FCONVERSION_BITS ;32-bit scaled Y7Y6 -> mm6
  978. packssdw mm2, mm6 ;Y7Y6Y5Y4 -> mm2
  979. // nop ;//JS
  980. pmaddwd mm5, const_VR0GR ;vrR7,vgG6+vrR6 -> mm5
  981. paddd mm7, mm4 ;U7U6 -> mm7
  982. psrad mm7, FCONVERSION_BITS ;32-bit scaled U7U6 -> mm7
  983. // nop
  984. movq mm6, tempY ;32-bit scaled Y3Y2Y1Y0 -> mm6
  985. packssdw mm0, mm7 ;32-bit scaled U7U6U5U4 -> mm0
  986. movq mm4, tempU ;32-bit scaled U3U2U1U0 -> mm4
  987. packuswb mm6, mm2 ;all 8 Y values -> mm6
  988. movq mm7, const_128 ;128,128,128,128 -> mm7
  989. paddd mm1, mm5 ;V7V6  -> mm1
  990. paddw mm0, mm7 ;add offset to U7U6U5U4
  991. // nop
  992. paddw mm4, mm7 ;add offset to U3U2U1U0
  993. psrad mm1, FCONVERSION_BITS ;32-bit scaled V7V6 -> mm1
  994. movq [ebx], mm6 ;store Y
  995. packuswb mm4, mm0 ;all 8 U values -> mm4
  996. movq mm5, tempV ;32-bit scaled V3V2V1V0 -> mm5
  997. packssdw mm3, mm1 ;V7V6V5V4 -> mm3
  998. paddw mm5, mm7 ;add offset to  V3V2V1V0
  999. paddw mm3, mm7 ;add offset to  V7V6V5V4
  1000. movq [ecx], mm4 ;store U
  1001. packuswb mm5, mm3 ;all 8 V values -> mm5
  1002. add ebx, 8 ;increment Y count
  1003. add ecx, 8 ;increment U count
  1004. movq [edx], mm5 ;store V
  1005. // nop
  1006. add edx, 8 ;increment V count
  1007. // nop
  1008. dec edi ;decrement loop counter
  1009. jnz RGBtoYUV ;do 24 more bytes if not 0
  1010. //JS  The following emms instruction is purposely commented out.
  1011. //emms       // commented out since it is done after the DCT
  1012. } // end of __asm
  1013. } // end of MRGB2YCbCr
  1014. void MRGBA2YCbCrA(
  1015. int rows,
  1016. int cols,
  1017. unsigned char *inRGBA,
  1018. unsigned char *outY,
  1019. unsigned char *outU,
  1020. unsigned char *outV,
  1021. unsigned char *outA)
  1022. {
  1023. // make global to align on stack properly
  1024. // __int64  tempY, tempU, tempV, tempA;
  1025. // written by Dave Shade - Intel Corp.
  1026. // Feb '97
  1027. //
  1028. // This color space conversion routine converts 
  1029. // true color pixels from RGBA to YCbCrA 
  1030. // one pass through the loop processes 4 pixels
  1031. // there is no provision for cols not an even multiple of 4
  1032. __asm {
  1033. // initializations
  1034. //DS - IJG will always call with rows=1, so don't multiply
  1035. // mov eax, rows
  1036. // mov ebx, cols
  1037. // mul ebx ;number pixels
  1038. // reorder to take advantage of Pentium v-pipe
  1039. mov edi, cols
  1040. mov eax, inRGBA
  1041. shr edi, 2 ;number of loops = (rows*cols)/4
  1042. mov edx, outV
  1043. mov ecx, outU
  1044. mov esi, outA
  1045. mov ebx, outY
  1046. // top of loop
  1047. RGBAtoYUVA:
  1048. movq mm3, [eax+8] ;load #1 A1B1G1R1A0B0G0R0 -> mm3
  1049. pxor mm6, mm6 ;0 -> mm6
  1050. movq mm4, mm3 ;A1B1G1R1A0B0G0R0 -> mm4
  1051. psrlq mm3, 32 ;00000000A1B1G1R1 -> mm3
  1052. punpcklwd mm4, mm3 ;A1B1A0B0G1R1G0R0 -> mm4
  1053. add esi, 4
  1054. movq mm0, mm4 ;A1B1A0B0G1R1G0R0 -> mm0
  1055. punpckhbw mm4, mm6 ;A1B1A0B0 -> mm4
  1056. movq mm3, mm4 ;A1B1A0B0 -> mm3
  1057. punpcklbw mm0, mm6 ;G1R1G0R0 -> mm0
  1058. pmaddwd mm3, const2_Y0B0B ;ybB1,ybB0 -> mm3
  1059. movq mm1, mm0 ;G1R1G0R0 -> mm1
  1060. pmaddwd mm0, const2_YGRGR ;yrG1+ygR1,ygG0+yrR0 -> mm0
  1061. movq mm5, mm4 ;A1B1A0B0 -> mm5
  1062. pmaddwd mm4, const2_U0B0B ;ubB1,ubB0 -> mm4
  1063. movq mm2, mm1 ;G1R1G0R0 -> mm2
  1064. pmaddwd mm1, const2_UGRGR ;urG1+ugR1,ugG0+urR0 -> mm1
  1065. movq mm7, mm5 ;A1B1A0B0 -> mm7
  1066. pmaddwd mm5, const2_V0B0B ;vbB1,vbB0 -> mm5
  1067. paddd mm0, mm3 ;Y1Y0 -> mm0
  1068. pmaddwd mm2, const2_VGRGR ;vgG1+vrR1,vgG0+vrR0 -> mm2
  1069. psrad mm0, FCONVERSION_BITS ;32 bit scaled Y1Y0
  1070. movq mm3, [eax] ;*load #2 A3B3G3R3A2B2G2R2 -> mm3
  1071. paddd mm1, mm4 ;U1U0 -> mm2
  1072. pmaddwd mm7, const2_A ;1*A1,1*A0
  1073. psrad mm1, FCONVERSION_BITS ;32 bit scaled U1U0
  1074. movq tempY, mm0 ;write out Y1Y0 in 32 bit format
  1075. paddd mm2, mm5 ;V1V0 -> mm2
  1076. movq mm4, mm3 ;*A3B3G3R3A2B2G2R2 -> mm4
  1077. psrad mm2, FCONVERSION_BITS ;32bit scaled V1V0
  1078. movq tempU, mm1 ;write out U1U0 in 32 bit format
  1079. psrlq mm3, 32 ;*00000000A3B3G3R3 -> mm3
  1080. movq tempV, mm2 ;write out V1V0 in 32 bit format
  1081. punpcklwd mm4, mm3 ;*A3B3A2B2G3R3G2R2 -> mm4
  1082. movq tempA, mm7
  1083. movq mm0, mm4 ;*A3B3A2B2G3R3G2R2 -> mm0
  1084. punpckhbw mm4, mm6 ;*A3B3A2B2 -> mm4
  1085. add eax, 16
  1086. movq mm3, mm4 ;*A3B3A2B2 -> mm3
  1087. punpcklbw mm0, mm6 ;*G3R3G2R2 -> mm0
  1088. pmaddwd mm3, const2_Y0B0B ;*ybB3,ybB2 -> mm3
  1089. movq mm1, mm0 ;*G3R3G2R2 -> mm1
  1090. pmaddwd mm0, const2_YGRGR ;*yrG3+ygR3,ygG2+yrR2 -> mm0
  1091. movq mm5, mm4 ;*A3B3A2B2 -> mm5
  1092. pmaddwd mm4, const2_U0B0B ;*ubB3,ubB2 -> mm4
  1093. movq mm2, mm1 ;*G3R3G2R2 -> mm2
  1094. pmaddwd mm1, const2_UGRGR ;*urG3+ugR3,ugG2+urR2 -> mm1
  1095. movq mm7, mm5 ;*A3B3A2B2 -> mm7
  1096. pmaddwd mm5, const2_V0B0B ;*vbB3,vbB2 -> mm5
  1097. paddd mm0, mm3 ;*Y3Y2 -> mm0
  1098. pmaddwd mm2, const2_VGRGR ;*vgG3+vrR3,vgG2+vrR2 -> mm2
  1099. psrad mm0, FCONVERSION_BITS
  1100. pmaddwd mm7, const2_A ;* 1*A3,1*A2
  1101. paddd mm1, mm4 ;*U3U2 -> mm2
  1102. movq mm6, const_128
  1103. psrad mm1, FCONVERSION_BITS
  1104. packssdw mm0, tempY ;*pack Y3Y2,Y1Y0 -> mm0
  1105. paddd mm2, mm5 ;*V3V2 -> mm2
  1106. psrad mm2, FCONVERSION_BITS
  1107. add ebx, 4
  1108. packssdw mm1, tempU ;*pack U3U2,U1U0 -> mm1
  1109. packssdw mm2, tempV ;*pack V3V2,V1V0 -> mm2
  1110. paddw mm1, mm6 ;add 128
  1111. packssdw mm7, tempA ;*pack A3A2,A1A0 -> mm7
  1112. paddw mm2, mm6 ;add 128
  1113. packuswb mm0, mm0
  1114. add ecx, 4
  1115. packuswb mm1, mm1
  1116. add edx, 4
  1117. movd [ebx-4], mm0
  1118. packuswb mm2, mm2
  1119. movd [ecx-4], mm1
  1120. packuswb mm7, mm7
  1121. movd [edx-4], mm2
  1122. movd [esi-4], mm7
  1123. dec edi
  1124. jnz RGBAtoYUVA
  1125. //JS  The following emms instruction is purposely commented out.
  1126. //emms       // commented out since it is done after the DCT
  1127. } // end of __asm
  1128. } // end of MRGBA2YCbCrA
  1129. void MRGBA2YCbCrALegacy(
  1130. int rows,
  1131. int cols,
  1132. unsigned char *inRGBA,
  1133. unsigned char *outY,
  1134. unsigned char *outU,
  1135. unsigned char *outV,
  1136. unsigned char *outA)
  1137. {
  1138. // ensure proper stack alignment by making global
  1139. // __int64  tempY, tempU, tempV, tempA;
  1140. // written by Dave Shade - Intel Corp.
  1141. // Feb '97
  1142. //
  1143. // This color space conversion routine converts 
  1144. // true color pixels from RGBA to YCbCrA 
  1145. // This routine subtracts the RGB components from 255 before converting them
  1146. // one pass through the loop processes 4 pixels
  1147. // there is no provision for cols not an even multiple of 4
  1148. __asm {
  1149. // initializations
  1150. //DS - IJG will always call with rows=1, so don't multiply
  1151. // mov eax, rows
  1152. // mov ebx, cols
  1153. // mul ebx ;number pixels
  1154. // reorder to take advantage of Pentium v-pipe
  1155. mov edi, cols
  1156. mov eax, inRGBA
  1157. shr edi, 2 ;number of loops = (rows*cols)/4
  1158. mov edx, outV
  1159. mov ecx, outU
  1160. mov esi, outA
  1161. mov ebx, outY
  1162. // top of loop
  1163. RGBAtoYUVALegacy:
  1164. movq mm3, [eax+8] ;load #1 A1B1G1R1A0B0G0R0 -> mm3
  1165. pxor mm6, mm6 ;0 -> mm6
  1166. pxor mm3, const2_Legacy ; subtract MaxJSample FlashPix rev. 1 "thing"
  1167. movq mm4, mm3 ;A1B1G1R1A0B0G0R0 -> mm4
  1168. psrlq mm3, 32 ;00000000A1B1G1R1 -> mm3
  1169. punpcklwd mm4, mm3 ;A1B1A0B0G1R1G0R0 -> mm4
  1170. add esi, 4 ;opportunistically increment pointer
  1171. movq mm0, mm4 ;A1B1A0B0G1R1G0R0 -> mm0
  1172. punpckhbw mm4, mm6 ;A1B1A0B0 -> mm4
  1173. movq mm3, mm4 ;A1B1A0B0 -> mm3
  1174. punpcklbw mm0, mm6 ;G1R1G0R0 -> mm0
  1175. pmaddwd mm3, const2_Y0B0B ;ybB1,ybB0 -> mm3
  1176. movq mm1, mm0 ;G1R1G0R0 -> mm1
  1177. pmaddwd mm0, const2_YGRGR ;yrG1+ygR1,ygG0+yrR0 -> mm0
  1178. movq mm5, mm4 ;A1B1A0B0 -> mm5
  1179. pmaddwd mm4, const2_U0B0B ;ubB1,ubB0 -> mm4
  1180. movq mm2, mm1 ;G1R1G0R0 -> mm2
  1181. pmaddwd mm1, const2_UGRGR ;urG1+ugR1,ugG0+urR0 -> mm1
  1182. movq mm7, mm5 ;A1B1A0B0 -> mm7
  1183. pmaddwd mm5, const2_V0B0B ;vbB1,vbB0 -> mm5
  1184. paddd mm0, mm3 ;Y1Y0 -> mm0
  1185. pmaddwd mm2, const2_VGRGR ;vgG1+vrR1,vgG0+vrR0 -> mm2
  1186. psrad mm0, FCONVERSION_BITS ;32 bit scaled Y1Y0
  1187. psrld mm7, 16 ;shift A1A0 down 
  1188. movq mm3, [eax] ;*load #2 A3B3G3R3A2B2G2R2 -> mm3
  1189. paddd mm1, mm4 ;U1U0 -> mm2
  1190. pxor mm3, const2_Legacy
  1191. psrad mm1, FCONVERSION_BITS ;32 bit scaled U1U0
  1192. movq tempY, mm0 ;write out Y1Y0 in 32 bit format
  1193. paddd mm2, mm5 ;V1V0 -> mm2
  1194. movq mm4, mm3 ;*A3B3G3R3A2B2G2R2 -> mm4
  1195. psrad mm2, FCONVERSION_BITS ;32bit scaled V1V0
  1196. movq tempU, mm1 ;write out U1U0 in 32 bit format
  1197. psrlq mm3, 32 ;*00000000A3B3G3R3 -> mm3
  1198. movq tempV, mm2 ;write out V1V0 in 32 bit format
  1199. punpcklwd mm4, mm3 ;*A3B3A2B2G3R3G2R2 -> mm4
  1200. movq tempA, mm7
  1201. movq mm0, mm4 ;*A3B3A2B2G3R3G2R2 -> mm0
  1202. punpckhbw mm4, mm6 ;*A3B3A2B2 -> mm4
  1203. add eax, 16 ;opportunistically increment pointer
  1204. movq mm3, mm4 ;*A3B3A2B2 -> mm3
  1205. punpcklbw mm0, mm6 ;*G3R3G2R2 -> mm0
  1206. pmaddwd mm3, const2_Y0B0B ;*ybB3,ybB2 -> mm3
  1207. movq mm1, mm0 ;*G3R3G2R2 -> mm1
  1208. pmaddwd mm0, const2_YGRGR ;*yrG3+ygR3,ygG2+yrR2 -> mm0
  1209. movq mm5, mm4 ;*A3B3A2B2 -> mm5
  1210. pmaddwd mm4, const2_U0B0B ;*ubB3,ubB2 -> mm4
  1211. movq mm2, mm1 ;*G3R3G2R2 -> mm2
  1212. pmaddwd mm1, const2_UGRGR ;*urG3+ugR3,ugG2+urR2 -> mm1
  1213. movq mm7, mm5 ;*A3B3A2B2 -> mm7
  1214. pmaddwd mm5, const2_V0B0B ;*vbB3,vbB2 -> mm5
  1215. paddd mm0, mm3 ;*Y3Y2 -> mm0
  1216. pmaddwd mm2, const2_VGRGR ;*vgG3+vrR3,vgG2+vrR2 -> mm2
  1217. psrad mm0, FCONVERSION_BITS ;shift Y3Y2 by 15 bits
  1218. psrld mm7, 16 ;shift the alpha values down
  1219. paddd mm1, mm4 ;*U3U2 -> mm2
  1220. movq mm6, const_128 ; load mm6 with 128
  1221. psrad mm1, FCONVERSION_BITS ;shift U3U2 by 15 bits
  1222. packssdw mm0, tempY ;*pack Y3Y2,Y1Y0 -> mm0
  1223. paddd mm2, mm5 ;*V3V2 -> mm2
  1224. packssdw mm1, tempU ;*pack U3U2,U1U0 -> mm1
  1225. psrad mm2, FCONVERSION_BITS ;shift V3V2 by 15 bits
  1226. add ebx, 4 ;opportunistically increment pointer
  1227. packssdw mm2, tempV ;pack V3V2,V1V0 -> mm2
  1228. paddw mm1, mm6 ;add 128
  1229. packssdw mm7, tempA ;pack A3A2,A1A0 -> mm7
  1230. paddw mm2, mm6 ;add 128
  1231. packuswb mm0, mm0 ;pack Y3Y2Y1Y0 from 16 bit to 8 bit
  1232. add ecx, 4 ;opportunistically increment pointer
  1233. packuswb mm1, mm1 ;pack U3U2U1U0 from 16 bit to 8 bit
  1234. add edx, 4 ;opportunistically increment pointer
  1235. movd [ebx-4], mm0 ;write out Y3Y2Y1Y0
  1236. packuswb mm2, mm2 ;pack V3V2V1V0 from 16 bit to 8 bit
  1237. movd [ecx-4], mm1 ;write out U3U2U1U0
  1238. packuswb mm7, mm7 ;pack A3A2A1A0 from 16 bit to 8 bits
  1239. movd [edx-4], mm2 ;write out V3V2V1V0
  1240. movd [esi-4], mm7 ;write out A3A2A1A0
  1241. dec edi ;subtract 4 from number of pixels
  1242. jnz RGBAtoYUVALegacy
  1243. //JS  The following emms instruction is purposely commented out.
  1244. //emms       // commented out since it is done after the DCT
  1245. } // end of __asm
  1246. } // end of MRGBA2YCbCrALegacy
  1247. #endif // defined (_X86_)