jccolext.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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,
  28. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  29. JDIMENSION output_row, 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)
  56. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  57. >> SCALEBITS);
  58. /* Cb */
  59. outptr1[col] = (JSAMPLE)
  60. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  61. >> SCALEBITS);
  62. /* Cr */
  63. outptr2[col] = (JSAMPLE)
  64. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  65. >> SCALEBITS);
  66. }
  67. }
  68. }
  69. /**************** Cases other than RGB -> YCbCr **************/
  70. /*
  71. * Convert some rows of samples to the JPEG colorspace.
  72. * This version handles RGB->grayscale conversion, which is the same
  73. * as the RGB->Y portion of RGB->YCbCr.
  74. * We assume rgb_ycc_start has been called (we only use the Y tables).
  75. */
  76. INLINE
  77. LOCAL(void)
  78. rgb_gray_convert_internal (j_compress_ptr cinfo,
  79. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  80. JDIMENSION output_row, int num_rows)
  81. {
  82. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  83. register int r, g, b;
  84. register JLONG * ctab = cconvert->rgb_ycc_tab;
  85. register JSAMPROW inptr;
  86. register JSAMPROW outptr;
  87. register JDIMENSION col;
  88. JDIMENSION num_cols = cinfo->image_width;
  89. while (--num_rows >= 0) {
  90. inptr = *input_buf++;
  91. outptr = output_buf[0][output_row];
  92. output_row++;
  93. for (col = 0; col < num_cols; col++) {
  94. r = GETJSAMPLE(inptr[RGB_RED]);
  95. g = GETJSAMPLE(inptr[RGB_GREEN]);
  96. b = GETJSAMPLE(inptr[RGB_BLUE]);
  97. inptr += RGB_PIXELSIZE;
  98. /* Y */
  99. outptr[col] = (JSAMPLE)
  100. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  101. >> SCALEBITS);
  102. }
  103. }
  104. }
  105. /*
  106. * Convert some rows of samples to the JPEG colorspace.
  107. * This version handles extended RGB->plain RGB conversion
  108. */
  109. INLINE
  110. LOCAL(void)
  111. rgb_rgb_convert_internal (j_compress_ptr cinfo,
  112. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  113. JDIMENSION output_row, int num_rows)
  114. {
  115. register JSAMPROW inptr;
  116. register JSAMPROW outptr0, outptr1, outptr2;
  117. register JDIMENSION col;
  118. JDIMENSION num_cols = cinfo->image_width;
  119. while (--num_rows >= 0) {
  120. inptr = *input_buf++;
  121. outptr0 = output_buf[0][output_row];
  122. outptr1 = output_buf[1][output_row];
  123. outptr2 = output_buf[2][output_row];
  124. output_row++;
  125. for (col = 0; col < num_cols; col++) {
  126. outptr0[col] = GETJSAMPLE(inptr[RGB_RED]);
  127. outptr1[col] = GETJSAMPLE(inptr[RGB_GREEN]);
  128. outptr2[col] = GETJSAMPLE(inptr[RGB_BLUE]);
  129. inptr += RGB_PIXELSIZE;
  130. }
  131. }
  132. }