2006-08-20 12:55:46 +00:00
|
|
|
/*
|
|
|
|
** jpegtexture.cpp
|
|
|
|
** Texture class for JPEG images
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
2007-12-20 04:36:43 +00:00
|
|
|
** Copyright 2006-2007 Randy Heit
|
2006-08-20 12:55:46 +00:00
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2011-07-06 08:50:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <jpeglib.h>
|
|
|
|
}
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
#include "doomtype.h"
|
|
|
|
#include "files.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "v_text.h"
|
2008-04-15 18:05:39 +00:00
|
|
|
#include "bitmap.h"
|
2011-07-05 10:02:38 +00:00
|
|
|
#include "v_video.h"
|
|
|
|
#include "textures/textures.h"
|
2006-08-20 12:55:46 +00:00
|
|
|
|
2011-07-06 08:50:15 +00:00
|
|
|
|
|
|
|
struct FLumpSourceMgr : public jpeg_source_mgr
|
|
|
|
{
|
|
|
|
FileReader *Lump;
|
|
|
|
JOCTET Buffer[4096];
|
|
|
|
bool StartOfFile;
|
|
|
|
|
|
|
|
FLumpSourceMgr (FileReader *lump, j_decompress_ptr cinfo);
|
|
|
|
static void InitSource (j_decompress_ptr cinfo);
|
|
|
|
static boolean FillInputBuffer (j_decompress_ptr cinfo);
|
|
|
|
static void SkipInputData (j_decompress_ptr cinfo, long num_bytes);
|
|
|
|
static void TermSource (j_decompress_ptr cinfo);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void FLumpSourceMgr::InitSource (j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
((FLumpSourceMgr *)(cinfo->src))->StartOfFile = true;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
boolean FLumpSourceMgr::FillInputBuffer (j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
FLumpSourceMgr *me = (FLumpSourceMgr *)(cinfo->src);
|
|
|
|
long nbytes = me->Lump->Read (me->Buffer, sizeof(me->Buffer));
|
|
|
|
|
|
|
|
if (nbytes <= 0)
|
|
|
|
{
|
|
|
|
me->Buffer[0] = (JOCTET)0xFF;
|
|
|
|
me->Buffer[1] = (JOCTET)JPEG_EOI;
|
|
|
|
nbytes = 2;
|
|
|
|
}
|
|
|
|
me->next_input_byte = me->Buffer;
|
|
|
|
me->bytes_in_buffer = nbytes;
|
|
|
|
me->StartOfFile = false;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void FLumpSourceMgr::SkipInputData (j_decompress_ptr cinfo, long num_bytes)
|
|
|
|
{
|
|
|
|
FLumpSourceMgr *me = (FLumpSourceMgr *)(cinfo->src);
|
|
|
|
if (num_bytes <= (long)me->bytes_in_buffer)
|
|
|
|
{
|
|
|
|
me->bytes_in_buffer -= num_bytes;
|
|
|
|
me->next_input_byte += num_bytes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
num_bytes -= (long)me->bytes_in_buffer;
|
|
|
|
me->Lump->Seek (num_bytes, SEEK_CUR);
|
|
|
|
FillInputBuffer (cinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void FLumpSourceMgr::TermSource (j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FLumpSourceMgr::FLumpSourceMgr (FileReader *lump, j_decompress_ptr cinfo)
|
|
|
|
: Lump (lump)
|
|
|
|
{
|
|
|
|
cinfo->src = this;
|
|
|
|
init_source = InitSource;
|
|
|
|
fill_input_buffer = FillInputBuffer;
|
|
|
|
skip_input_data = SkipInputData;
|
|
|
|
resync_to_restart = jpeg_resync_to_restart;
|
|
|
|
term_source = TermSource;
|
|
|
|
bytes_in_buffer = 0;
|
|
|
|
next_input_byte = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void JPEG_ErrorExit (j_common_ptr cinfo)
|
|
|
|
{
|
|
|
|
(*cinfo->err->output_message) (cinfo);
|
|
|
|
throw -1;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void JPEG_OutputMessage (j_common_ptr cinfo)
|
|
|
|
{
|
|
|
|
char buffer[JMSG_LENGTH_MAX];
|
|
|
|
|
|
|
|
(*cinfo->err->format_message) (cinfo, buffer);
|
|
|
|
Printf (TEXTCOLOR_ORANGE "JPEG failure: %s\n", buffer);
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// A JPEG texture
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
class FJPEGTexture : public FTexture
|
2006-08-20 12:55:46 +00:00
|
|
|
{
|
2008-06-01 20:43:02 +00:00
|
|
|
public:
|
|
|
|
FJPEGTexture (int lumpnum, int width, int height);
|
|
|
|
~FJPEGTexture ();
|
|
|
|
|
|
|
|
const BYTE *GetColumn (unsigned int column, const Span **spans_out);
|
|
|
|
const BYTE *GetPixels ();
|
|
|
|
void Unload ();
|
|
|
|
FTextureFormat GetFormat ();
|
2009-09-30 10:41:24 +00:00
|
|
|
int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf = NULL);
|
2008-06-01 20:43:02 +00:00
|
|
|
bool UseBasePalette();
|
|
|
|
|
|
|
|
protected:
|
2006-08-20 12:55:46 +00:00
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
BYTE *Pixels;
|
|
|
|
Span DummySpans[2];
|
|
|
|
|
|
|
|
void MakeTexture ();
|
|
|
|
|
|
|
|
friend class FTexture;
|
|
|
|
};
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FTexture *JPEGTexture_TryCreate(FileReader & data, int lumpnum)
|
2006-08-20 12:55:46 +00:00
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
WORD w[2];
|
|
|
|
BYTE b[4];
|
|
|
|
} first4bytes;
|
|
|
|
|
|
|
|
data.Seek(0, SEEK_SET);
|
2008-06-01 20:43:02 +00:00
|
|
|
if (data.Read(&first4bytes, 4) < 4) return NULL;
|
|
|
|
|
|
|
|
if (first4bytes.b[0] != 0xFF || first4bytes.b[1] != 0xD8 || first4bytes.b[2] != 0xFF)
|
|
|
|
return NULL;
|
2006-08-20 12:55:46 +00:00
|
|
|
|
|
|
|
// Find the SOFn marker to extract the image dimensions,
|
|
|
|
// where n is 0, 1, or 2 (other types are unsupported).
|
|
|
|
while ((unsigned)first4bytes.b[3] - 0xC0 >= 3)
|
|
|
|
{
|
|
|
|
if (data.Read (first4bytes.w, 2) != 2)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
data.Seek (BigShort(first4bytes.w[0]) - 2, SEEK_CUR);
|
|
|
|
if (data.Read (first4bytes.b + 2, 2) != 2 || first4bytes.b[2] != 0xFF)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data.Read (first4bytes.b, 3) != 3)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-20 04:36:43 +00:00
|
|
|
if (BigShort (first4bytes.w[0]) < 5)
|
2006-08-20 12:55:46 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (data.Read (first4bytes.b, 4) != 4)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new FJPEGTexture (lumpnum, BigShort(first4bytes.w[1]), BigShort(first4bytes.w[0]));
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-08-20 12:55:46 +00:00
|
|
|
|
|
|
|
FJPEGTexture::FJPEGTexture (int lumpnum, int width, int height)
|
2008-11-30 12:49:27 +00:00
|
|
|
: FTexture(NULL, lumpnum), Pixels(0)
|
2006-08-20 12:55:46 +00:00
|
|
|
{
|
|
|
|
UseType = TEX_MiscPatch;
|
|
|
|
LeftOffset = 0;
|
|
|
|
TopOffset = 0;
|
|
|
|
bMasked = false;
|
|
|
|
|
|
|
|
Width = width;
|
|
|
|
Height = height;
|
|
|
|
CalcBitSize ();
|
|
|
|
|
|
|
|
DummySpans[0].TopOffset = 0;
|
|
|
|
DummySpans[0].Length = Height;
|
|
|
|
DummySpans[1].TopOffset = 0;
|
|
|
|
DummySpans[1].Length = 0;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
FJPEGTexture::~FJPEGTexture ()
|
|
|
|
{
|
|
|
|
Unload ();
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void FJPEGTexture::Unload ()
|
|
|
|
{
|
|
|
|
if (Pixels != NULL)
|
|
|
|
{
|
|
|
|
delete[] Pixels;
|
|
|
|
Pixels = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2007-12-20 04:36:43 +00:00
|
|
|
FTextureFormat FJPEGTexture::GetFormat()
|
|
|
|
{
|
|
|
|
return TEX_RGB;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
const BYTE *FJPEGTexture::GetColumn (unsigned int column, const Span **spans_out)
|
|
|
|
{
|
|
|
|
if (Pixels == NULL)
|
|
|
|
{
|
|
|
|
MakeTexture ();
|
|
|
|
}
|
|
|
|
if ((unsigned)column >= (unsigned)Width)
|
|
|
|
{
|
|
|
|
if (WidthMask + 1 == Width)
|
|
|
|
{
|
|
|
|
column &= WidthMask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
column %= Width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (spans_out != NULL)
|
|
|
|
{
|
|
|
|
*spans_out = DummySpans;
|
|
|
|
}
|
|
|
|
return Pixels + column*Height;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
const BYTE *FJPEGTexture::GetPixels ()
|
|
|
|
{
|
|
|
|
if (Pixels == NULL)
|
|
|
|
{
|
|
|
|
MakeTexture ();
|
|
|
|
}
|
|
|
|
return Pixels;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void FJPEGTexture::MakeTexture ()
|
|
|
|
{
|
|
|
|
FWadLump lump = Wads.OpenLumpNum (SourceLump);
|
|
|
|
JSAMPLE *buff = NULL;
|
|
|
|
|
|
|
|
jpeg_decompress_struct cinfo;
|
|
|
|
jpeg_error_mgr jerr;
|
|
|
|
|
|
|
|
Pixels = new BYTE[Width * Height];
|
|
|
|
memset (Pixels, 0xBA, Width * Height);
|
|
|
|
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
cinfo.err->output_message = JPEG_OutputMessage;
|
|
|
|
cinfo.err->error_exit = JPEG_ErrorExit;
|
|
|
|
jpeg_create_decompress(&cinfo);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FLumpSourceMgr sourcemgr(&lump, &cinfo);
|
|
|
|
jpeg_read_header(&cinfo, TRUE);
|
|
|
|
if (!((cinfo.out_color_space == JCS_RGB && cinfo.num_components == 3) ||
|
|
|
|
(cinfo.out_color_space == JCS_CMYK && cinfo.num_components == 4) ||
|
|
|
|
(cinfo.out_color_space == JCS_GRAYSCALE && cinfo.num_components == 1)))
|
|
|
|
{
|
2006-08-30 02:38:39 +00:00
|
|
|
Printf (TEXTCOLOR_ORANGE "Unsupported color format\n");
|
2006-08-20 12:55:46 +00:00
|
|
|
throw -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_start_decompress(&cinfo);
|
|
|
|
|
|
|
|
int y = 0;
|
|
|
|
buff = new BYTE[cinfo.output_width * cinfo.output_components];
|
|
|
|
|
|
|
|
while (cinfo.output_scanline < cinfo.output_height)
|
|
|
|
{
|
|
|
|
int num_scanlines = jpeg_read_scanlines(&cinfo, &buff, 1);
|
|
|
|
BYTE *in = buff;
|
|
|
|
BYTE *out = Pixels + y;
|
|
|
|
switch (cinfo.out_color_space)
|
|
|
|
{
|
|
|
|
case JCS_RGB:
|
|
|
|
for (int x = Width; x > 0; --x)
|
|
|
|
{
|
2015-03-08 23:05:02 +00:00
|
|
|
*out = RGB32k.RGB[in[0]>>3][in[1]>>3][in[2]>>3];
|
2006-08-20 12:55:46 +00:00
|
|
|
out += Height;
|
|
|
|
in += 3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JCS_GRAYSCALE:
|
|
|
|
for (int x = Width; x > 0; --x)
|
|
|
|
{
|
|
|
|
*out = GrayMap[in[0]];
|
|
|
|
out += Height;
|
|
|
|
in += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JCS_CMYK:
|
|
|
|
// What are you doing using a CMYK image? :)
|
|
|
|
for (int x = Width; x > 0; --x)
|
|
|
|
{
|
|
|
|
// To be precise, these calculations should use 255, but
|
|
|
|
// 256 is much faster and virtually indistinguishable.
|
|
|
|
int r = in[3] - (((256-in[0])*in[3]) >> 8);
|
|
|
|
int g = in[3] - (((256-in[1])*in[3]) >> 8);
|
|
|
|
int b = in[3] - (((256-in[2])*in[3]) >> 8);
|
2015-03-08 23:05:02 +00:00
|
|
|
*out = RGB32k.RGB[r >> 3][g >> 3][b >> 3];
|
2006-08-20 12:55:46 +00:00
|
|
|
out += Height;
|
|
|
|
in += 4;
|
|
|
|
}
|
|
|
|
break;
|
2006-08-30 02:38:39 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// The other colorspaces were considered above and discarded,
|
|
|
|
// but GCC will complain without a default for them here.
|
|
|
|
break;
|
2006-08-20 12:55:46 +00:00
|
|
|
}
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
jpeg_finish_decompress(&cinfo);
|
|
|
|
jpeg_destroy_decompress(&cinfo);
|
|
|
|
}
|
|
|
|
catch (int)
|
|
|
|
{
|
2014-05-24 23:12:16 +00:00
|
|
|
Printf (TEXTCOLOR_ORANGE " in texture %s\n", Name.GetChars());
|
2006-08-20 12:55:46 +00:00
|
|
|
jpeg_destroy_decompress(&cinfo);
|
|
|
|
}
|
|
|
|
if (buff != NULL)
|
|
|
|
{
|
|
|
|
delete[] buff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-20 18:53:35 +00:00
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// FJPEGTexture::CopyTrueColorPixels
|
|
|
|
//
|
|
|
|
// Preserves the full color information (unlike software mode)
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2009-09-30 10:41:24 +00:00
|
|
|
int FJPEGTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf)
|
2007-12-20 18:53:35 +00:00
|
|
|
{
|
|
|
|
PalEntry pe[256];
|
|
|
|
|
|
|
|
FWadLump lump = Wads.OpenLumpNum (SourceLump);
|
|
|
|
JSAMPLE *buff = NULL;
|
|
|
|
|
|
|
|
jpeg_decompress_struct cinfo;
|
|
|
|
jpeg_error_mgr jerr;
|
|
|
|
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
cinfo.err->output_message = JPEG_OutputMessage;
|
|
|
|
cinfo.err->error_exit = JPEG_ErrorExit;
|
|
|
|
jpeg_create_decompress(&cinfo);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FLumpSourceMgr sourcemgr(&lump, &cinfo);
|
|
|
|
jpeg_read_header(&cinfo, TRUE);
|
|
|
|
|
|
|
|
if (!((cinfo.out_color_space == JCS_RGB && cinfo.num_components == 3) ||
|
|
|
|
(cinfo.out_color_space == JCS_CMYK && cinfo.num_components == 4) ||
|
|
|
|
(cinfo.out_color_space == JCS_GRAYSCALE && cinfo.num_components == 1)))
|
|
|
|
{
|
|
|
|
Printf (TEXTCOLOR_ORANGE "Unsupported color format\n");
|
|
|
|
throw -1;
|
|
|
|
}
|
|
|
|
jpeg_start_decompress(&cinfo);
|
|
|
|
|
|
|
|
int yc = 0;
|
|
|
|
buff = new BYTE[cinfo.output_height * cinfo.output_width * cinfo.output_components];
|
|
|
|
|
|
|
|
|
|
|
|
while (cinfo.output_scanline < cinfo.output_height)
|
|
|
|
{
|
|
|
|
BYTE * ptr = buff + cinfo.output_width * cinfo.output_components * yc;
|
|
|
|
jpeg_read_scanlines(&cinfo, &ptr, 1);
|
|
|
|
yc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cinfo.out_color_space)
|
|
|
|
{
|
|
|
|
case JCS_RGB:
|
2009-09-30 10:41:24 +00:00
|
|
|
bmp->CopyPixelDataRGB(x, y, buff, cinfo.output_width, cinfo.output_height,
|
2008-04-16 18:17:20 +00:00
|
|
|
3, cinfo.output_width * cinfo.output_components, rotate, CF_RGB, inf);
|
2007-12-20 18:53:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JCS_GRAYSCALE:
|
2008-04-22 18:48:30 +00:00
|
|
|
for(int i=0;i<256;i++) pe[i]=PalEntry(255,i,i,i); // default to a gray map
|
2009-09-30 10:41:24 +00:00
|
|
|
bmp->CopyPixelData(x, y, buff, cinfo.output_width, cinfo.output_height,
|
2008-04-16 18:17:20 +00:00
|
|
|
1, cinfo.output_width, rotate, pe, inf);
|
2007-12-20 18:53:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JCS_CMYK:
|
2009-09-30 10:41:24 +00:00
|
|
|
bmp->CopyPixelDataRGB(x, y, buff, cinfo.output_width, cinfo.output_height,
|
2008-04-16 18:17:20 +00:00
|
|
|
4, cinfo.output_width * cinfo.output_components, rotate, CF_CMYK, inf);
|
2007-12-20 18:53:35 +00:00
|
|
|
break;
|
2007-12-26 05:03:14 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2007-12-20 18:53:35 +00:00
|
|
|
}
|
|
|
|
jpeg_finish_decompress(&cinfo);
|
|
|
|
}
|
|
|
|
catch(int)
|
|
|
|
{
|
2014-05-24 23:12:16 +00:00
|
|
|
Printf (TEXTCOLOR_ORANGE " in JPEG texture %s\n", Name.GetChars());
|
2007-12-20 18:53:35 +00:00
|
|
|
}
|
|
|
|
jpeg_destroy_decompress(&cinfo);
|
|
|
|
if (buff != NULL) delete [] buff;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
bool FJPEGTexture::UseBasePalette()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|