more eol-style
git-svn-id: svn://svn.icculus.org/gtkradiant/GtkRadiant/branches/ZeroRadiant.ab@186 8a3a26a2-13c4-0310-b231-cf6edde360e5
This commit is contained in:
@@ -1,402 +1,402 @@
|
||||
/*
|
||||
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
||||
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
#include "bmp.h"
|
||||
|
||||
#include "image.h"
|
||||
|
||||
static void BMPLineNone(FILE *f, char *sline, int pixbytes, int width)
|
||||
{
|
||||
int nbytes, i, k, j;
|
||||
|
||||
switch (pixbytes)
|
||||
{
|
||||
case 1 :
|
||||
nbytes = (width + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fread(sline, width, 1, f);
|
||||
nbytes -= width;
|
||||
|
||||
while (nbytes-- > 0) fgetc(f);
|
||||
return;
|
||||
|
||||
case 3 :
|
||||
nbytes = ((width * 3) + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fread(sline, width, 3, f);
|
||||
nbytes -= width * 3;
|
||||
|
||||
while (nbytes-- > 0) fgetc(f);
|
||||
|
||||
// reorder bgr to rgb
|
||||
for (i = 0, j = 0; i < width; i++, j += 3)
|
||||
{
|
||||
k = sline[j];
|
||||
sline[j] = sline[j+2];
|
||||
sline[j+2] = k;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Error("BMPLineNone failed.");
|
||||
}
|
||||
|
||||
|
||||
static void BMPLineRLE8(FILE *f, char *sline, int pixbytes, int width)
|
||||
{
|
||||
Error("RLE8 not yet supported.");
|
||||
}
|
||||
|
||||
|
||||
static void BMPLineRLE4(FILE *f, char *sline, int pixbytes, int width)
|
||||
{
|
||||
Error("RLE4 not yet supported.");
|
||||
}
|
||||
|
||||
|
||||
static void BMPLine(FILE *f, char *scanline, int pixbytes, int width, int rle)
|
||||
{
|
||||
switch (rle)
|
||||
{
|
||||
case xBI_NONE : BMPLineNone(f, scanline, pixbytes, width); return;
|
||||
case xBI_RLE8 : BMPLineRLE8(f, scanline, pixbytes, width); return;
|
||||
case xBI_RLE4 : BMPLineRLE4(f, scanline, pixbytes, width); return;
|
||||
}
|
||||
|
||||
Error("Unknown compression type.");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
static void PrintHeader(binfo_t *b)
|
||||
{
|
||||
printf("biSize : %ld\n", b->biSize);
|
||||
printf("biWidth : %ld\n", b->biWidth);
|
||||
printf("biHeight : %ld\n", b->biHeight);
|
||||
printf("biPlanes : %d\n", b->biPlanes);
|
||||
printf("biBitCount : %d\n", b->biBitCount);
|
||||
printf("biCompression : %ld\n", b->biCompression);
|
||||
printf("biSizeImage : %ld\n", b->biSizeImage);
|
||||
printf("biXPelsPerMeter: %ld\n", b->biXPelsPerMeter);
|
||||
printf("biYPelsPerMeter: %ld\n", b->biYPelsPerMeter);
|
||||
printf("biClrUsed : %ld\n", b->biClrUsed);
|
||||
printf("biClrImportant : %ld\n", b->biClrImportant);
|
||||
}
|
||||
*/
|
||||
|
||||
// FIXME: calls to Error(const char *, ... ) are dependant on qe3.cpp
|
||||
void LoadBMP(char *filename, bitmap_t *bit)
|
||||
{
|
||||
FILE *f;
|
||||
bmphd_t bhd;
|
||||
binfo_t info;
|
||||
// int pxlsize = 1;
|
||||
int rowbytes, i, pixbytes;
|
||||
char *scanline;
|
||||
|
||||
// open file
|
||||
if ((f = fopen(filename, "rb")) == NULL)
|
||||
{
|
||||
Error("Unable to open file");// %s.", filename);
|
||||
}
|
||||
|
||||
// read in bitmap header
|
||||
if (fread(&bhd, sizeof(bhd), 1, f) != 1)
|
||||
{
|
||||
fclose(f);
|
||||
Error("Unable to read in bitmap header.");
|
||||
}
|
||||
|
||||
// make sure we have a valid bitmap file
|
||||
if (bhd.bfType != BMP_SIGNATURE_WORD)
|
||||
{
|
||||
fclose(f);
|
||||
Error("Invalid BMP file");//: %s", filename);
|
||||
}
|
||||
|
||||
// load in info header
|
||||
if (fread(&info, sizeof(info), 1, f) != 1)
|
||||
{
|
||||
fclose(f);
|
||||
Error("Unable to read bitmap info header.");
|
||||
}
|
||||
|
||||
// make sure this is an info type of bitmap
|
||||
if (info.biSize != sizeof(binfo_t))
|
||||
{
|
||||
fclose(f);
|
||||
Error("We only support the info bitmap type.");
|
||||
}
|
||||
|
||||
// PrintHeader(&info);
|
||||
|
||||
bit->bpp = info.biBitCount;
|
||||
bit->width = info.biWidth;
|
||||
bit->height = info.biHeight;
|
||||
bit->data = NULL;
|
||||
bit->palette = NULL;
|
||||
|
||||
//currently we only read in 8 and 24 bit bmp files
|
||||
if (info.biBitCount == 8) pixbytes = 1;
|
||||
else if (info.biBitCount == 24) pixbytes = 3;
|
||||
else
|
||||
{
|
||||
Error("Only 8BPP and 24BPP supported");
|
||||
//Error("BPP %d not supported.", info.biBitCount);
|
||||
}
|
||||
|
||||
// if this is an eight bit image load palette
|
||||
if (pixbytes == 1)
|
||||
{
|
||||
drgb_t q;
|
||||
|
||||
bit->palette = reinterpret_cast<rgb_t*>(g_malloc(sizeof(rgb_t) * 256));
|
||||
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
if (fread(&q, sizeof(drgb_t), 1, f) != 1)
|
||||
{
|
||||
fclose(f); g_free(bit->palette);
|
||||
Error("Unable to read palette.");
|
||||
}
|
||||
|
||||
bit->palette[i].r = q.red;
|
||||
bit->palette[i].g = q.green;
|
||||
bit->palette[i].b = q.blue;
|
||||
}
|
||||
}
|
||||
|
||||
// position to start of bitmap
|
||||
fseek(f, bhd.bfOffBits, SEEK_SET);
|
||||
|
||||
// create scanline to read data into
|
||||
rowbytes = ((info.biWidth * pixbytes) + 3) / 4;
|
||||
rowbytes *= 4;
|
||||
|
||||
scanline = reinterpret_cast<char*>(g_malloc(rowbytes));
|
||||
|
||||
// alloc space for new bitmap
|
||||
bit->data = reinterpret_cast<unsigned char*>(g_malloc(info.biWidth * pixbytes * info.biHeight));
|
||||
|
||||
// read in image
|
||||
for (i = 0; i < info.biHeight; i++)
|
||||
{
|
||||
BMPLine(f, scanline, pixbytes, info.biWidth, info.biCompression);
|
||||
|
||||
// store line
|
||||
memcpy(&bit->data[info.biWidth * pixbytes * (info.biHeight - i - 1)], scanline, info.biWidth * pixbytes);
|
||||
}
|
||||
|
||||
g_free(scanline);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void BMPEncodeLine(FILE *f, unsigned char *data, int npxls, int pixbytes)
|
||||
{
|
||||
int nbytes, i, j, k;
|
||||
|
||||
switch (pixbytes)
|
||||
{
|
||||
case 1 :
|
||||
nbytes = (npxls + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fwrite(data, npxls, 1, f);
|
||||
nbytes -= npxls;
|
||||
|
||||
while (nbytes-- > 0) fputc(0, f);
|
||||
return;
|
||||
|
||||
case 3 :
|
||||
// reorder rgb to bgr
|
||||
for (i = 0, j = 0; i < npxls; i++, j+= 3)
|
||||
{
|
||||
k = data[j];
|
||||
data[j] = data[j + 2];
|
||||
data[j + 2] = k;
|
||||
}
|
||||
|
||||
nbytes = ((npxls * 3) + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fwrite(data, npxls, 3, f);
|
||||
nbytes -= npxls * 3;
|
||||
|
||||
while (nbytes-- > 0) fputc(0, f);
|
||||
return;
|
||||
}
|
||||
|
||||
Error("BMPEncodeLine Failed.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void WriteBMP(char *filename, bitmap_t *bit)
|
||||
{
|
||||
FILE *f;
|
||||
bmphd_t header;
|
||||
binfo_t info;
|
||||
drgb_t q; // palette that gets written
|
||||
long bmofs;
|
||||
int w, h, i;
|
||||
int pixbytes;
|
||||
|
||||
if (bit->bpp == 8) pixbytes = 1;
|
||||
else if (bit->bpp == 24) pixbytes = 3;
|
||||
|
||||
else
|
||||
{
|
||||
Error("Only 8BPP and 24BPP supported");
|
||||
//Error("BPP %d not supported.", bit->bpp);
|
||||
}
|
||||
|
||||
|
||||
if ((f = fopen(filename, "wb")) == NULL)
|
||||
{
|
||||
Error("Unable to open file");//%s.", filename);
|
||||
}
|
||||
|
||||
// write out an empty header as a place holder
|
||||
if (fwrite(&header, sizeof(header), 1, f) != 1)
|
||||
{
|
||||
Error("Unable to fwrite.");
|
||||
}
|
||||
|
||||
// init and write info header
|
||||
info.biSize = sizeof(binfo_t);
|
||||
info.biWidth = bit->width;
|
||||
info.biHeight = bit->height;
|
||||
info.biPlanes = 1;
|
||||
info.biBitCount = bit->bpp;
|
||||
info.biCompression = xBI_NONE;
|
||||
info.biSizeImage = bit->width * bit->height;
|
||||
info.biXPelsPerMeter = 0;
|
||||
info.biYPelsPerMeter = 0;
|
||||
info.biClrUsed = 256;
|
||||
info.biClrImportant = 256;
|
||||
|
||||
if (fwrite(&info, sizeof(binfo_t), 1, f) != 1)
|
||||
{
|
||||
Error("fwrite failed.");
|
||||
}
|
||||
|
||||
// write out palette if we need to
|
||||
if (bit->bpp == 8)
|
||||
{
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
q.red = bit->palette[i].r;
|
||||
q.green = bit->palette[i].g;
|
||||
q.blue = bit->palette[i].b;
|
||||
|
||||
fwrite(&q, sizeof(q), 1, f);
|
||||
}
|
||||
}
|
||||
|
||||
// save offset to start of bitmap
|
||||
bmofs = ftell(f);
|
||||
|
||||
// output bitmap
|
||||
w = bit->width;
|
||||
h = bit->height;
|
||||
|
||||
for (i = h - 1; i >= 0; i--)
|
||||
{
|
||||
BMPEncodeLine(f, &bit->data[w * pixbytes * i], w, pixbytes);
|
||||
}
|
||||
|
||||
// update and rewrite file header
|
||||
header.bfType = BMP_SIGNATURE_WORD;
|
||||
header.bfSize = ftell(f);
|
||||
header.bfOffBits = bmofs;
|
||||
|
||||
fseek(f, 0L, SEEK_SET);
|
||||
fwrite(&header, sizeof(header), 1, f);
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
void NewBMP(int width, int height, int bpp, bitmap_t *bit)
|
||||
{
|
||||
int pixbytes;
|
||||
|
||||
if (bpp == 8) pixbytes = 1;
|
||||
else if (bpp == 24) pixbytes = 3;
|
||||
|
||||
else
|
||||
{
|
||||
Error("NewBMP: 8 or 24 bit only.");
|
||||
}
|
||||
|
||||
bit->bpp = bpp;
|
||||
bit->width = width;
|
||||
bit->height = height;
|
||||
|
||||
bit->data = reinterpret_cast<unsigned char*>(g_malloc(width * height * pixbytes));
|
||||
|
||||
if (bit->data == NULL)
|
||||
{
|
||||
Error("NewBMP: g_malloc failed.");
|
||||
}
|
||||
|
||||
// see if we need to create a palette
|
||||
if (pixbytes == 1)
|
||||
{
|
||||
bit->palette = (rgb_t *) g_malloc(768);
|
||||
|
||||
if (bit->palette == NULL)
|
||||
{
|
||||
Error("NewBMP: unable to g_malloc palette.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bit->palette = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void FreeBMP(bitmap_t *bitmap)
|
||||
{
|
||||
if (bitmap->palette)
|
||||
{
|
||||
g_free(bitmap->palette);
|
||||
bitmap->palette = NULL;
|
||||
}
|
||||
|
||||
if (bitmap->data)
|
||||
{
|
||||
g_free(bitmap->data);
|
||||
bitmap->data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
||||
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
#include "bmp.h"
|
||||
|
||||
#include "image.h"
|
||||
|
||||
static void BMPLineNone(FILE *f, char *sline, int pixbytes, int width)
|
||||
{
|
||||
int nbytes, i, k, j;
|
||||
|
||||
switch (pixbytes)
|
||||
{
|
||||
case 1 :
|
||||
nbytes = (width + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fread(sline, width, 1, f);
|
||||
nbytes -= width;
|
||||
|
||||
while (nbytes-- > 0) fgetc(f);
|
||||
return;
|
||||
|
||||
case 3 :
|
||||
nbytes = ((width * 3) + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fread(sline, width, 3, f);
|
||||
nbytes -= width * 3;
|
||||
|
||||
while (nbytes-- > 0) fgetc(f);
|
||||
|
||||
// reorder bgr to rgb
|
||||
for (i = 0, j = 0; i < width; i++, j += 3)
|
||||
{
|
||||
k = sline[j];
|
||||
sline[j] = sline[j+2];
|
||||
sline[j+2] = k;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Error("BMPLineNone failed.");
|
||||
}
|
||||
|
||||
|
||||
static void BMPLineRLE8(FILE *f, char *sline, int pixbytes, int width)
|
||||
{
|
||||
Error("RLE8 not yet supported.");
|
||||
}
|
||||
|
||||
|
||||
static void BMPLineRLE4(FILE *f, char *sline, int pixbytes, int width)
|
||||
{
|
||||
Error("RLE4 not yet supported.");
|
||||
}
|
||||
|
||||
|
||||
static void BMPLine(FILE *f, char *scanline, int pixbytes, int width, int rle)
|
||||
{
|
||||
switch (rle)
|
||||
{
|
||||
case xBI_NONE : BMPLineNone(f, scanline, pixbytes, width); return;
|
||||
case xBI_RLE8 : BMPLineRLE8(f, scanline, pixbytes, width); return;
|
||||
case xBI_RLE4 : BMPLineRLE4(f, scanline, pixbytes, width); return;
|
||||
}
|
||||
|
||||
Error("Unknown compression type.");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
static void PrintHeader(binfo_t *b)
|
||||
{
|
||||
printf("biSize : %ld\n", b->biSize);
|
||||
printf("biWidth : %ld\n", b->biWidth);
|
||||
printf("biHeight : %ld\n", b->biHeight);
|
||||
printf("biPlanes : %d\n", b->biPlanes);
|
||||
printf("biBitCount : %d\n", b->biBitCount);
|
||||
printf("biCompression : %ld\n", b->biCompression);
|
||||
printf("biSizeImage : %ld\n", b->biSizeImage);
|
||||
printf("biXPelsPerMeter: %ld\n", b->biXPelsPerMeter);
|
||||
printf("biYPelsPerMeter: %ld\n", b->biYPelsPerMeter);
|
||||
printf("biClrUsed : %ld\n", b->biClrUsed);
|
||||
printf("biClrImportant : %ld\n", b->biClrImportant);
|
||||
}
|
||||
*/
|
||||
|
||||
// FIXME: calls to Error(const char *, ... ) are dependant on qe3.cpp
|
||||
void LoadBMP(char *filename, bitmap_t *bit)
|
||||
{
|
||||
FILE *f;
|
||||
bmphd_t bhd;
|
||||
binfo_t info;
|
||||
// int pxlsize = 1;
|
||||
int rowbytes, i, pixbytes;
|
||||
char *scanline;
|
||||
|
||||
// open file
|
||||
if ((f = fopen(filename, "rb")) == NULL)
|
||||
{
|
||||
Error("Unable to open file");// %s.", filename);
|
||||
}
|
||||
|
||||
// read in bitmap header
|
||||
if (fread(&bhd, sizeof(bhd), 1, f) != 1)
|
||||
{
|
||||
fclose(f);
|
||||
Error("Unable to read in bitmap header.");
|
||||
}
|
||||
|
||||
// make sure we have a valid bitmap file
|
||||
if (bhd.bfType != BMP_SIGNATURE_WORD)
|
||||
{
|
||||
fclose(f);
|
||||
Error("Invalid BMP file");//: %s", filename);
|
||||
}
|
||||
|
||||
// load in info header
|
||||
if (fread(&info, sizeof(info), 1, f) != 1)
|
||||
{
|
||||
fclose(f);
|
||||
Error("Unable to read bitmap info header.");
|
||||
}
|
||||
|
||||
// make sure this is an info type of bitmap
|
||||
if (info.biSize != sizeof(binfo_t))
|
||||
{
|
||||
fclose(f);
|
||||
Error("We only support the info bitmap type.");
|
||||
}
|
||||
|
||||
// PrintHeader(&info);
|
||||
|
||||
bit->bpp = info.biBitCount;
|
||||
bit->width = info.biWidth;
|
||||
bit->height = info.biHeight;
|
||||
bit->data = NULL;
|
||||
bit->palette = NULL;
|
||||
|
||||
//currently we only read in 8 and 24 bit bmp files
|
||||
if (info.biBitCount == 8) pixbytes = 1;
|
||||
else if (info.biBitCount == 24) pixbytes = 3;
|
||||
else
|
||||
{
|
||||
Error("Only 8BPP and 24BPP supported");
|
||||
//Error("BPP %d not supported.", info.biBitCount);
|
||||
}
|
||||
|
||||
// if this is an eight bit image load palette
|
||||
if (pixbytes == 1)
|
||||
{
|
||||
drgb_t q;
|
||||
|
||||
bit->palette = reinterpret_cast<rgb_t*>(g_malloc(sizeof(rgb_t) * 256));
|
||||
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
if (fread(&q, sizeof(drgb_t), 1, f) != 1)
|
||||
{
|
||||
fclose(f); g_free(bit->palette);
|
||||
Error("Unable to read palette.");
|
||||
}
|
||||
|
||||
bit->palette[i].r = q.red;
|
||||
bit->palette[i].g = q.green;
|
||||
bit->palette[i].b = q.blue;
|
||||
}
|
||||
}
|
||||
|
||||
// position to start of bitmap
|
||||
fseek(f, bhd.bfOffBits, SEEK_SET);
|
||||
|
||||
// create scanline to read data into
|
||||
rowbytes = ((info.biWidth * pixbytes) + 3) / 4;
|
||||
rowbytes *= 4;
|
||||
|
||||
scanline = reinterpret_cast<char*>(g_malloc(rowbytes));
|
||||
|
||||
// alloc space for new bitmap
|
||||
bit->data = reinterpret_cast<unsigned char*>(g_malloc(info.biWidth * pixbytes * info.biHeight));
|
||||
|
||||
// read in image
|
||||
for (i = 0; i < info.biHeight; i++)
|
||||
{
|
||||
BMPLine(f, scanline, pixbytes, info.biWidth, info.biCompression);
|
||||
|
||||
// store line
|
||||
memcpy(&bit->data[info.biWidth * pixbytes * (info.biHeight - i - 1)], scanline, info.biWidth * pixbytes);
|
||||
}
|
||||
|
||||
g_free(scanline);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void BMPEncodeLine(FILE *f, unsigned char *data, int npxls, int pixbytes)
|
||||
{
|
||||
int nbytes, i, j, k;
|
||||
|
||||
switch (pixbytes)
|
||||
{
|
||||
case 1 :
|
||||
nbytes = (npxls + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fwrite(data, npxls, 1, f);
|
||||
nbytes -= npxls;
|
||||
|
||||
while (nbytes-- > 0) fputc(0, f);
|
||||
return;
|
||||
|
||||
case 3 :
|
||||
// reorder rgb to bgr
|
||||
for (i = 0, j = 0; i < npxls; i++, j+= 3)
|
||||
{
|
||||
k = data[j];
|
||||
data[j] = data[j + 2];
|
||||
data[j + 2] = k;
|
||||
}
|
||||
|
||||
nbytes = ((npxls * 3) + 3) / 4;
|
||||
nbytes *= 4;
|
||||
|
||||
fwrite(data, npxls, 3, f);
|
||||
nbytes -= npxls * 3;
|
||||
|
||||
while (nbytes-- > 0) fputc(0, f);
|
||||
return;
|
||||
}
|
||||
|
||||
Error("BMPEncodeLine Failed.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void WriteBMP(char *filename, bitmap_t *bit)
|
||||
{
|
||||
FILE *f;
|
||||
bmphd_t header;
|
||||
binfo_t info;
|
||||
drgb_t q; // palette that gets written
|
||||
long bmofs;
|
||||
int w, h, i;
|
||||
int pixbytes;
|
||||
|
||||
if (bit->bpp == 8) pixbytes = 1;
|
||||
else if (bit->bpp == 24) pixbytes = 3;
|
||||
|
||||
else
|
||||
{
|
||||
Error("Only 8BPP and 24BPP supported");
|
||||
//Error("BPP %d not supported.", bit->bpp);
|
||||
}
|
||||
|
||||
|
||||
if ((f = fopen(filename, "wb")) == NULL)
|
||||
{
|
||||
Error("Unable to open file");//%s.", filename);
|
||||
}
|
||||
|
||||
// write out an empty header as a place holder
|
||||
if (fwrite(&header, sizeof(header), 1, f) != 1)
|
||||
{
|
||||
Error("Unable to fwrite.");
|
||||
}
|
||||
|
||||
// init and write info header
|
||||
info.biSize = sizeof(binfo_t);
|
||||
info.biWidth = bit->width;
|
||||
info.biHeight = bit->height;
|
||||
info.biPlanes = 1;
|
||||
info.biBitCount = bit->bpp;
|
||||
info.biCompression = xBI_NONE;
|
||||
info.biSizeImage = bit->width * bit->height;
|
||||
info.biXPelsPerMeter = 0;
|
||||
info.biYPelsPerMeter = 0;
|
||||
info.biClrUsed = 256;
|
||||
info.biClrImportant = 256;
|
||||
|
||||
if (fwrite(&info, sizeof(binfo_t), 1, f) != 1)
|
||||
{
|
||||
Error("fwrite failed.");
|
||||
}
|
||||
|
||||
// write out palette if we need to
|
||||
if (bit->bpp == 8)
|
||||
{
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
q.red = bit->palette[i].r;
|
||||
q.green = bit->palette[i].g;
|
||||
q.blue = bit->palette[i].b;
|
||||
|
||||
fwrite(&q, sizeof(q), 1, f);
|
||||
}
|
||||
}
|
||||
|
||||
// save offset to start of bitmap
|
||||
bmofs = ftell(f);
|
||||
|
||||
// output bitmap
|
||||
w = bit->width;
|
||||
h = bit->height;
|
||||
|
||||
for (i = h - 1; i >= 0; i--)
|
||||
{
|
||||
BMPEncodeLine(f, &bit->data[w * pixbytes * i], w, pixbytes);
|
||||
}
|
||||
|
||||
// update and rewrite file header
|
||||
header.bfType = BMP_SIGNATURE_WORD;
|
||||
header.bfSize = ftell(f);
|
||||
header.bfOffBits = bmofs;
|
||||
|
||||
fseek(f, 0L, SEEK_SET);
|
||||
fwrite(&header, sizeof(header), 1, f);
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
void NewBMP(int width, int height, int bpp, bitmap_t *bit)
|
||||
{
|
||||
int pixbytes;
|
||||
|
||||
if (bpp == 8) pixbytes = 1;
|
||||
else if (bpp == 24) pixbytes = 3;
|
||||
|
||||
else
|
||||
{
|
||||
Error("NewBMP: 8 or 24 bit only.");
|
||||
}
|
||||
|
||||
bit->bpp = bpp;
|
||||
bit->width = width;
|
||||
bit->height = height;
|
||||
|
||||
bit->data = reinterpret_cast<unsigned char*>(g_malloc(width * height * pixbytes));
|
||||
|
||||
if (bit->data == NULL)
|
||||
{
|
||||
Error("NewBMP: g_malloc failed.");
|
||||
}
|
||||
|
||||
// see if we need to create a palette
|
||||
if (pixbytes == 1)
|
||||
{
|
||||
bit->palette = (rgb_t *) g_malloc(768);
|
||||
|
||||
if (bit->palette == NULL)
|
||||
{
|
||||
Error("NewBMP: unable to g_malloc palette.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bit->palette = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void FreeBMP(bitmap_t *bitmap)
|
||||
{
|
||||
if (bitmap->palette)
|
||||
{
|
||||
g_free(bitmap->palette);
|
||||
bitmap->palette = NULL;
|
||||
}
|
||||
|
||||
if (bitmap->data)
|
||||
{
|
||||
g_free(bitmap->data);
|
||||
bitmap->data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,128 +1,128 @@
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
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.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// Image loading plugin
|
||||
//
|
||||
// Leonardo Zide (leo@lokigames.com)
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include "image.h"
|
||||
#include "lbmlib.h"
|
||||
|
||||
// =============================================================================
|
||||
// global tables
|
||||
|
||||
_QERFuncTable_1 g_FuncTable; // Radiant function table
|
||||
_QERFileSystemTable g_FileSystemTable;
|
||||
|
||||
// =============================================================================
|
||||
// SYNAPSE
|
||||
|
||||
CSynapseServer* g_pSynapseServer = NULL;
|
||||
CSynapseClientImage g_SynapseClient;
|
||||
|
||||
static const XMLConfigEntry_t entries[] =
|
||||
{
|
||||
{ VFS_MAJOR, SYN_REQUIRE, sizeof(g_FileSystemTable), &g_FileSystemTable },
|
||||
{ NULL, SYN_UNKNOWN, 0, NULL } };
|
||||
|
||||
extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)
|
||||
{
|
||||
if (strcmp(version, SYNAPSE_VERSION))
|
||||
{
|
||||
Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
|
||||
return NULL;
|
||||
}
|
||||
g_pSynapseServer = pServer;
|
||||
g_pSynapseServer->IncRef();
|
||||
Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
|
||||
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "jpg", sizeof(_QERPlugImageTable));
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "tga", sizeof(_QERPlugImageTable));
|
||||
// NOTE: these two are for md2 support
|
||||
// instead of requesting them systematically, we could request them per-config before enabling Q2 support
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "pcx", sizeof(_QERPlugImageTable));
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "bmp", sizeof(_QERPlugImageTable));
|
||||
g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(_QERFuncTable_1), SYN_REQUIRE, &g_FuncTable);
|
||||
|
||||
if ( !g_SynapseClient.ConfigXML( pServer, NULL, entries ) ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &g_SynapseClient;
|
||||
}
|
||||
|
||||
bool CSynapseClientImage::RequestAPI(APIDescriptor_t *pAPI)
|
||||
{
|
||||
if (!strcmp(pAPI->major_name, "image"))
|
||||
{
|
||||
_QERPlugImageTable* pTable= static_cast<_QERPlugImageTable*>(pAPI->mpTable);
|
||||
if (!strcmp(pAPI->minor_name, "jpg"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadJPG;
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(pAPI->minor_name, "tga"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadImage;
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(pAPI->minor_name, "pcx"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadImage;
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(pAPI->minor_name, "bmp"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadImage;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CSynapseClientImage::OnActivate() {
|
||||
if (!g_FileSystemTable.m_nSize) {
|
||||
Syn_Printf("ERROR: VFS_MAJOR table was not initialized before OnActivate in '%s' - incomplete synapse.config?\n", GetInfo());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#include "version.h"
|
||||
|
||||
const char* CSynapseClientImage::GetInfo()
|
||||
{
|
||||
return "image formats JPG TGA PCX BMP module built " __DATE__ " " RADIANT_VERSION;
|
||||
}
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
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.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// Image loading plugin
|
||||
//
|
||||
// Leonardo Zide (leo@lokigames.com)
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include "image.h"
|
||||
#include "lbmlib.h"
|
||||
|
||||
// =============================================================================
|
||||
// global tables
|
||||
|
||||
_QERFuncTable_1 g_FuncTable; // Radiant function table
|
||||
_QERFileSystemTable g_FileSystemTable;
|
||||
|
||||
// =============================================================================
|
||||
// SYNAPSE
|
||||
|
||||
CSynapseServer* g_pSynapseServer = NULL;
|
||||
CSynapseClientImage g_SynapseClient;
|
||||
|
||||
static const XMLConfigEntry_t entries[] =
|
||||
{
|
||||
{ VFS_MAJOR, SYN_REQUIRE, sizeof(g_FileSystemTable), &g_FileSystemTable },
|
||||
{ NULL, SYN_UNKNOWN, 0, NULL } };
|
||||
|
||||
extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)
|
||||
{
|
||||
if (strcmp(version, SYNAPSE_VERSION))
|
||||
{
|
||||
Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
|
||||
return NULL;
|
||||
}
|
||||
g_pSynapseServer = pServer;
|
||||
g_pSynapseServer->IncRef();
|
||||
Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
|
||||
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "jpg", sizeof(_QERPlugImageTable));
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "tga", sizeof(_QERPlugImageTable));
|
||||
// NOTE: these two are for md2 support
|
||||
// instead of requesting them systematically, we could request them per-config before enabling Q2 support
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "pcx", sizeof(_QERPlugImageTable));
|
||||
g_SynapseClient.AddAPI(IMAGE_MAJOR, "bmp", sizeof(_QERPlugImageTable));
|
||||
g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(_QERFuncTable_1), SYN_REQUIRE, &g_FuncTable);
|
||||
|
||||
if ( !g_SynapseClient.ConfigXML( pServer, NULL, entries ) ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &g_SynapseClient;
|
||||
}
|
||||
|
||||
bool CSynapseClientImage::RequestAPI(APIDescriptor_t *pAPI)
|
||||
{
|
||||
if (!strcmp(pAPI->major_name, "image"))
|
||||
{
|
||||
_QERPlugImageTable* pTable= static_cast<_QERPlugImageTable*>(pAPI->mpTable);
|
||||
if (!strcmp(pAPI->minor_name, "jpg"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadJPG;
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(pAPI->minor_name, "tga"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadImage;
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(pAPI->minor_name, "pcx"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadImage;
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(pAPI->minor_name, "bmp"))
|
||||
{
|
||||
pTable->m_pfnLoadImage = &LoadImage;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CSynapseClientImage::OnActivate() {
|
||||
if (!g_FileSystemTable.m_nSize) {
|
||||
Syn_Printf("ERROR: VFS_MAJOR table was not initialized before OnActivate in '%s' - incomplete synapse.config?\n", GetInfo());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#include "version.h"
|
||||
|
||||
const char* CSynapseClientImage::GetInfo()
|
||||
{
|
||||
return "image formats JPG TGA PCX BMP module built " __DATE__ " " RADIANT_VERSION;
|
||||
}
|
||||
|
||||
@@ -1,411 +1,411 @@
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
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.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// Functions to load JPEG files from a buffer, based on jdatasrc.c
|
||||
//
|
||||
// Leonardo Zide (leo@lokigames.com)
|
||||
//
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
extern "C" {
|
||||
#include "radiant_jpeglib.h"
|
||||
#include "jpeg6/jerror.h"
|
||||
}
|
||||
|
||||
#include "image.h"
|
||||
|
||||
/* Expanded data source object for stdio input */
|
||||
|
||||
typedef struct {
|
||||
struct jpeg_source_mgr pub; /* public fields */
|
||||
|
||||
int src_size;
|
||||
JOCTET * src_buffer;
|
||||
|
||||
JOCTET * buffer; /* start of buffer */
|
||||
boolean start_of_file; /* have we gotten any data yet? */
|
||||
} my_source_mgr;
|
||||
|
||||
typedef my_source_mgr * my_src_ptr;
|
||||
|
||||
#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
|
||||
|
||||
|
||||
/*
|
||||
* Initialize source --- called by jpeg_read_header
|
||||
* before any data is actually read.
|
||||
*/
|
||||
|
||||
static void my_init_source (j_decompress_ptr cinfo)
|
||||
{
|
||||
my_src_ptr src = (my_src_ptr) cinfo->src;
|
||||
|
||||
/* We reset the empty-input-file flag for each image,
|
||||
* but we don't clear the input buffer.
|
||||
* This is correct behavior for reading a series of images from one source.
|
||||
*/
|
||||
src->start_of_file = TRUE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Fill the input buffer --- called whenever buffer is emptied.
|
||||
*
|
||||
* In typical applications, this should read fresh data into the buffer
|
||||
* (ignoring the current state of next_input_byte & bytes_in_buffer),
|
||||
* reset the pointer & count to the start of the buffer, and return TRUE
|
||||
* indicating that the buffer has been reloaded. It is not necessary to
|
||||
* fill the buffer entirely, only to obtain at least one more byte.
|
||||
*
|
||||
* There is no such thing as an EOF return. If the end of the file has been
|
||||
* reached, the routine has a choice of ERREXIT() or inserting fake data into
|
||||
* the buffer. In most cases, generating a warning message and inserting a
|
||||
* fake EOI marker is the best course of action --- this will allow the
|
||||
* decompressor to output however much of the image is there. However,
|
||||
* the resulting error message is misleading if the real problem is an empty
|
||||
* input file, so we handle that case specially.
|
||||
*
|
||||
* In applications that need to be able to suspend compression due to input
|
||||
* not being available yet, a FALSE return indicates that no more data can be
|
||||
* obtained right now, but more may be forthcoming later. In this situation,
|
||||
* the decompressor will return to its caller (with an indication of the
|
||||
* number of scanlines it has read, if any). The application should resume
|
||||
* decompression after it has loaded more data into the input buffer. Note
|
||||
* that there are substantial restrictions on the use of suspension --- see
|
||||
* the documentation.
|
||||
*
|
||||
* When suspending, the decompressor will back up to a convenient restart point
|
||||
* (typically the start of the current MCU). next_input_byte & bytes_in_buffer
|
||||
* indicate where the restart point will be if the current call returns FALSE.
|
||||
* Data beyond this point must be rescanned after resumption, so move it to
|
||||
* the front of the buffer rather than discarding it.
|
||||
*/
|
||||
|
||||
static boolean my_fill_input_buffer (j_decompress_ptr cinfo)
|
||||
{
|
||||
my_src_ptr src = (my_src_ptr) cinfo->src;
|
||||
size_t nbytes;
|
||||
|
||||
if (src->src_size > INPUT_BUF_SIZE)
|
||||
nbytes = INPUT_BUF_SIZE;
|
||||
else
|
||||
nbytes = src->src_size;
|
||||
|
||||
memcpy (src->buffer, src->src_buffer, nbytes);
|
||||
src->src_buffer += nbytes;
|
||||
src->src_size -= nbytes;
|
||||
|
||||
if (nbytes <= 0) {
|
||||
if (src->start_of_file) /* Treat empty input file as fatal error */
|
||||
ERREXIT(cinfo, JERR_INPUT_EMPTY);
|
||||
WARNMS(cinfo, JWRN_JPEG_EOF);
|
||||
/* Insert a fake EOI marker */
|
||||
src->buffer[0] = (JOCTET) 0xFF;
|
||||
src->buffer[1] = (JOCTET) JPEG_EOI;
|
||||
nbytes = 2;
|
||||
}
|
||||
|
||||
src->pub.next_input_byte = src->buffer;
|
||||
src->pub.bytes_in_buffer = nbytes;
|
||||
src->start_of_file = FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Skip data --- used to skip over a potentially large amount of
|
||||
* uninteresting data (such as an APPn marker).
|
||||
*
|
||||
* Writers of suspendable-input applications must note that skip_input_data
|
||||
* is not granted the right to give a suspension return. If the skip extends
|
||||
* beyond the data currently in the buffer, the buffer can be marked empty so
|
||||
* that the next read will cause a fill_input_buffer call that can suspend.
|
||||
* Arranging for additional bytes to be discarded before reloading the input
|
||||
* buffer is the application writer's problem.
|
||||
*/
|
||||
|
||||
static void my_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
|
||||
{
|
||||
my_src_ptr src = (my_src_ptr) cinfo->src;
|
||||
|
||||
/* Just a dumb implementation for now. Could use fseek() except
|
||||
* it doesn't work on pipes. Not clear that being smart is worth
|
||||
* any trouble anyway --- large skips are infrequent.
|
||||
*/
|
||||
if (num_bytes > 0) {
|
||||
while (num_bytes > (long) src->pub.bytes_in_buffer) {
|
||||
num_bytes -= (long) src->pub.bytes_in_buffer;
|
||||
(void) my_fill_input_buffer(cinfo);
|
||||
/* note we assume that fill_input_buffer will never return FALSE,
|
||||
* so suspension need not be handled.
|
||||
*/
|
||||
}
|
||||
src->pub.next_input_byte += (size_t) num_bytes;
|
||||
src->pub.bytes_in_buffer -= (size_t) num_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* An additional method that can be provided by data source modules is the
|
||||
* resync_to_restart method for error recovery in the presence of RST markers.
|
||||
* For the moment, this source module just uses the default resync method
|
||||
* provided by the JPEG library. That method assumes that no backtracking
|
||||
* is possible.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Terminate source --- called by jpeg_finish_decompress
|
||||
* after all data has been read. Often a no-op.
|
||||
*
|
||||
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
|
||||
* application must deal with any cleanup that should happen even
|
||||
* for error exit.
|
||||
*/
|
||||
|
||||
static void my_term_source (j_decompress_ptr cinfo)
|
||||
{
|
||||
/* no work necessary here */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Prepare for input from a stdio stream.
|
||||
* The caller must have already opened the stream, and is responsible
|
||||
* for closing it after finishing decompression.
|
||||
*/
|
||||
|
||||
static void jpeg_buffer_src (j_decompress_ptr cinfo, void* buffer, int bufsize)
|
||||
{
|
||||
my_src_ptr src;
|
||||
|
||||
/* The source object and input buffer are made permanent so that a series
|
||||
* of JPEG images can be read from the same file by calling jpeg_stdio_src
|
||||
* only before the first one. (If we discarded the buffer at the end of
|
||||
* one image, we'd likely lose the start of the next one.)
|
||||
* This makes it unsafe to use this manager and a different source
|
||||
* manager serially with the same JPEG object. Caveat programmer.
|
||||
*/
|
||||
if (cinfo->src == NULL) { /* first time for this JPEG object? */
|
||||
cinfo->src = (struct jpeg_source_mgr *)
|
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
|
||||
sizeof (my_source_mgr));
|
||||
src = (my_src_ptr) cinfo->src;
|
||||
src->buffer = (JOCTET *)
|
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
|
||||
INPUT_BUF_SIZE * sizeof (JOCTET));
|
||||
}
|
||||
|
||||
src = (my_src_ptr) cinfo->src;
|
||||
src->pub.init_source = my_init_source;
|
||||
src->pub.fill_input_buffer = my_fill_input_buffer;
|
||||
src->pub.skip_input_data = my_skip_input_data;
|
||||
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
|
||||
src->pub.term_source = my_term_source;
|
||||
src->src_buffer = (JOCTET *)buffer;
|
||||
src->src_size = bufsize;
|
||||
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
|
||||
src->pub.next_input_byte = NULL; /* until buffer loaded */
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static char errormsg[JMSG_LENGTH_MAX];
|
||||
|
||||
typedef struct my_jpeg_error_mgr
|
||||
{
|
||||
struct jpeg_error_mgr pub; // "public" fields
|
||||
jmp_buf setjmp_buffer; // for return to caller
|
||||
} bt_jpeg_error_mgr;
|
||||
|
||||
static void my_jpeg_error_exit (j_common_ptr cinfo)
|
||||
{
|
||||
my_jpeg_error_mgr* myerr = (bt_jpeg_error_mgr*) cinfo->err;
|
||||
|
||||
(*cinfo->err->format_message) (cinfo, errormsg);
|
||||
|
||||
longjmp (myerr->setjmp_buffer, 1);
|
||||
}
|
||||
|
||||
// stash a scanline
|
||||
static void j_putRGBScanline (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
|
||||
{
|
||||
int offset = row * widthPix * 4;
|
||||
int count;
|
||||
|
||||
for (count = 0; count < widthPix; count++)
|
||||
{
|
||||
unsigned char iRed, iBlu, iGrn;
|
||||
unsigned char *oRed, *oBlu, *oGrn, *oAlp;
|
||||
|
||||
iRed = *(jpegline + count * 3 + 0);
|
||||
iGrn = *(jpegline + count * 3 + 1);
|
||||
iBlu = *(jpegline + count * 3 + 2);
|
||||
|
||||
oRed = outBuf + offset + count * 4 + 0;
|
||||
oGrn = outBuf + offset + count * 4 + 1;
|
||||
oBlu = outBuf + offset + count * 4 + 2;
|
||||
oAlp = outBuf + offset + count * 4 + 3;
|
||||
|
||||
*oRed = iRed;
|
||||
*oGrn = iGrn;
|
||||
*oBlu = iBlu;
|
||||
*oAlp = 255;
|
||||
}
|
||||
}
|
||||
|
||||
// stash a scanline
|
||||
static void j_putRGBAScanline (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
|
||||
{
|
||||
int offset = row * widthPix * 4;
|
||||
int count;
|
||||
|
||||
for (count = 0; count < widthPix; count++)
|
||||
{
|
||||
unsigned char iRed, iBlu, iGrn, iAlp;
|
||||
unsigned char *oRed, *oBlu, *oGrn, *oAlp;
|
||||
|
||||
iRed = *(jpegline + count * 4 + 0);
|
||||
iGrn = *(jpegline + count * 4 + 1);
|
||||
iBlu = *(jpegline + count * 4 + 2);
|
||||
iAlp = *(jpegline + count * 4 + 3);
|
||||
|
||||
oRed = outBuf + offset + count * 4 + 0;
|
||||
oGrn = outBuf + offset + count * 4 + 1;
|
||||
oBlu = outBuf + offset + count * 4 + 2;
|
||||
oAlp = outBuf + offset + count * 4 + 3;
|
||||
|
||||
*oRed = iRed;
|
||||
*oGrn = iGrn;
|
||||
*oBlu = iBlu;
|
||||
// ydnar: see bug 900
|
||||
*oAlp = 255; //% iAlp;
|
||||
}
|
||||
}
|
||||
|
||||
// stash a gray scanline
|
||||
static void j_putGrayScanlineToRGB (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
|
||||
{
|
||||
int offset = row * widthPix * 4;
|
||||
int count;
|
||||
|
||||
for (count = 0; count < widthPix; count++)
|
||||
{
|
||||
unsigned char iGray;
|
||||
unsigned char *oRed, *oBlu, *oGrn, *oAlp;
|
||||
|
||||
// get our grayscale value
|
||||
iGray = *(jpegline + count);
|
||||
|
||||
oRed = outBuf + offset + count * 4;
|
||||
oGrn = outBuf + offset + count * 4 + 1;
|
||||
oBlu = outBuf + offset + count * 4 + 2;
|
||||
oAlp = outBuf + offset + count * 4 + 3;
|
||||
|
||||
*oRed = iGray;
|
||||
*oGrn = iGray;
|
||||
*oBlu = iGray;
|
||||
*oAlp = 255;
|
||||
}
|
||||
}
|
||||
|
||||
static int _LoadJPGBuff (void *src_buffer, int src_size, unsigned char **pic, int *width, int *height)
|
||||
{
|
||||
struct jpeg_decompress_struct cinfo;
|
||||
struct my_jpeg_error_mgr jerr;
|
||||
JSAMPARRAY buffer;
|
||||
int row_stride, size;
|
||||
|
||||
cinfo.err = jpeg_std_error (&jerr.pub);
|
||||
jerr.pub.error_exit = my_jpeg_error_exit;
|
||||
|
||||
if (setjmp (jerr.setjmp_buffer))
|
||||
{
|
||||
*pic = (unsigned char*)errormsg;
|
||||
jpeg_destroy_decompress (&cinfo);
|
||||
return -1;
|
||||
}
|
||||
|
||||
jpeg_create_decompress (&cinfo);
|
||||
jpeg_buffer_src (&cinfo, src_buffer, src_size);
|
||||
jpeg_read_header (&cinfo, TRUE);
|
||||
jpeg_start_decompress (&cinfo);
|
||||
|
||||
row_stride = cinfo.output_width * cinfo.output_components;
|
||||
|
||||
size = cinfo.output_width * cinfo.output_height * 4;
|
||||
*width = cinfo.output_width;
|
||||
*height = cinfo.output_height;
|
||||
*pic = (unsigned char*) (g_malloc (size+1));
|
||||
memset (*pic, 0, size+1);
|
||||
|
||||
buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
|
||||
|
||||
while (cinfo.output_scanline < cinfo.output_height)
|
||||
{
|
||||
jpeg_read_scanlines (&cinfo, buffer, 1);
|
||||
|
||||
if (cinfo.out_color_components == 4)
|
||||
j_putRGBAScanline (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
|
||||
else if (cinfo.out_color_components == 3)
|
||||
j_putRGBScanline (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
|
||||
else if (cinfo.out_color_components == 1)
|
||||
j_putGrayScanlineToRGB (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
|
||||
}
|
||||
|
||||
jpeg_finish_decompress (&cinfo);
|
||||
jpeg_destroy_decompress (&cinfo);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LoadJPG (const char *filename, unsigned char **pic, int *width, int *height)
|
||||
{
|
||||
unsigned char *fbuffer = NULL;
|
||||
int nLen = vfsLoadFile ((char *)filename, (void **)&fbuffer, 0 );
|
||||
if (nLen == -1)
|
||||
return;
|
||||
|
||||
if (_LoadJPGBuff (fbuffer, nLen, pic, width, height) != 0)
|
||||
{
|
||||
g_FuncTable.m_pfnSysPrintf( "WARNING: JPEG library failed to load %s because %s\n", filename, *pic );
|
||||
*pic = NULL;
|
||||
}
|
||||
|
||||
vfsFreeFile (fbuffer);
|
||||
}
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
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.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// Functions to load JPEG files from a buffer, based on jdatasrc.c
|
||||
//
|
||||
// Leonardo Zide (leo@lokigames.com)
|
||||
//
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
extern "C" {
|
||||
#include "radiant_jpeglib.h"
|
||||
#include "jpeg6/jerror.h"
|
||||
}
|
||||
|
||||
#include "image.h"
|
||||
|
||||
/* Expanded data source object for stdio input */
|
||||
|
||||
typedef struct {
|
||||
struct jpeg_source_mgr pub; /* public fields */
|
||||
|
||||
int src_size;
|
||||
JOCTET * src_buffer;
|
||||
|
||||
JOCTET * buffer; /* start of buffer */
|
||||
boolean start_of_file; /* have we gotten any data yet? */
|
||||
} my_source_mgr;
|
||||
|
||||
typedef my_source_mgr * my_src_ptr;
|
||||
|
||||
#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
|
||||
|
||||
|
||||
/*
|
||||
* Initialize source --- called by jpeg_read_header
|
||||
* before any data is actually read.
|
||||
*/
|
||||
|
||||
static void my_init_source (j_decompress_ptr cinfo)
|
||||
{
|
||||
my_src_ptr src = (my_src_ptr) cinfo->src;
|
||||
|
||||
/* We reset the empty-input-file flag for each image,
|
||||
* but we don't clear the input buffer.
|
||||
* This is correct behavior for reading a series of images from one source.
|
||||
*/
|
||||
src->start_of_file = TRUE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Fill the input buffer --- called whenever buffer is emptied.
|
||||
*
|
||||
* In typical applications, this should read fresh data into the buffer
|
||||
* (ignoring the current state of next_input_byte & bytes_in_buffer),
|
||||
* reset the pointer & count to the start of the buffer, and return TRUE
|
||||
* indicating that the buffer has been reloaded. It is not necessary to
|
||||
* fill the buffer entirely, only to obtain at least one more byte.
|
||||
*
|
||||
* There is no such thing as an EOF return. If the end of the file has been
|
||||
* reached, the routine has a choice of ERREXIT() or inserting fake data into
|
||||
* the buffer. In most cases, generating a warning message and inserting a
|
||||
* fake EOI marker is the best course of action --- this will allow the
|
||||
* decompressor to output however much of the image is there. However,
|
||||
* the resulting error message is misleading if the real problem is an empty
|
||||
* input file, so we handle that case specially.
|
||||
*
|
||||
* In applications that need to be able to suspend compression due to input
|
||||
* not being available yet, a FALSE return indicates that no more data can be
|
||||
* obtained right now, but more may be forthcoming later. In this situation,
|
||||
* the decompressor will return to its caller (with an indication of the
|
||||
* number of scanlines it has read, if any). The application should resume
|
||||
* decompression after it has loaded more data into the input buffer. Note
|
||||
* that there are substantial restrictions on the use of suspension --- see
|
||||
* the documentation.
|
||||
*
|
||||
* When suspending, the decompressor will back up to a convenient restart point
|
||||
* (typically the start of the current MCU). next_input_byte & bytes_in_buffer
|
||||
* indicate where the restart point will be if the current call returns FALSE.
|
||||
* Data beyond this point must be rescanned after resumption, so move it to
|
||||
* the front of the buffer rather than discarding it.
|
||||
*/
|
||||
|
||||
static boolean my_fill_input_buffer (j_decompress_ptr cinfo)
|
||||
{
|
||||
my_src_ptr src = (my_src_ptr) cinfo->src;
|
||||
size_t nbytes;
|
||||
|
||||
if (src->src_size > INPUT_BUF_SIZE)
|
||||
nbytes = INPUT_BUF_SIZE;
|
||||
else
|
||||
nbytes = src->src_size;
|
||||
|
||||
memcpy (src->buffer, src->src_buffer, nbytes);
|
||||
src->src_buffer += nbytes;
|
||||
src->src_size -= nbytes;
|
||||
|
||||
if (nbytes <= 0) {
|
||||
if (src->start_of_file) /* Treat empty input file as fatal error */
|
||||
ERREXIT(cinfo, JERR_INPUT_EMPTY);
|
||||
WARNMS(cinfo, JWRN_JPEG_EOF);
|
||||
/* Insert a fake EOI marker */
|
||||
src->buffer[0] = (JOCTET) 0xFF;
|
||||
src->buffer[1] = (JOCTET) JPEG_EOI;
|
||||
nbytes = 2;
|
||||
}
|
||||
|
||||
src->pub.next_input_byte = src->buffer;
|
||||
src->pub.bytes_in_buffer = nbytes;
|
||||
src->start_of_file = FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Skip data --- used to skip over a potentially large amount of
|
||||
* uninteresting data (such as an APPn marker).
|
||||
*
|
||||
* Writers of suspendable-input applications must note that skip_input_data
|
||||
* is not granted the right to give a suspension return. If the skip extends
|
||||
* beyond the data currently in the buffer, the buffer can be marked empty so
|
||||
* that the next read will cause a fill_input_buffer call that can suspend.
|
||||
* Arranging for additional bytes to be discarded before reloading the input
|
||||
* buffer is the application writer's problem.
|
||||
*/
|
||||
|
||||
static void my_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
|
||||
{
|
||||
my_src_ptr src = (my_src_ptr) cinfo->src;
|
||||
|
||||
/* Just a dumb implementation for now. Could use fseek() except
|
||||
* it doesn't work on pipes. Not clear that being smart is worth
|
||||
* any trouble anyway --- large skips are infrequent.
|
||||
*/
|
||||
if (num_bytes > 0) {
|
||||
while (num_bytes > (long) src->pub.bytes_in_buffer) {
|
||||
num_bytes -= (long) src->pub.bytes_in_buffer;
|
||||
(void) my_fill_input_buffer(cinfo);
|
||||
/* note we assume that fill_input_buffer will never return FALSE,
|
||||
* so suspension need not be handled.
|
||||
*/
|
||||
}
|
||||
src->pub.next_input_byte += (size_t) num_bytes;
|
||||
src->pub.bytes_in_buffer -= (size_t) num_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* An additional method that can be provided by data source modules is the
|
||||
* resync_to_restart method for error recovery in the presence of RST markers.
|
||||
* For the moment, this source module just uses the default resync method
|
||||
* provided by the JPEG library. That method assumes that no backtracking
|
||||
* is possible.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Terminate source --- called by jpeg_finish_decompress
|
||||
* after all data has been read. Often a no-op.
|
||||
*
|
||||
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
|
||||
* application must deal with any cleanup that should happen even
|
||||
* for error exit.
|
||||
*/
|
||||
|
||||
static void my_term_source (j_decompress_ptr cinfo)
|
||||
{
|
||||
/* no work necessary here */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Prepare for input from a stdio stream.
|
||||
* The caller must have already opened the stream, and is responsible
|
||||
* for closing it after finishing decompression.
|
||||
*/
|
||||
|
||||
static void jpeg_buffer_src (j_decompress_ptr cinfo, void* buffer, int bufsize)
|
||||
{
|
||||
my_src_ptr src;
|
||||
|
||||
/* The source object and input buffer are made permanent so that a series
|
||||
* of JPEG images can be read from the same file by calling jpeg_stdio_src
|
||||
* only before the first one. (If we discarded the buffer at the end of
|
||||
* one image, we'd likely lose the start of the next one.)
|
||||
* This makes it unsafe to use this manager and a different source
|
||||
* manager serially with the same JPEG object. Caveat programmer.
|
||||
*/
|
||||
if (cinfo->src == NULL) { /* first time for this JPEG object? */
|
||||
cinfo->src = (struct jpeg_source_mgr *)
|
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
|
||||
sizeof (my_source_mgr));
|
||||
src = (my_src_ptr) cinfo->src;
|
||||
src->buffer = (JOCTET *)
|
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
|
||||
INPUT_BUF_SIZE * sizeof (JOCTET));
|
||||
}
|
||||
|
||||
src = (my_src_ptr) cinfo->src;
|
||||
src->pub.init_source = my_init_source;
|
||||
src->pub.fill_input_buffer = my_fill_input_buffer;
|
||||
src->pub.skip_input_data = my_skip_input_data;
|
||||
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
|
||||
src->pub.term_source = my_term_source;
|
||||
src->src_buffer = (JOCTET *)buffer;
|
||||
src->src_size = bufsize;
|
||||
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
|
||||
src->pub.next_input_byte = NULL; /* until buffer loaded */
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
static char errormsg[JMSG_LENGTH_MAX];
|
||||
|
||||
typedef struct my_jpeg_error_mgr
|
||||
{
|
||||
struct jpeg_error_mgr pub; // "public" fields
|
||||
jmp_buf setjmp_buffer; // for return to caller
|
||||
} bt_jpeg_error_mgr;
|
||||
|
||||
static void my_jpeg_error_exit (j_common_ptr cinfo)
|
||||
{
|
||||
my_jpeg_error_mgr* myerr = (bt_jpeg_error_mgr*) cinfo->err;
|
||||
|
||||
(*cinfo->err->format_message) (cinfo, errormsg);
|
||||
|
||||
longjmp (myerr->setjmp_buffer, 1);
|
||||
}
|
||||
|
||||
// stash a scanline
|
||||
static void j_putRGBScanline (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
|
||||
{
|
||||
int offset = row * widthPix * 4;
|
||||
int count;
|
||||
|
||||
for (count = 0; count < widthPix; count++)
|
||||
{
|
||||
unsigned char iRed, iBlu, iGrn;
|
||||
unsigned char *oRed, *oBlu, *oGrn, *oAlp;
|
||||
|
||||
iRed = *(jpegline + count * 3 + 0);
|
||||
iGrn = *(jpegline + count * 3 + 1);
|
||||
iBlu = *(jpegline + count * 3 + 2);
|
||||
|
||||
oRed = outBuf + offset + count * 4 + 0;
|
||||
oGrn = outBuf + offset + count * 4 + 1;
|
||||
oBlu = outBuf + offset + count * 4 + 2;
|
||||
oAlp = outBuf + offset + count * 4 + 3;
|
||||
|
||||
*oRed = iRed;
|
||||
*oGrn = iGrn;
|
||||
*oBlu = iBlu;
|
||||
*oAlp = 255;
|
||||
}
|
||||
}
|
||||
|
||||
// stash a scanline
|
||||
static void j_putRGBAScanline (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
|
||||
{
|
||||
int offset = row * widthPix * 4;
|
||||
int count;
|
||||
|
||||
for (count = 0; count < widthPix; count++)
|
||||
{
|
||||
unsigned char iRed, iBlu, iGrn, iAlp;
|
||||
unsigned char *oRed, *oBlu, *oGrn, *oAlp;
|
||||
|
||||
iRed = *(jpegline + count * 4 + 0);
|
||||
iGrn = *(jpegline + count * 4 + 1);
|
||||
iBlu = *(jpegline + count * 4 + 2);
|
||||
iAlp = *(jpegline + count * 4 + 3);
|
||||
|
||||
oRed = outBuf + offset + count * 4 + 0;
|
||||
oGrn = outBuf + offset + count * 4 + 1;
|
||||
oBlu = outBuf + offset + count * 4 + 2;
|
||||
oAlp = outBuf + offset + count * 4 + 3;
|
||||
|
||||
*oRed = iRed;
|
||||
*oGrn = iGrn;
|
||||
*oBlu = iBlu;
|
||||
// ydnar: see bug 900
|
||||
*oAlp = 255; //% iAlp;
|
||||
}
|
||||
}
|
||||
|
||||
// stash a gray scanline
|
||||
static void j_putGrayScanlineToRGB (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
|
||||
{
|
||||
int offset = row * widthPix * 4;
|
||||
int count;
|
||||
|
||||
for (count = 0; count < widthPix; count++)
|
||||
{
|
||||
unsigned char iGray;
|
||||
unsigned char *oRed, *oBlu, *oGrn, *oAlp;
|
||||
|
||||
// get our grayscale value
|
||||
iGray = *(jpegline + count);
|
||||
|
||||
oRed = outBuf + offset + count * 4;
|
||||
oGrn = outBuf + offset + count * 4 + 1;
|
||||
oBlu = outBuf + offset + count * 4 + 2;
|
||||
oAlp = outBuf + offset + count * 4 + 3;
|
||||
|
||||
*oRed = iGray;
|
||||
*oGrn = iGray;
|
||||
*oBlu = iGray;
|
||||
*oAlp = 255;
|
||||
}
|
||||
}
|
||||
|
||||
static int _LoadJPGBuff (void *src_buffer, int src_size, unsigned char **pic, int *width, int *height)
|
||||
{
|
||||
struct jpeg_decompress_struct cinfo;
|
||||
struct my_jpeg_error_mgr jerr;
|
||||
JSAMPARRAY buffer;
|
||||
int row_stride, size;
|
||||
|
||||
cinfo.err = jpeg_std_error (&jerr.pub);
|
||||
jerr.pub.error_exit = my_jpeg_error_exit;
|
||||
|
||||
if (setjmp (jerr.setjmp_buffer))
|
||||
{
|
||||
*pic = (unsigned char*)errormsg;
|
||||
jpeg_destroy_decompress (&cinfo);
|
||||
return -1;
|
||||
}
|
||||
|
||||
jpeg_create_decompress (&cinfo);
|
||||
jpeg_buffer_src (&cinfo, src_buffer, src_size);
|
||||
jpeg_read_header (&cinfo, TRUE);
|
||||
jpeg_start_decompress (&cinfo);
|
||||
|
||||
row_stride = cinfo.output_width * cinfo.output_components;
|
||||
|
||||
size = cinfo.output_width * cinfo.output_height * 4;
|
||||
*width = cinfo.output_width;
|
||||
*height = cinfo.output_height;
|
||||
*pic = (unsigned char*) (g_malloc (size+1));
|
||||
memset (*pic, 0, size+1);
|
||||
|
||||
buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
|
||||
|
||||
while (cinfo.output_scanline < cinfo.output_height)
|
||||
{
|
||||
jpeg_read_scanlines (&cinfo, buffer, 1);
|
||||
|
||||
if (cinfo.out_color_components == 4)
|
||||
j_putRGBAScanline (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
|
||||
else if (cinfo.out_color_components == 3)
|
||||
j_putRGBScanline (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
|
||||
else if (cinfo.out_color_components == 1)
|
||||
j_putGrayScanlineToRGB (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
|
||||
}
|
||||
|
||||
jpeg_finish_decompress (&cinfo);
|
||||
jpeg_destroy_decompress (&cinfo);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LoadJPG (const char *filename, unsigned char **pic, int *width, int *height)
|
||||
{
|
||||
unsigned char *fbuffer = NULL;
|
||||
int nLen = vfsLoadFile ((char *)filename, (void **)&fbuffer, 0 );
|
||||
if (nLen == -1)
|
||||
return;
|
||||
|
||||
if (_LoadJPGBuff (fbuffer, nLen, pic, width, height) != 0)
|
||||
{
|
||||
g_FuncTable.m_pfnSysPrintf( "WARNING: JPEG library failed to load %s because %s\n", filename, *pic );
|
||||
*pic = NULL;
|
||||
}
|
||||
|
||||
vfsFreeFile (fbuffer);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user