jccolext.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * jccolext.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1996, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2009-2012, D. R. Commander.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains input colorspace conversion routines.
  11. */
  12. /* This file is included by jccolor.c */
  13. /*
  14. * Convert some rows of samples to the JPEG colorspace.
  15. *
  16. * Note that we change from the application's interleaved-pixel format
  17. * to our internal noninterleaved, one-plane-per-component format.
  18. * The input buffer is therefore three times as wide as the output buffer.
  19. *
  20. * A starting row offset is provided only for the output buffer. The caller
  21. * can easily adjust the passed input_buf value to accommodate any row
  22. * offset required on that side.
  23. */
  24. INLINE
  25. LOCAL(void)
  26. rgb_ycc_convert_internal (j_compress_ptr cinfo,
  27. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  28. JDIMENSION output_row, int num_rows)
  29. {
  30. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  31. register int r, g, b;
  32. register INT32 * ctab = cconvert->rgb_ycc_tab;
  33. register JSAMPROW inptr;
  34. register JSAMPROW outptr0, outptr1, outptr2;
  35. register JDIMENSION col;
  36. JDIMENSION num_cols = cinfo->image_width;
  37. while (--num_rows >= 0) {
  38. inptr = *input_buf++;
  39. outptr0 = output_buf[0][output_row];
  40. outptr1 = output_buf[1][output_row];
  41. outptr2 = output_buf[2][output_row];
  42. output_row++;
  43. for (col = 0; col < num_cols; col++) {
  44. r = GETJSAMPLE(inptr[RGB_RED]);
  45. g = GETJSAMPLE(inptr[RGB_GREEN]);
  46. b = GETJSAMPLE(inptr[RGB_BLUE]);
  47. inptr += RGB_PIXELSIZE;
  48. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  49. * must be too; we do not need an explicit range-limiting operation.
  50. * Hence the value being shifted is never negative, and we don't
  51. * need the general RIGHT_SHIFT macro.
  52. */
  53. /* Y */
  54. outptr0[col] = (JSAMPLE)
  55. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  56. >> SCALEBITS);
  57. /* Cb */
  58. outptr1[col] = (JSAMPLE)
  59. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  60. >> SCALEBITS);
  61. /* Cr */
  62. outptr2[col] = (JSAMPLE)
  63. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  64. >> SCALEBITS);
  65. }
  66. }
  67. }
  68. /**************** Cases other than RGB -> YCbCr **************/
  69. /*
  70. * Convert some rows of samples to the JPEG colorspace.
  71. * This version handles RGB->grayscale conversion, which is the same
  72. * as the RGB->Y portion of RGB->YCbCr.
  73. * We assume rgb_ycc_start has been called (we only use the Y tables).
  74. */
  75. INLINE
  76. LOCAL(void)
  77. rgb_gray_convert_internal (j_compress_ptr cinfo,
  78. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  79. JDIMENSION output_row, int num_rows)
  80. {
  81. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  82. register int r, g, b;
  83. register INT32 * ctab = cconvert->rgb_ycc_tab;
  84. register JSAMPROW inptr;
  85. register JSAMPROW outptr;
  86. register JDIMENSION col;
  87. JDIMENSION num_cols = cinfo->image_width;
  88. while (--num_rows >= 0) {
  89. inptr = *input_buf++;
  90. outptr = output_buf[0][output_row];
  91. output_row++;
  92. for (col = 0; col < num_cols; col++) {
  93. r = GETJSAMPLE(inptr[RGB_RED]);
  94. g = GETJSAMPLE(inptr[RGB_GREEN]);
  95. b = GETJSAMPLE(inptr[RGB_BLUE]);
  96. inptr += RGB_PIXELSIZE;
  97. /* Y */
  98. outptr[col] = (JSAMPLE)
  99. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  100. >> SCALEBITS);
  101. }
  102. }
  103. }
  104. /*
  105. * Convert some rows of samples to the JPEG colorspace.
  106. * This version handles extended RGB->plain RGB conversion
  107. */
  108. INLINE
  109. LOCAL(void)
  110. rgb_rgb_convert_internal (j_compress_ptr cinfo,
  111. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  112. JDIMENSION output_row, int num_rows)
  113. {
  114. register JSAMPROW inptr;
  115. register JSAMPROW outptr0, outptr1, outptr2;
  116. register JDIMENSION col;
  117. JDIMENSION num_cols = cinfo->image_width;
  118. while (--num_rows >= 0) {
  119. inptr = *input_buf++;
  120. outptr0 = output_buf[0][output_row];
  121. outptr1 = output_buf[1][output_row];
  122. outptr2 = output_buf[2][output_row];
  123. output_row++;
  124. for (col = 0; col < num_cols; col++) {
  125. outptr0[col] = GETJSAMPLE(inptr[RGB_RED]);
  126. outptr1[col] = GETJSAMPLE(inptr[RGB_GREEN]);
  127. outptr2[col] = GETJSAMPLE(inptr[RGB_BLUE]);
  128. inptr += RGB_PIXELSIZE;
  129. }
  130. }
  131. }