1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/***************************************************/
/* Unit GIF : contains procedure to write and load */
/* conpressed gif image. */
/* Use an ultra fast compression method */
/***************************************************/
#include <stdio.h>
#include "CImage.h"
#include "gif/gif.h"
#include "jpg/jpegdecoder.h"
#include "png/hpng.h"
// ------ Default constructor -----
CImage::CImage() {
strcpy(m_LastError,"No errors");
m_Width=0;
m_Height=0;
m_Data=NULL;
}
// ------ Load image -----
int CImage::LoadImage(char *FileName) {
Release();
// Get extensions
char *p = strrchr(FileName,'.');
char ext[12];
if( p==NULL ) {
sprintf(m_LastError,"Does not have extensions.");
return 0;
}
strcpy(ext,p+1);
if( strcasecmp(ext,"gif")!=0 &&
strcasecmp(ext,"jpg")!=0 &&
strcasecmp(ext,"jpeg")!=0 &&
strcasecmp(ext,"png")) {
sprintf(m_LastError,"Not gif,jpg or png.");
return 0;
}
// Load GIF image
if( strcasecmp(ext,"gif")==0 ) {
GIF_IMAGE img;
int Length;
strcpy(img.FileName,FileName);
if( !LoadGifImage(&img) ) {
strcpy(m_LastError,GifErrorMessage);
return 0;
}
m_Width=img.width;
m_Height=img.height;
Length=m_Width*m_Height;
m_Data=(BYTE *)malloc( Length*3 );
for(int i=0,j=0;i<Length;i++,j+=3) {
m_Data[j+0] = (BYTE)( img.Blue[ (BYTE)img.data[i] ] );
m_Data[j+1] = (BYTE)( img.Green[ (BYTE)img.data[i] ] );
m_Data[j+2] = (BYTE)( img.Red[ (BYTE)img.data[i] ] );
}
free(img.data);
return 1;
}
// Load JPG image
if( strcasecmp(ext,"jpg")==0 || strcasecmp(ext,"jpeg")==0 ) {
JPEG_IMAGE img;
strcpy(img.FileName,FileName);
if( !LoadJpegImage(&img) ) {
strcpy(m_LastError,JpegErrorMessage);
return 0;
}
m_Width=img.width;
m_Height=img.height;
m_Data=(BYTE *)img.data;
return 1;
}
// Load PNG image
if( strcasecmp(ext,"png")==0 ) {
PNG_IMAGE img;
strcpy(img.FileName,FileName);
if( !LoadPngImage(&img) ) {
strcpy(m_LastError,PngErrorMessage);
return 0;
}
m_Width=img.width;
m_Height=img.height;
m_Data=(BYTE *)img.data;
return 1;
}
return 1;
}
int CImage::Width() {
return m_Width;
}
int CImage::Height() {
return m_Height;
}
BYTE *CImage::GetData() {
return m_Data;
}
// ------ Get error message if a routine fails ------
char *CImage::GetErrorMessage() {
return m_LastError;
}
// ------ Write a PNG file -----------------------------
char *CImage::WritePNG(char *FileName,int width,int height,BYTE *data) {
return WritePngImage(FileName,width,height,data);
}
// ------ Release memory -----------------------------
void CImage::Release() {
if( m_Data!=NULL ) {
free(m_Data);
m_Data=NULL;
}
m_Width=0;
m_Height=0;
}
|