#define EXIT_WARNING 2
#endif
-#define RGB2GRAY(r, g, b) \
- (JSAMPLE)((double)(r) * 0.299 + (double)(g) * 0.587 + \
- (double)(b) * 0.114 + 0.5)
-
#define IsExtRGB(cs) \
(cs == JCS_RGB || (cs >= JCS_EXT_RGB && cs <= JCS_EXT_ARGB))
<tr><td class="paramname">width</td><td>pointer to an integer variable that will receive the width (in pixels) of the uncompressed image</td></tr>
<tr><td class="paramname">align</td><td>row alignment of the image buffer to be returned (must be a power of 2.) For instance, setting this parameter to 4 will cause all rows in the image buffer to be padded to the nearest 32-bit boundary, and setting this parameter to 1 will cause all rows in the image buffer to be unpadded.</td></tr>
<tr><td class="paramname">height</td><td>pointer to an integer variable that will receive the height (in pixels) of the uncompressed image</td></tr>
- <tr><td class="paramname">pixelFormat</td><td>pointer to an integer variable that specifies or will receive the pixel format of the uncompressed image buffer. If <code>*pixelFormat</code> is set to <a class="el" href="group___turbo_j_p_e_g.html#ggac916144e26c3817ac514e64ae5d12e2aa84c1a6cead7952998e2fb895844a21ed">TJPF_UNKNOWN</a> prior to calling this function, then the uncompressed image buffer returned by the function will use the most optimal pixel format for the file type, and <code>*pixelFormat</code> will contain the ID of this pixel format upon successful return from the function. Otherwise, the uncompressed image buffer will use the <a class="el" href="group___turbo_j_p_e_g.html#gac916144e26c3817ac514e64ae5d12e2a">pixel format</a> specified in <code>*pixelFormat</code>, and pixel format conversion will be performed if necessary. If <code>*pixelFormat</code> is set to <a class="el" href="group___turbo_j_p_e_g.html#ggac916144e26c3817ac514e64ae5d12e2aa7f5100ec44c91994e243f1cf55553f8b">TJPF_CMYK</a>, then the RGB or grayscale pixels stored in the file will be converted using a quick & dirty algorithm that is suitable only for testing purposes (proper conversion between CMYK and other formats requires a color management system.)</td></tr>
+ <tr><td class="paramname">pixelFormat</td><td>pointer to an integer variable that specifies or will receive the pixel format of the uncompressed image buffer. The behavior of <a class="el" href="group___turbo_j_p_e_g.html#ga144b981d6b281ecca4cbb4709de75749" title="Load an uncompressed image from disk into memory.">tjLoadImage()</a> will vary depending on the value of <code>*pixelFormat</code> passed to the function:<ul>
+<li><a class="el" href="group___turbo_j_p_e_g.html#ggac916144e26c3817ac514e64ae5d12e2aa84c1a6cead7952998e2fb895844a21ed">TJPF_UNKNOWN</a> : The uncompressed image buffer returned by the function will use the most optimal pixel format for the file type, and <code>*pixelFormat</code> will contain the ID of this pixel format upon successful return from the function.</li>
+<li><a class="el" href="group___turbo_j_p_e_g.html#ggac916144e26c3817ac514e64ae5d12e2aa5431b54b015337705f13118073711a1a">TJPF_GRAY</a> : Only PGM files and 8-bit BMP files with a grayscale colormap can be loaded.</li>
+<li><a class="el" href="group___turbo_j_p_e_g.html#ggac916144e26c3817ac514e64ae5d12e2aa7f5100ec44c91994e243f1cf55553f8b">TJPF_CMYK</a> : The RGB or grayscale pixels stored in the file will be converted using a quick & dirty algorithm that is suitable only for testing purposes (proper conversion between CMYK and other formats requires a color management system.)</li>
+<li>Other <a class="el" href="group___turbo_j_p_e_g.html#gac916144e26c3817ac514e64ae5d12e2a">pixel formats</a> : The uncompressed image buffer will use the specified pixel format, and pixel format conversion will be performed if necessary.</li>
+</ul>
+</td></tr>
<tr><td class="paramname">flags</td><td>the bitwise OR of one or more of the <a class="el" href="group___turbo_j_p_e_g.html#ga72ecf4ebe6eb702d3c6f5ca27455e1ec">flags</a>.</td></tr>
</table>
</dd>
read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
/* Read the colormap from a BMP file */
{
- int i;
+ int i, gray = 1;
switch (mapentrysize) {
case 3:
sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
+ if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
+ sinfo->colormap[1][i] != sinfo->colormap[0][i])
+ gray = 0;
}
break;
case 4:
sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
(void) read_byte(sinfo);
+ if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
+ sinfo->colormap[1][i] != sinfo->colormap[0][i])
+ gray = 0;
}
break;
default:
ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
break;
}
+
+ if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
+ ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
}
if (cinfo->in_color_space == JCS_GRAYSCALE) {
for (col = cinfo->image_width; col > 0; col--) {
t = GETJSAMPLE(*inptr++);
- *outptr++ = RGB2GRAY(colormap[0][t], colormap[1][t], colormap[2][t]);
+ *outptr++ = colormap[0][t];
}
} else if (cinfo->in_color_space == JCS_CMYK) {
for (col = cinfo->image_width; col > 0; col--) {
outptr = source->pub.buffer[0];
if (cinfo->in_color_space == JCS_EXT_BGR) {
MEMCOPY(outptr, inptr, source->row_width);
- } else if (cinfo->in_color_space == JCS_GRAYSCALE) {
- for (col = cinfo->image_width; col > 0; col--) {
- /* can omit GETJSAMPLE() safely */
- JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
- *outptr++ = RGB2GRAY(r, g, b);
- }
} else if (cinfo->in_color_space == JCS_CMYK) {
for (col = cinfo->image_width; col > 0; col--) {
/* can omit GETJSAMPLE() safely */
if (cinfo->in_color_space == JCS_EXT_BGRX ||
cinfo->in_color_space == JCS_EXT_BGRA) {
MEMCOPY(outptr, inptr, source->row_width);
- } else if (cinfo->in_color_space == JCS_GRAYSCALE) {
- for (col = cinfo->image_width; col > 0; col--) {
- /* can omit GETJSAMPLE() safely */
- JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
- *outptr++ = RGB2GRAY(r, g, b);
- }
} else if (cinfo->in_color_space == JCS_CMYK) {
for (col = cinfo->image_width; col > 0; col--) {
/* can omit GETJSAMPLE() safely */
case 8:
if (cinfo->in_color_space == JCS_UNKNOWN)
cinfo->in_color_space = JCS_EXT_RGB;
+ if (IsExtRGB(cinfo->in_color_space))
+ cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
+ else if (cinfo->in_color_space == JCS_GRAYSCALE)
+ cinfo->input_components = 1;
+ else if (cinfo->in_color_space == JCS_CMYK)
+ cinfo->input_components = 4;
+ else
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
row_width = (JDIMENSION) biWidth;
break;
case 24:
if (cinfo->in_color_space == JCS_UNKNOWN)
cinfo->in_color_space = JCS_EXT_BGR;
+ if (IsExtRGB(cinfo->in_color_space))
+ cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
+ else if (cinfo->in_color_space == JCS_CMYK)
+ cinfo->input_components = 4;
+ else
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
row_width = (JDIMENSION) (biWidth * 3);
break;
case 32:
if (cinfo->in_color_space == JCS_UNKNOWN)
cinfo->in_color_space = JCS_EXT_BGRA;
+ if (IsExtRGB(cinfo->in_color_space))
+ cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
+ else if (cinfo->in_color_space == JCS_CMYK)
+ cinfo->input_components = 4;
+ else
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
row_width = (JDIMENSION) (biWidth * 4);
break;
default:
}
}
- if (IsExtRGB(cinfo->in_color_space))
- cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
- else if (cinfo->in_color_space == JCS_GRAYSCALE)
- cinfo->input_components = 1;
- else if (cinfo->in_color_space == JCS_CMYK)
- cinfo->input_components = 4;
- else
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
-
/* Allocate one-row buffer for returned data */
source->pub.buffer = (*cinfo->mem->alloc_sarray)
((j_common_ptr) cinfo, JPOOL_IMAGE,
}
-METHODDEF(JDIMENSION)
-get_text_rgb_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
-/* This version is for reading text-format PPM files with any maxval and
- converting to grayscale */
-{
- ppm_source_ptr source = (ppm_source_ptr) sinfo;
- FILE *infile = source->pub.input_file;
- register JSAMPROW ptr;
- register JSAMPLE *rescale = source->rescale;
- JDIMENSION col;
- unsigned int maxval = source->maxval;
-
- ptr = source->pub.buffer[0];
- if (maxval == MAXJSAMPLE) {
- for (col = cinfo->image_width; col > 0; col--) {
- JSAMPLE r = read_pbm_integer(cinfo, infile, maxval);
- JSAMPLE g = read_pbm_integer(cinfo, infile, maxval);
- JSAMPLE b = read_pbm_integer(cinfo, infile, maxval);
- *ptr++ = RGB2GRAY(r, g, b);
- }
- } else {
- for (col = cinfo->image_width; col > 0; col--) {
- JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
- JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
- JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
- *ptr++ = RGB2GRAY(r, g, b);
- }
- }
- return 1;
-}
-
-
METHODDEF(JDIMENSION)
get_text_rgb_cmyk_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
/* This version is for reading text-format PPM files with any maxval and
}
-METHODDEF(JDIMENSION)
-get_rgb_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
-/* This version is for reading raw-byte-format PPM files with any maxval and
- converting to grayscale */
-{
- ppm_source_ptr source = (ppm_source_ptr) sinfo;
- register JSAMPROW ptr;
- register U_CHAR *bufferptr;
- register JSAMPLE *rescale = source->rescale;
- JDIMENSION col;
- unsigned int maxval = source->maxval;
-
- if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- ptr = source->pub.buffer[0];
- bufferptr = source->iobuffer;
- if (maxval == MAXJSAMPLE) {
- for (col = cinfo->image_width; col > 0; col--) {
- JSAMPLE r = *bufferptr++;
- JSAMPLE g = *bufferptr++;
- JSAMPLE b = *bufferptr++;
- *ptr++ = RGB2GRAY(r, g, b);
- }
- } else {
- for (col = cinfo->image_width; col > 0; col--) {
- JSAMPLE r = rescale[UCH(*bufferptr++)];
- JSAMPLE g = rescale[UCH(*bufferptr++)];
- JSAMPLE b = rescale[UCH(*bufferptr++)];
- *ptr++ = RGB2GRAY(r, g, b);
- }
- }
- return 1;
-}
-
-
METHODDEF(JDIMENSION)
get_rgb_cmyk_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
/* This version is for reading raw-byte-format PPM files with any maxval and
source->pub.get_pixel_rows = get_text_rgb_row;
else if (cinfo->in_color_space == JCS_CMYK)
source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
- else if (cinfo->in_color_space == JCS_GRAYSCALE)
- source->pub.get_pixel_rows = get_text_rgb_gray_row;
else
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
need_iobuffer = FALSE;
source->pub.get_pixel_rows = get_rgb_row;
else if (cinfo->in_color_space == JCS_CMYK)
source->pub.get_pixel_rows = get_rgb_cmyk_row;
- else if (cinfo->in_color_space == JCS_GRAYSCALE)
- source->pub.get_pixel_rows = get_rgb_gray_row;
else
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
}
bailout(); \
}
-#define RGB2GRAY(r, g, b) \
- (unsigned char)((double)(r)*0.299+(double)(g)*0.587+(double)(b)*0.114+0.5)
-
const char *subNameLong[TJ_NUMSAMP]=
{
"4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
unsigned char g=(j*256/height)%256;
unsigned char b=(j*256/height+i*256/width)%256;
memset(&buf[row*pitch+i*ps], 0, ps);
- if(pf==TJPF_GRAY) buf[row*pitch+i*ps]=RGB2GRAY(r, g, b);
+ if(pf==TJPF_GRAY) buf[row*pitch+i*ps]=b;
else if(pf==TJPF_CMYK)
rgb_to_cmyk(r, g, b, &buf[row*pitch+i*ps+0], &buf[row*pitch+i*ps+1],
&buf[row*pitch+i*ps+2], &buf[row*pitch+i*ps+3]);
unsigned char b=(j*256/height+i*256/width)%256;
if(pf==TJPF_GRAY)
{
- if(buf[row*pitch+i*ps]!=RGB2GRAY(r, g, b))
+ if(buf[row*pitch+i*ps]!=b)
return 0;
}
else if(pf==TJPF_CMYK)
&bf);
if(gray2rgb)
{
- unsigned char gray=RGB2GRAY(r, g, b);
- if(rf!=gray || gf!=gray || bf!=gray)
+ if(rf!=b || gf!=b || bf!=b)
return 0;
}
else if(rf!=r || gf!=g || bf!=b) return 0;
{
if(gray2rgb)
{
- unsigned char gray=RGB2GRAY(r, g, b);
- if(buf[row*pitch+i*ps+roffset]!=gray ||
- buf[row*pitch+i*ps+goffset]!=gray ||
- buf[row*pitch+i*ps+boffset]!=gray)
+ if(buf[row*pitch+i*ps+roffset]!=b ||
+ buf[row*pitch+i*ps+goffset]!=b ||
+ buf[row*pitch+i*ps+boffset]!=b)
return 0;
}
else if(buf[row*pitch+i*ps+roffset]!=r ||
if(pf==TJPF_GRAY)
{
- md5ref=!strcasecmp(ext, "ppm")? "bc77dea8eaf006aa187582b301f67e02":
- "2670a3f8cf19d855183c02ccf18d2a35";
+ md5ref=!strcasecmp(ext, "ppm")? "112c682e82ce5de1cca089e20d60000b":
+ "51976530acf75f02beddf5d21149101d";
}
else
{
retval=-1; goto bailout;
}
}
- else if(pf!=TJPF_CMYK)
- {
- tjFree(buf); buf=NULL;
- pf=TJPF_GRAY;
- if((buf=tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
- flags))==NULL)
- _throwtj();
- pitch=PAD(width, align);
- if(!cmpBitmap(buf, width, pitch, height, pf, flags, 0))
- {
- printf("\n Converting %s to grayscale failed\n", filename);
- retval=-1; goto bailout;
- }
- }
unlink(filename);
bailout:
* (in pixels) of the uncompressed image
*
* @param pixelFormat pointer to an integer variable that specifies or will
- * receive the pixel format of the uncompressed image buffer. If
- * <tt>*pixelFormat</tt> is set to @ref TJPF_UNKNOWN prior to calling this
- * function, then the uncompressed image buffer returned by the function will
- * use the most optimal pixel format for the file type, and
+ * receive the pixel format of the uncompressed image buffer. The behavior of
+ * #tjLoadImage() will vary depending on the value of <tt>*pixelFormat</tt>
+ * passed to the function:
+ * - @ref TJPF_UNKNOWN : The uncompressed image buffer returned by the function
+ * will use the most optimal pixel format for the file type, and
* <tt>*pixelFormat</tt> will contain the ID of this pixel format upon
- * successful return from the function. Otherwise, the uncompressed image
- * buffer will use the @ref TJPF "pixel format" specified in
- * <tt>*pixelFormat</tt>, and pixel format conversion will be performed if
- * necessary. If <tt>*pixelFormat</tt> is set to @ref TJPF_CMYK, then the RGB
- * or grayscale pixels stored in the file will be converted using a quick &
- * dirty algorithm that is suitable only for testing purposes (proper
- * conversion between CMYK and other formats requires a color management
- * system.)
+ * successful return from the function.
+ * - @ref TJPF_GRAY : Only PGM files and 8-bit BMP files with a grayscale
+ * colormap can be loaded.
+ * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
+ * converted using a quick & dirty algorithm that is suitable only for testing
+ * purposes (proper conversion between CMYK and other formats requires a color
+ * management system.)
+ * - Other @ref TJPF "pixel formats" : The uncompressed image buffer will use
+ * the specified pixel format, and pixel format conversion will be performed if
+ * necessary.
*
* @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
* "flags".