Fixed: Remove dependency on Pic (NeL manages TGA and PNG itself, reused code by Kaetemi)

This commit is contained in:
kervala 2016-01-06 12:31:13 +01:00
parent ba81d0ad85
commit 9a10f0f4f6
10 changed files with 37 additions and 1597 deletions

View file

@ -24,6 +24,37 @@ using namespace NLMISC;
extern CTileBank tileBankBrowser; extern CTileBank tileBankBrowser;
static const char* comp[]={"Red", "Green", "Blue", "Alpha", ""}; static const char* comp[]={"Red", "Green", "Blue", "Alpha", ""};
static bool loadPic(const string &path, std::vector<NLMISC::CBGRA> &tampon, uint &width, uint &height)
{
try
{
NLMISC::CIFile file;
if (file.open(path.c_str()))
{
NLMISC::CBitmap bitmap;
bitmap.load(file);
width = bitmap.getWidth();
height = bitmap.getHeight();
tampon.resize(width * height);
bitmap.convertToType(NLMISC::CBitmap::RGBA);
for (uint y = 0; y < height; ++y)
{
for (uint x = 0; x < width; ++x)
{
NLMISC::CRGBA c = bitmap.getPixelColor(x, y, 0);
c.R = (c.R * c.A) / 255;
c.G = (c.G * c.A) / 255;
c.B = (c.B * c.A) / 255;
tampon[(y * width) + x] = c;
}
}
return true;
}
}
catch (NLMISC::Exception& ) { }
return false;
}
bool RemovePath (std::string& path, const char* absolutePathToRemplace); bool RemovePath (std::string& path, const char* absolutePathToRemplace);
// Rotate a buffer // Rotate a buffer
@ -55,7 +86,7 @@ int loadPixmapBuffer(const std::string& path, std::vector<NLMISC::CBGRA>& Buffer
{ {
uint Width; uint Width;
uint Height; uint Height;
if (PIC_LoadPic(path, Buffer, Width, Height)) if (loadPic(path, Buffer, Width, Height))
{ {
while (rot) while (rot)
{ {
@ -268,7 +299,7 @@ bool TileList::setTile128 (int tile, const std::string& name, NL3D::CTile::TBitm
vector<NLMISC::CBGRA> tampon; vector<NLMISC::CBGRA> tampon;
uint Width; uint Width;
uint Height; uint Height;
if (!PIC_LoadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height)) if (!loadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height))
{ {
return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap."), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No)); return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap."), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No));
} }
@ -333,7 +364,7 @@ bool TileList::setTile256 (int tile, const std::string& name, NL3D::CTile::TBitm
vector<NLMISC::CBGRA> tampon; vector<NLMISC::CBGRA> tampon;
uint Width; uint Width;
uint Height; uint Height;
if (!PIC_LoadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height)) if (!loadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height))
{ {
return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap."), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) ); return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap."), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) );
@ -399,7 +430,7 @@ bool TileList::setTileTransition (int tile, const std::string& name, NL3D::CTile
vector<NLMISC::CBGRA> tampon; vector<NLMISC::CBGRA> tampon;
uint Width; uint Width;
uint Height; uint Height;
if (!PIC_LoadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height)) if (!loadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height))
{ {
return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap."), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) ); return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap."), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) );
} }
@ -463,7 +494,7 @@ bool TileList::setDisplacement (int tile, const std::string& name, NL3D::CTile::
vector<NLMISC::CBGRA> tampon; vector<NLMISC::CBGRA> tampon;
uint Width; uint Width;
uint Height; uint Height;
if (!PIC_LoadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height)) if (!loadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height))
{ {
return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap"), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) ); return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap"), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) );
} }
@ -506,7 +537,7 @@ bool TileList::setTileTransitionAlpha (int tile, const std::string& name, int ro
vector<NLMISC::CBGRA> tampon; vector<NLMISC::CBGRA> tampon;
uint Width; uint Width;
uint Height; uint Height;
if (!PIC_LoadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height)) if (!loadPic(tileBankBrowser.getAbsPath ()+troncated, tampon, Width, Height))
{ {
return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap"), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) ); return ( QMessageBox::Yes == QMessageBox::question( NULL, QObject::tr("Can't load bitmap"), QString( ((tileBankBrowser.getAbsPath ()+troncated)+"\nContinue ?").c_str() ), QMessageBox::Yes | QMessageBox::No) );
} }

View file

@ -1,134 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#if !defined(__APPLE__)
#include <malloc.h>
#endif
#include <string.h>
#include <stdarg.h>
#define PIC_ERRSIZE 256
static size_t PIC_Sys_MEM_Allocated;
static size_t PIC_Sys_MEM_NbAllocs;
#if defined(__APPLE__)
#define _msize malloc_size
#elif defined(__GNUC__)
#define _msize malloc_usable_size
#endif /* __GNUC__ */
/* ---------------------------------------------------------------------------------------------------------------------------------- */
void *Pic_malloc(unsigned long size)
{
void *mem;
mem=malloc(size);
if (mem)
{
PIC_Sys_MEM_Allocated+=size;
PIC_Sys_MEM_NbAllocs++;
}
return(mem);
}
/* ----- */
void *Pic_calloc(size_t count, size_t size)
{
void *mem;
mem=calloc(count,size);
if (mem)
{
PIC_Sys_MEM_Allocated+=(size*count);
PIC_Sys_MEM_NbAllocs++;
}
return(mem);
}
/* ----- */
void Pic_free(void *memblock)
{
size_t size;
size=_msize(memblock);
PIC_Sys_MEM_Allocated-=size;
PIC_Sys_MEM_NbAllocs--;
free(memblock);
}
/* ----- */
size_t Pic__msize(void *memblock)
{
return(_msize(memblock));
}
/* ----- */
size_t PIC_GetMemNbAllocs(void)
{
return(PIC_Sys_MEM_NbAllocs);
}
/* ----- */
size_t PIC_GetMemAllocated(void)
{
return(PIC_Sys_MEM_Allocated);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
static unsigned char PIC_ErrorFlag;
static unsigned char PIC_ErrorString[PIC_ERRSIZE];
static unsigned char PIC_Sys_FnctActive=0;
static void (*PIC_Sys_Fnct)(void);
void Pic_SetError(const char *msg, ...)
{
unsigned char curerr[PIC_ERRSIZE],olderr[PIC_ERRSIZE];
va_list args;
va_start(args,msg);
vsprintf(curerr,msg,args);
va_end(args);
if ( (strlen(curerr)+strlen(PIC_ErrorString))>PIC_ERRSIZE ) return;
if (PIC_ErrorFlag)
{
strcpy(olderr,PIC_ErrorString);
sprintf(PIC_ErrorString,"--- [PIC#%03d] :\n%s",PIC_ErrorFlag,curerr);
strcat(PIC_ErrorString,"\n");
strcat(PIC_ErrorString,olderr);
}
else
{
sprintf(PIC_ErrorString,"--- [PIC#%03d] :\n%s",PIC_ErrorFlag,curerr);
}
PIC_ErrorFlag++;
if (PIC_Sys_FnctActive) PIC_Sys_Fnct();
return;
}
/* ----- */
char* PIC_GetError(void)
{
return(PIC_ErrorString);
}
/* ----- */
unsigned char PIC_Error(void)
{
return(PIC_ErrorFlag);
}
/* ----- */
void PIC_ResetError(void)
{
strcpy(PIC_ErrorString,"");
PIC_ErrorFlag=0;
}
/* ----- */
unsigned char PIC_OnErrorCall( void pFnct(void) )
{
if (pFnct != NULL)
{
PIC_Sys_Fnct=pFnct;
PIC_Sys_FnctActive=1;
}
else
{
PIC_Sys_FnctActive=0;
}
return(1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */

View file

@ -1,215 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pic_private.h"
#include "pic.h"
/* ---------------------------------------------------------------------------------------------------------------------------------- */
#pragma pack(1)
typedef struct BMP_HEADER
{
unsigned short bfType;
unsigned long bfSize;
unsigned short Res1;
unsigned short Res2;
unsigned long bfOffBits;
unsigned long biSize;
unsigned long biWidth;
unsigned long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
} BMP_HEADER;
#pragma pack()
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long Pic_BMP_Write( const char *FileName,
unsigned char *pPal,unsigned char *pDatas,
unsigned long w, unsigned long h, unsigned long d)
{
FILE *file;
BMP_HEADER bmph;
unsigned long slsize;
unsigned char *scanline;
unsigned long i;
long x,y,rest;
unsigned char r,g,b;
file=fopen(FileName,"wb");
if (!file)
{
return(0);
}
memset(&bmph,0,sizeof(BMP_HEADER));
bmph.bfType=19778;
bmph.bfSize=sizeof(BMP_HEADER);
bmph.bfSize+=w*h*d/8;
if (pPal)
{
bmph.bfSize+=(256*4);
}
bmph.bfOffBits=sizeof(BMP_HEADER);
if (pPal)
{
bmph.bfOffBits+=(256*4);
}
bmph.biSize=40;/*sizeof(BMP_HEADER);*/
bmph.biWidth=w;
bmph.biHeight=h;
bmph.biPlanes=1;
bmph.biBitCount=(unsigned short)d;
bmph.biCompression=0;
bmph.biSizeImage=w*h*d/8;
fwrite(&bmph,1,sizeof(BMP_HEADER),file);
if (pPal)
{
for(i=0 ; i<256 ; i++)
{
fwrite(&pPal[i*3+0],1,1,file);
fwrite(&pPal[i*3+1],1,1,file);
fwrite(&pPal[i*3+2],1,1,file);
fwrite(&pPal[i*3+2],1,1,file);
}
}
slsize=w*d/8;
scanline=Pic_calloc(1,slsize);
if (!scanline)
{
Pic_SetError("BMP_Write, not enough memory for scanline");
return(0);
}
for(rest=0 ; ((w*d/8)+rest)%4!=0 ; rest++);
for(y=0 ; y<(long)h ; y++)
{
memcpy(scanline,&pDatas[(h-y-1)*slsize],slsize);
if (d==24)
{
for(x=0 ; x<(long)w ; x++)
{
b=scanline[x*3+0];
g=scanline[x*3+1];
r=scanline[x*3+2];
scanline[x*3+0]=b;
scanline[x*3+1]=g;
scanline[x*3+2]=r;
}
}
fwrite(scanline,1,slsize,file);
if (rest)
{
fwrite(scanline,1,rest,file);
}
}
Pic_free(scanline);
fclose(file);
return(1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long Pic_BMP_Read( const char *FileName,
unsigned char **ppPal, unsigned char **ppDatas,
unsigned long *pWidth, unsigned long *pHeight,
unsigned long *pDepth)
{
FILE *file;
BMP_HEADER bmph;
unsigned char *pPal;
unsigned char *pDatas;
unsigned char *scanline;
long w,h,d;
long i,x,y,rest;
unsigned char r,g,b;
unsigned char pad[4];
pPal=NULL;
pDatas=NULL;
file=fopen(FileName,"rb");
if (!file)
{
Pic_SetError("BMP_Read, unable to open %s",FileName);
return(0);
}
fread(&bmph,1,sizeof(BMP_HEADER),file);
*pWidth=w=bmph.biWidth;
*pHeight=h=bmph.biHeight;
*pDepth=d=bmph.biBitCount;
if (d!=8 && d!=24)
{
Pic_SetError("BMP_Read, number of bits per pixel unsupported");
return(0);
}
if (*pDepth==8)
{
pPal=Pic_calloc(1,256*3);
if (!pPal)
{
Pic_SetError("BMP_Read, not enough memory for palette");
return(0);
}
for(i=0 ; i<256 ; i++)
{
fread(&pPal[i*3+2],1,1,file);
fread(&pPal[i*3+1],1,1,file);
fread(&pPal[i*3+0],1,1,file);
fread(&pad[0],1,1,file);
}
}
pDatas=Pic_calloc(1,w*h*d/8);
if (!pDatas)
{
if (pPal)
{
Pic_free(pPal);
}
Pic_SetError("BMP_Read, not enough memory for datas");
return(0);
}
scanline=Pic_calloc(1,w*h*d/8);
if (!scanline)
{
if (pPal)
{
Pic_free(pPal);
}
Pic_free(pDatas);
Pic_SetError("BMP_Read, not enough memory for scanline");
return(0);
}
for(rest=0 ; (w+rest)%4!=0 ; rest++);
for(y=0 ; y<h ; y++)
{
fread(scanline,w,d/8,file);
if (d==24)
{
for(x=0 ; x<w ; x++)
{
r=scanline[x*3+0];
g=scanline[x*3+1];
b=scanline[x*3+2];
scanline[x*3+0]=b;
scanline[x*3+1]=g;
scanline[x*3+2]=r;
}
}
memcpy(&pDatas[(h-y-1)*w*d/8],scanline,w*d/8);
fread(pad,rest,d/8,file);
}
fclose(file);
Pic_free(scanline);
*ppPal=pPal;
*ppDatas=pDatas;
return(1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */

View file

@ -1,160 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <string.h>
#include "jpeglib.h"
#include "pic_private.h"
#include "pic.h"
/* ---------------------------------------------------------------------------------------------------------------------------------- */
struct my_error_mgr
{
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
typedef struct my_error_mgr * my_error_ptr;
/* ---------------------------------------------------------------------------------------------------------------------------------- */
static unsigned char error;
/* ---------------------------------------------------------------------------------------------------------------------------------- */
void my_error_exit(j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
error=1;
longjmp(myerr->setjmp_buffer, 1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long Pic_JPG_Read(const char *FileName, unsigned char **ppPal, unsigned char **ppDatas, unsigned long *w, unsigned long *h)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE *file;
JSAMPARRAY buffer;
int row_stride,i;
unsigned char *pDatas,*pPal;
unsigned long ptr;
error=0;
ptr=0;
file=fopen(FileName, "rb");
if (!file)
{
Pic_SetError("JPG_Read, unable to open %s",FileName);
return(0);
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
setjmp(jerr.setjmp_buffer);
if (error)
{
Pic_SetError("JPG_Read, internal decompression error");
jpeg_destroy_decompress(&cinfo);
return(0);
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, file);
(void) jpeg_read_header(&cinfo, TRUE);
*w=cinfo.image_width;
*h=cinfo.image_height;
if (!ppPal)
{
pDatas=Pic_calloc(1,(*w)*(*h)*3);
}
else
{
pDatas=Pic_calloc(1,(*w)*(*h));
pPal=Pic_calloc(1,256*3);
if (!pPal)
{
Pic_SetError("JPG_Read, not enough memory for palette");
return(0);
}
cinfo.desired_number_of_colors = 256;
cinfo.quantize_colors = TRUE;
cinfo.dither_mode = JDITHER_ORDERED;
}
if (!pDatas)
{
Pic_SetError("JPG_Read, not enough memory for pic");
return(0);
}
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height)
{
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(&pDatas[ptr],buffer[0],row_stride);
ptr+=row_stride;
}
*ppDatas=pDatas;
if (ppPal)
{
for(i=0 ; i<256 ; i++)
{
pPal[i*3+0]=cinfo.colormap[2][i];
pPal[i*3+1]=cinfo.colormap[1][i];
pPal[i*3+2]=cinfo.colormap[0][i];
}
*ppPal=pPal;
}
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(file);
return(1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long Pic_JPG_Write(const char *FileName, unsigned long Qual, unsigned char *pDatas, unsigned long w, unsigned long h)
{
struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
FILE *file;
JSAMPROW row_pointer[1];
int row_stride;
error=0;
file=fopen(FileName,"wb");
if (!file)
{
Pic_SetError("JPG_Write, unable to open %s",FileName);
return(0);
}
jpeg_create_compress(&cinfo);
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
setjmp(jerr.setjmp_buffer);
if (error)
{
Pic_SetError("JPG_Write, internal compression error");
jpeg_destroy_compress(&cinfo);
return(0);
}
jpeg_stdio_dest(&cinfo, file);
cinfo.image_width = w;
cinfo.image_height = h;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, Qual, TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = w * 3;
while(cinfo.next_scanline<cinfo.image_height)
{
row_pointer[0] = & pDatas[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(file);
jpeg_destroy_compress(&cinfo);
return(1);
}

View file

@ -1,654 +0,0 @@
#include <string.h>
#include "pic_private.h"
#include "pic.h"
static unsigned long NbPics=0;
static PIC_PICTURE *HeadPic=NULL;
#ifdef __GNUC__
#include <ctype.h>
char *strupr(char *string)
{
char *s;
if (string)
{
for (s = string; *s; ++s)
*s = toupper(*s);
}
return string;
}
#endif /* __GNUC__ */
/* ---------------------------------------------------------------------------------------------------------------------------------- */
static PIC_PICTURE *GetPic(unsigned long id)
{
PIC_PICTURE *pic;
for(pic=HeadPic ; pic ; pic=pic->Next)
{
if (pic->ID==id)
{
return(pic);
}
}
return(NULL);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long PIC_Load(char* FileName, unsigned char Quantize)
{
unsigned char ext[4];
unsigned long type;
unsigned long i,taken,id;
PIC_PICTURE *pic;
unsigned char *pDatas;
unsigned char *pPal;
unsigned long w,h,Depth;
unsigned long ret;
/* --- Init */
ret=0;
type=0;
id=0;
taken=0;
w=0;
h=0;
Depth=0;
pic=NULL;
pDatas=NULL;
pPal=NULL;
/* --- Get 1st available ID */
for(i=1 ; i<=NbPics+1 ; i++)
{
taken=0;
for(pic=HeadPic ; pic ; pic=pic->Next)
{
if (pic->ID==i)
{
taken=1;
break;
}
}
if (!taken)
{
id=i;
break;
}
}
if (!id)
{
Pic_SetError("Load, unable to create ID");
return(0);
}
/* --- Load pic */
if (FileName)
{
ext[0]=FileName[strlen(FileName)-3];
ext[1]=FileName[strlen(FileName)-2];
ext[2]=FileName[strlen(FileName)-1];
ext[3]=0;
strupr(ext);
if ( !strcmp(ext,"JPG") )
{
type=1;
}
else if ( !strcmp(ext,"TGA") )
{
type=2;
}
else if ( !strcmp(ext,"BMP") )
{
type=3;
}
switch(type)
{
/* - JPG */
case 1:
if (!Quantize)
{
Depth=24;
ret=Pic_JPG_Read(FileName,NULL,&pDatas,&w,&h);
}
else
{
Depth=8;
ret=Pic_JPG_Read(FileName,&pPal,&pDatas,&w,&h);
}
if (!ret)
{
Pic_SetError("Load, unable to load JPG file %s",FileName);
return(0);
}
break;
/* - TGA */
case 2:
ret=Pic_TGA_Read(FileName,&pPal,&pDatas,&w,&h,&Depth);
if (!ret)
{
Pic_SetError("Load, unable to load TGA file %s",FileName);
return(0);
}
break;
/* - BMP */
case 3:
ret=Pic_BMP_Read(FileName,&pPal,&pDatas,&w,&h,&Depth);
if (!ret)
{
Pic_SetError("Load, unable to load BMP file %s",FileName);
return(0);
}
break;
/* - Unknown */
default:
Pic_SetError("Load, unknown extension for %s",FileName);
return(0);
}
}
/* --- Create and place new pic struct */
pic=Pic_calloc(1,sizeof(PIC_PICTURE));
if (!pic)
{
Pic_SetError("Load, not enough memory for internal structure");
return(0);
}
pic->Next=HeadPic;
HeadPic=pic;
NbPics++;
pic->ID=id;
pic->pDatas=pDatas;
pic->pPal=pPal;
pic->Width=w;
pic->Height=h;
pic->Depth=Depth;
return(id);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long PIC_Create(unsigned char* pPal, unsigned char* pDatas, unsigned long w, unsigned long h, unsigned long d)
{
unsigned long i,taken,id;
PIC_PICTURE *pic;
/* --- Init */
id=0;
taken=0;
pic=NULL;
/* --- Get 1st available ID */
for(i=1 ; i<=NbPics+1 ; i++)
{
taken=0;
for(pic=HeadPic ; pic ; pic=pic->Next)
{
if (pic->ID==i)
{
taken=1;
break;
}
}
if (!taken)
{
id=i;
break;
}
}
if (!id)
{
Pic_SetError("Create, unable to create ID");
return(0);
}
/* --- Create pic */
if (!pDatas)
{
pDatas=Pic_calloc(1,w*h*d/8);
if (!pDatas)
{
Pic_SetError("Create, not enough memory for datas");
return(0);
}
}
if (d==8)
{
if (!pPal)
{
pPal=Pic_calloc(1,256*3);
if (!pPal)
{
Pic_SetError("Create, not enough memory for palette");
return(0);
}
}
}
else
{
pPal=NULL;
}
/* --- Create and place new pic struct */
pic=Pic_calloc(1,sizeof(PIC_PICTURE));
if (!pic)
{
Pic_SetError("Create, not enough memory for internal structure");
return(0);
}
pic->Next=HeadPic;
HeadPic=pic;
NbPics++;
pic->ID=id;
pic->pDatas=pDatas;
pic->pPal=pPal;
pic->Width=w;
pic->Height=h;
pic->Depth=d;
return(id);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long PIC_GetInfos( unsigned long id,
unsigned char* *ppPal, unsigned char* *ppDatas,
unsigned long *pW, unsigned long *pH, unsigned long *pD)
{
PIC_PICTURE *pic;
pic=GetPic(id);
if (!pic)
{
Pic_SetError("GetInfos, picture internal structure not found");
return(0);
}
if (ppPal)
{
*ppPal=pic->pPal;
}
if (ppDatas)
{
*ppDatas=pic->pDatas;
}
if (pW)
{
*pW=pic->Width;
}
if (pH)
{
*pH=pic->Height;
}
if (pD)
{
*pD=pic->Depth;
}
return(id);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
static char* Conv8To24(unsigned long id)
{
PIC_PICTURE *pic;
unsigned char *buf;
unsigned long i;
pic=GetPic(id);
if (!pic)
{
Pic_SetError("Conv8To24, picture internal structure not found");
return(NULL);
}
buf=Pic_malloc(pic->Width*pic->Height*3);
if (!buf)
{
Pic_SetError("Conv8To24, not enough memory for temporary buffer");
return(NULL);
}
for(i=0 ; i<pic->Width*pic->Height ; i++)
{
buf[i*3+0]=pic->pPal[pic->pDatas[i]*3+0];
buf[i*3+1]=pic->pPal[pic->pDatas[i]*3+1];
buf[i*3+2]=pic->pPal[pic->pDatas[i]*3+2];
}
return(buf);
}
/* ---------------------------------------- */
static char* Conv8To16(unsigned long id)
{
PIC_PICTURE *pic;
unsigned short *buf;
unsigned long i;
unsigned short r,g,b,pix16;
pic=GetPic(id);
if (!pic)
{
Pic_SetError("Conv8To24, picture internal structure not found");
return(NULL);
}
buf=(unsigned short*)Pic_malloc(pic->Width*pic->Height*2);
if (!buf)
{
Pic_SetError("Conv8To24, not enough memory for temporary buffer");
return(NULL);
}
for(i=0 ; i<pic->Width*pic->Height ; i++)
{
b=pic->pPal[pic->pDatas[i]*3+0];
g=pic->pPal[pic->pDatas[i]*3+1];
r=pic->pPal[pic->pDatas[i]*3+2];
r>>=3;
g>>=3; g&=0x3E;
b>>=3;
pix16=(r<<10)+(g<<5)+b;
buf[i]=pix16;
}
return((unsigned char*)buf);
}
/* ---------------------------------------- */
static char* Conv16To24(unsigned long id)
{
PIC_PICTURE *pic;
unsigned short *pDatas;
unsigned char *buf;
unsigned long i;
unsigned short r,g,b;
pic=GetPic(id);
if (!pic)
{
Pic_SetError("Conv16To24, picture internal structure not found");
return(NULL);
}
buf=Pic_malloc(pic->Width*pic->Height*3);
if (!buf)
{
Pic_SetError("Conv16To24, not enough memory for temporary buffer");
return(NULL);
}
pDatas=(unsigned short*)pic->pDatas;
for(i=0 ; i<pic->Width*pic->Height ; i++)
{
r=(pDatas[i] & 0x7C00)>>(10-3);
g=(pDatas[i] & 0x03E0)>>(5-3);
b=(pDatas[i] & 0x001F)<<3;
buf[i*3+0]=(unsigned char)r;
buf[i*3+1]=(unsigned char)g;
buf[i*3+2]=(unsigned char)b;
}
return(buf);
}
/* ---------------------------------------- */
static char* Conv24To16(unsigned long id)
{
PIC_PICTURE *pic;
unsigned short *buf;
unsigned long i;
unsigned short r,g,b;
unsigned short pix16;
pic=GetPic(id);
if (!pic)
{
Pic_SetError("Conv24To16, picture internal structure not found");
return(NULL);
}
buf=(unsigned short*)Pic_malloc(pic->Width*pic->Height*2);
if (!buf)
{
Pic_SetError("Conv24To16, not enough memory for temporary buffer");
return(NULL);
}
for(i=0 ; i<pic->Width*pic->Height ; i++)
{
r=pic->pDatas[i*3+0];
g=pic->pDatas[i*3+1];
b=pic->pDatas[i*3+2];
/* r : 5 bits forts (0x7C) */
/* g : 5 bits (6e zapped) (0x3E) */
/* b : 5 bits faibles (0x1F) */
r>>=3;
g>>=3; g&=0x3E;
b>>=3;
pix16=(r<<10)+(g<<5)+b;
buf[i]=pix16;
}
return((unsigned char*)buf);
}
/* ---------------------------------------- */
static char* ConvPic(PIC_PICTURE *pic, unsigned long type, char* pErr)
{
unsigned char *buf;
unsigned long src,dst;
*pErr=0;
buf=NULL;
src=pic->Depth;
if (type==PIC_TYPE_TGA8 || type==PIC_TYPE_BMP8)
{
dst=8;
}
if (type==PIC_TYPE_TGA16)
{
dst=16;
}
if (type==PIC_TYPE_JPG || type==PIC_TYPE_TGA24 || type==PIC_TYPE_BMP24)
{
dst=24;
}
/* --- */
if (src==dst)
{
return(NULL);
}
/* --- */
if (src==8 && dst==24)
{
buf=Conv8To24(pic->ID);
if (!buf)
{
*pErr=1;
}
return(buf);
}
if (src==8 && dst==16)
{
buf=Conv8To16(pic->ID);
if (!buf)
{
*pErr=1;
}
return(buf);
}
/* --- */
if (src==16 && dst==24)
{
buf=Conv16To24(pic->ID);
if (!buf)
{
*pErr=1;
}
return(buf);
}
/* --- */
if (src==24 && dst==16)
{
buf=Conv24To16(pic->ID);
if (!buf)
{
*pErr=1;
}
return(buf);
}
/* --- */
if (src==24 && dst==8)
{
Pic_SetError("ConvPic, downsampling 24 to 8 bits unsupported");
*pErr=1;
return(NULL);
}
Pic_SetError("ConvPic, conversion %d to %d unsupported",src,dst);
*pErr=1;
return(NULL);
}
/* ---------------------------------------- */
unsigned long PIC_Save(unsigned long id, const char* FileName, unsigned long type, unsigned long qual)
{
PIC_PICTURE *pic;
unsigned char err;
unsigned char *buf;
unsigned char *freeit;
unsigned long depth;
freeit=NULL;
pic=GetPic(id);
if (!pic)
{
Pic_SetError("Save %s, picture internal structure not found",FileName);
return(0);
}
freeit=ConvPic(pic,type,&err);
if (err)
{
Pic_SetError("Save %s, error while converting picture",FileName);
return(0);
}
if (!freeit)
{
buf=pic->pDatas;
}
else
{
buf=freeit;
}
err=0;
switch(type)
{
/* --- */
case PIC_TYPE_JPG:
if ( !Pic_JPG_Write(FileName,qual,buf,pic->Width,pic->Height) )
{
if (freeit)
{
Pic_free(buf);
}
Pic_SetError("Save %s, error while saving JPG file",FileName);
err=1;
}
break;
/* --- */
case PIC_TYPE_TGA8:
case PIC_TYPE_TGA16:
case PIC_TYPE_TGA24:
if (type==PIC_TYPE_TGA8)
{
depth=8;
}
if (type==PIC_TYPE_TGA16)
{
depth=16;
}
if (type==PIC_TYPE_TGA24)
{
depth=24;
}
if ( !Pic_TGA_Write(FileName,pic->pPal,buf,pic->Width,pic->Height,depth) )
{
if (freeit)
{
Pic_free(freeit);
}
Pic_SetError("Save %s, error while saving TGA file",FileName);
err=1;
}
break;
/* --- */
case PIC_TYPE_BMP8:
case PIC_TYPE_BMP24:
if (type==PIC_TYPE_BMP8)
{
depth=8;
}
if (type==PIC_TYPE_BMP24)
{
depth=24;
}
if ( !Pic_BMP_Write(FileName,pic->pPal,buf,pic->Width,pic->Height,depth) )
{
if (freeit)
{
Pic_free(freeit);
}
Pic_SetError("Save %s, error while saving BMP file",FileName);
err=1;
}
break;
/* --- */
default:
Pic_SetError("Save %s, unknow save format/type",FileName);
err=1;
break;
}
if (freeit)
{
Pic_free(freeit);
}
return(err-1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long PIC_Destroy(unsigned long id)
{
PIC_PICTURE *prevpic,*pic;
unsigned long found;
prevpic=NULL;
found=0;
for(pic=HeadPic ; pic ; pic=pic->Next)
{
if (pic->ID==id)
{
found=1;
break;
}
prevpic=pic;
}
if (!found)
{
Pic_SetError("Destroy, picture internal structure not found");
return(0);
}
if (prevpic)
{
prevpic->Next=pic->Next;
}
if (pic->pDatas)
{
Pic_free(pic->pDatas);
}
if (pic->pPal)
{
Pic_free(pic->pPal);
}
if (pic==HeadPic)
{
HeadPic=pic->Next;
}
Pic_free(pic);
return(1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */

View file

@ -1,215 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pic_private.h"
#include "pic.h"
/* ---------------------------------------------------------------------------------------------------------------------------------- */
#pragma pack(1)
typedef struct TGA_HEADER
{
unsigned char LengthID;
unsigned char CMapType;
unsigned char ImageType;
unsigned short Origin;
unsigned short Length;
unsigned char Depth;
unsigned short XOrg;
unsigned short YOrg;
unsigned short Width;
unsigned short Height;
unsigned char ImageDepth;
unsigned char Desc;
} TGA_HEADER;
#pragma pack()
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long Pic_TGA_Read( const char *FileName,
unsigned char **ppPal, unsigned char **ppDatas,
unsigned long *pWidth, unsigned long *pHeight,
unsigned long *pDepth)
{
FILE *file;
TGA_HEADER tgah;
long w,h,d;
unsigned long size;
unsigned char *pDatas;
unsigned char *pPal;
long x,y;
long slsize;
unsigned char *scanline;
unsigned char r,g,b;
long i;
int upSideDown;
pDatas=NULL;
pPal=NULL;
file=fopen(FileName,"rb");
if (!file)
{
Pic_SetError("TGA_Read, unable to open %s",FileName);
return(0);
}
fread(&tgah,1,sizeof(TGA_HEADER),file);
if (tgah.ImageType>3)
{
Pic_SetError("TGA_Read, unsupported TGA format");
return(0);
}
*pWidth=w=tgah.Width;
*pHeight=h=tgah.Height;
*pDepth=d=tgah.ImageDepth;
upSideDown = ((tgah.Desc & (1 << 5))==0);
size=tgah.Width*tgah.Height*(tgah.ImageDepth/8);
pDatas=Pic_malloc(size);
if (!pDatas)
{
Pic_SetError("TGA_Read, not enough memory");
return(0);
}
if (*pDepth==8)
{
if (!ppPal)
{
Pic_free(pDatas);
Pic_SetError("TGA_Read, need a pointer to palette");
return(0);
}
pPal=Pic_calloc(1,256*3);
if (!pPal)
{
Pic_SetError("TGA_Read, not enough memory for palette");
return(0);
}
if (tgah.ImageType==1)
{
for(i=0 ; i<256*3 ; i+=3)
{
fread(&pPal[i+2],1,1,file);
fread(&pPal[i+1],1,1,file);
fread(&pPal[i+0],1,1,file);
}
}
*ppPal=pPal;
}
slsize=w*d/8;
scanline=Pic_calloc(1,slsize);
if (!scanline)
{
if (pPal)
{
Pic_free(pPal);
}
Pic_free(pDatas);
Pic_SetError("TGA_Read, not enough memory for scanline");
return(0);
}
for(y=0 ; y<h ; y++)
{
fread(scanline,1,slsize,file);
if (d==24 || d==32)
{
long mult=3;
if(d==32) mult=4;
for(x=0 ; x<w ; x++)
{
r=scanline[x*mult+0];
g=scanline[x*mult+1];
b=scanline[x*mult+2];
scanline[x*mult+0]=b;
scanline[x*mult+1]=g;
scanline[x*mult+2]=r;
}
}
if (upSideDown)
memcpy(&pDatas[(h-y-1)*slsize], scanline, slsize);
else
memcpy(&pDatas[(y)*slsize], scanline, slsize);
}
Pic_free(scanline);
fclose(file);
*ppDatas=pDatas;
return(1);
}
/* ---------------------------------------------------------------------------------------------------------------------------------- */
unsigned long Pic_TGA_Write( const char *FileName,
unsigned char *pPal,unsigned char *pDatas,
unsigned long w, unsigned long h, unsigned long d)
{
FILE *file;
TGA_HEADER tgah;
long x,y;
long slsize;
unsigned char *scanline;
unsigned char r,g,b;
file=fopen(FileName,"wb");
if (!file)
{
Pic_SetError("TGA_Write, unable to open %s",FileName);
return(0);
}
memset(&tgah,0,sizeof(TGA_HEADER));
tgah.LengthID=0;
if (d>8)
{
tgah.CMapType=0;
tgah.ImageType=2;
tgah.Length=0;
tgah.Depth=0;
}
else
{
tgah.CMapType=1;
tgah.ImageType=1;
tgah.Length=256;
tgah.Depth=24;
}
tgah.Origin=0;
tgah.XOrg=0;
tgah.YOrg=0;
tgah.Width=(unsigned short)w;
tgah.Height=(unsigned short)h;
tgah.ImageDepth=(unsigned char)d;
tgah.Desc=0;
fwrite(&tgah,1,sizeof(TGA_HEADER),file);
if (d==8)
{
fwrite(pPal,1,256*3,file);
}
slsize=w*d/8;
scanline=Pic_calloc(1,slsize);
if (!scanline)
{
Pic_SetError("TGA_Write, not enough memory for scanline");
return(0);
}
for(y=0 ; y<(long)h ; y++)
{
memcpy(scanline,&pDatas[(h-y-1)*slsize],slsize);
if (d==24)
{
for(x=0 ; x<(long)w ; x++)
{
r=scanline[x*3+0];
g=scanline[x*3+1];
b=scanline[x*3+2];
scanline[x*3+0]=b;
scanline[x*3+1]=g;
scanline[x*3+2]=r;
}
}
fwrite(scanline,1,slsize,file);
}
Pic_free(scanline);
fclose(file);
return(1);
}

View file

@ -1,48 +0,0 @@
#ifndef _PIC_H_
#define _PIC_H_
#ifdef __cplusplus
extern "C" {
#endif
/* ---------------------------------------------------------------------------------------------------------------------------------- */
#define PIC_TYPE_JPG 1
#define PIC_TYPE_TGA8 2
#define PIC_TYPE_TGA16 3
#define PIC_TYPE_TGA24 4
#define PIC_TYPE_BMP8 5
#define PIC_TYPE_BMP24 6
/* ---------------------------------------------------------------------------------------------------------------------------------- */
/*
* Basic API
*/
extern unsigned long PIC_Load(char* FileName, unsigned char Quantize);
extern unsigned long PIC_Create(unsigned char* pPal, unsigned char* pDatas, unsigned long w, unsigned long h, unsigned long d);
extern unsigned long PIC_Save(unsigned long id, const char* FileName, unsigned long type, unsigned long qual);
extern unsigned long PIC_GetInfos( unsigned long id,
unsigned char* *ppPal, unsigned char* *ppDatas,
unsigned long *pW, unsigned long *pH, unsigned long *pD);
extern unsigned long PIC_Destroy(unsigned long id);
/*
* System
*/
extern unsigned long PIC_GetMemNbAllocs(void);
extern unsigned long PIC_GetMemAllocated(void);
extern char* PIC_GetError(void);
extern unsigned char PIC_Error(void);
extern void PIC_ResetError(void);
extern unsigned char PIC_OnErrorCall( void pFnct(void) );
/* ---------------------------------------------------------------------------------------------------------------------------------- */
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,71 +0,0 @@
#ifndef _PIC_PRIVATE_H_
#define _PIC_PRIVATE_H_
#ifdef __cplusplus
extern "C" {
#endif
/* ---------------------------------------------------------------------------------------------------------------------------------- */
typedef struct PIC_PICTURE
{
unsigned long ID;
unsigned long Width;
unsigned long Height;
unsigned long Depth;
unsigned char *pDatas;
unsigned char *pPal;
struct PIC_PICTURE *Next;
} PIC_PICTURE;
/* ---------------------------------------------------------------------------------------------------------------------------------- */
/*
* JPG
*/
extern unsigned long Pic_JPG_Read( const char *FileName,
unsigned char **ppPal, unsigned char **ppDatas,
unsigned long *w, unsigned long *h);
extern unsigned long Pic_JPG_Write( const char *FileName,
unsigned long Qual,
unsigned char *pDatas,
unsigned long w, unsigned long h);
/*
* TGA
*/
extern unsigned long Pic_TGA_Read( const char *FileName,
unsigned char **ppPal, unsigned char **ppDatas,
unsigned long *pWidth, unsigned long *pHeight,
unsigned long *pDepth);
extern unsigned long Pic_TGA_Write( const char *FileName,
unsigned char *pPal,unsigned char *pDatas,
unsigned long w, unsigned long h, unsigned long d);
/*
* BMP
*/
extern unsigned long Pic_BMP_Read( const char *FileName,
unsigned char **ppPal, unsigned char **ppDatas,
unsigned long *pWidth, unsigned long *pHeight,
unsigned long *pDepth);
extern unsigned long Pic_BMP_Write( const char *FileName,
unsigned char *pPal,unsigned char *pDatas,
unsigned long w, unsigned long h, unsigned long d);
/*
* System
*/
extern void* Pic_malloc(size_t size);
extern void* Pic_calloc(size_t count, size_t size);
extern void Pic_free(void *memblock);
extern size_t Pic__msize(void *memblock);
extern void Pic_SetError(const char *msg, ...);
/* ---------------------------------------------------------------------------------------------------------------------------------- */
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,68 +0,0 @@
#include <assert.h>
#include <algorithm>
#include "readpic.h"
#include "pic.h"
#include <nel/misc/rgba.h>
//============================================================
// Image API.
//============================================================
bool PIC_LoadPic(const std::string &path, std::vector<NLMISC::CBGRA> &tampon, uint &Width, uint &Height)
{
uint32 id;
unsigned char *pal, *data;
unsigned long w,h,depth;
uint i;
// Loadons l'image.
id= PIC_Load((char*)path.c_str(), 0);
if(id==0)
return false;
PIC_GetInfos( id, &pal, &data, &w, &h, &depth);
Width=w;
Height=h;
// On traduit en RGBA.
tampon.resize(w*h);
switch(depth)
{
case 8:
for(i=0;i<w*h;i++)
{
tampon[i].R= data[i];
tampon[i].G= data[i];
tampon[i].B= data[i];
tampon[i].A= data[i];
}
break;
case 24:
for(i=0;i<w*h;i++)
{
tampon[i].R= data[i*3+ 0];
tampon[i].G= data[i*3+ 1];
tampon[i].B= data[i*3+ 2];
tampon[i].A= 255;
}
break;
case 32:
for(i=0;i<w*h;i++)
{
tampon[i].R= data[i*4+ 0];
tampon[i].G= data[i*4+ 1];
tampon[i].B= data[i*4+ 2];
tampon[i].A= data[i*4+ 3];
}
break;
}
// On ferme.
PIC_Destroy(id);
return true;
}

View file

@ -1,26 +0,0 @@
#ifndef _READPIC_H_
#define _READPIC_H_
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include <string>
#include <vector>
using namespace std;
#include <nel/misc/types_nl.h>
#include <nel/misc/rgba.h>
//============================================================
// API.
//============================================================
bool PIC_LoadPic(const std::string &Path, std::vector<NLMISC::CBGRA> &tampon, uint &Width, uint &Height);
#endif