static int
OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc)
{
	static const char module[]="OJPEGDecodeRaw";
	OJPEGState* sp=(OJPEGState*)tif->tif_data;
	uint8* m;
	tmsize_t n;
	uint8* oy;
	uint8* ocb;
	uint8* ocr;
	uint8* p;
	uint32 q;
	uint8* r;
	uint8 sx,sy;
	if (cc%sp->bytes_per_line!=0)
	{
		TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
		return(0);
	}
	assert(cc>0);
	m=buf;
	n=cc;
	do
	{
		if (sp->subsampling_convert_state==0)
		{
			const jpeg_decompress_struct* cinfo = &sp->libjpeg_jpeg_decompress_struct;
			int width = 0;
			int last_col_width = 0;
			int jpeg_bytes;
			int expected_bytes;
			int i;
			if (cinfo->MCUs_per_row == 0)
			{
				sp->error_in_raw_data_decoding = 1;
				return 0;
			}
			for (i = 0; i < cinfo->comps_in_scan; ++i)
			{
				const jpeg_component_info* info = cinfo->cur_comp_info[i];
#if JPEG_LIB_VERSION >= 70
				width += info->MCU_width * info->DCT_h_scaled_size;
				last_col_width += info->last_col_width * info->DCT_h_scaled_size;
#else
				width += info->MCU_width * info->DCT_scaled_size;
				last_col_width += info->last_col_width * info->DCT_scaled_size;
#endif
			}
			jpeg_bytes = (cinfo->MCUs_per_row - 1) * width + last_col_width;
			expected_bytes = sp->subsampling_convert_clinelenout * sp->subsampling_ver * sp->subsampling_hor;
			if (jpeg_bytes != expected_bytes)
			{
				TIFFErrorExt(tif->tif_clientdata,module,"Inconsistent number of MCU in codestream");
				sp->error_in_raw_data_decoding = 1;
				return(0);
			}
			if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
			{
				sp->error_in_raw_data_decoding = 1;
				return(0);
			}
		}
		oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp->subsampling_ver*sp->subsampling_convert_ylinelen;
		ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
		ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
		p=m;
		for (q=0; q<sp->subsampling_convert_clinelenout; q++)
		{
			r=oy;
			for (sy=0; sy<sp->subsampling_ver; sy++)
			{
				for (sx=0; sx<sp->subsampling_hor; sx++)
					*p++=*r++;
				r+=sp->subsampling_convert_ylinelen-sp->subsampling_hor;
			}
			oy+=sp->subsampling_hor;
			*p++=*ocb++;
			*p++=*ocr++;
		}
		sp->subsampling_convert_state++;
		if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
			sp->subsampling_convert_state=0;
		m+=sp->bytes_per_line;
		n-=sp->bytes_per_line;
	} while(n>0);
	return(1);
}