jccolext.c 4.5 KB

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