Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

// untitled.

cpp
//
//
// This program 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.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
//

#include <iostream>
#include <fstream>

typedef unsigned char BYTE;


typedef unsigned short WORD;

using namespace std;

struct PCXHeader {
BYTE Identifier; /* PCX Id Number (Always 0x0A) */
BYTE Version; /* Version Number */
BYTE Encoding; /* Encoding Format */
BYTE BitsPerPixel; /* Bits per Pixel */
WORD XStart; /* Left of image */
WORD YStart; /* Top of Image */
WORD XEnd; /* Right of Image*/
WORD YEnd; /* Bottom of image */
WORD HorzRes; /* Horizontal Resolution */
WORD VertRes; /* Vertical Resolution */
BYTE Palette[48]; /* 16-Color EGA Palette */
BYTE Reserved1; /* Reserved (Always 0) */
BYTE NumBitPlanes; /* Number of Bit Planes */
WORD BytesPerLine; /* Bytes per Scan-line */
WORD PaletteType; /* Palette Type */
WORD HorzScreenSize; /* Horizontal Screen Size */
WORD VertScreenSize; /* Vertical Screen Size */
BYTE Reserved2[54]; /* Reserved (Always 0) */
};

struct CCYHeader {
BYTE id[3];
WORD frames;
WORD width;
WORD height;
BYTE pixelSize;
BYTE orientation;
BYTE method;
WORD paleteSize;
};

int main(int argc, char **argv) {

ifstream sStream;
PCXHeader pcxHeader;

unsigned int *iPalette;


unsigned int *iPixels;
int ImageWidth, ImageHeight, ImageBytes, nLines, qtd;
long FileBegin, FileEnd, FileBytes;
char nome_arq[50];

// Parametros de entrada
//cout << "Caminho do Arquivo: ";
//cin >> caminho;
cout << "Numero Frames: ";
cin >> qtd;

int frame[qtd];

// Ler frames
for (int i = 0; i < qtd; i++) {
sprintf(nome_arq, "jungle%d.pcx",i);
sStream.open(nome_arq, ios::binary);
if (!sStream) {
cout << "Falha ao Carregar o Arquivo " << endl;
return 1;
}

if (i ==0){
FileBegin = sStream.tellg();
sStream.seekg(0, ios::end);
FileEnd = sStream.tellg();
FileBytes = FileEnd - FileBegin;

sStream.seekg(0, ios::beg);
sStream.read(reinterpret_cast<char*>(&pcxHeader), sizeof(pcxHeader));
sStream.seekg(128);
}
}

ImageWidth = (pcxHeader.XEnd - pcxHeader.XStart + 1);


ImageHeight = (pcxHeader.YEnd - pcxHeader.YStart + 1);
ImageBytes = ImageWidth * ImageHeight;
nLines = ImageBytes / pcxHeader.BytesPerLine;

iPixels = new unsigned int[ImageBytes];


for (int i = 0; i < ImageBytes; i++) iPixels[i] = 0;

unsigned char cBuffer = 0;


unsigned char cRC = 0;
for (int h = nLines - 1; h >= 0; h--) {
for (int rgb = 0; rgb < pcxHeader.NumBitPlanes; rgb++) {
for (int c = 0; c < pcxHeader.BytesPerLine; c++) {
if (cRC == 0) {
cBuffer = sStream.get();
if ((cBuffer & 0xC0) == 0xC0) { /* 2-byte code */
cRC = cBuffer & 0x3F; /* Get run
count */
cBuffer = sStream.get(); /* Get pixel
value */
};
iPixels[h*ImageWidth+c] |= cBuffer << rgb *
pcxHeader.BitsPerPixel;
} else {
iPixels[h*ImageWidth+c] |= cBuffer << rgb *
pcxHeader.BitsPerPixel;
};
if (cRC > 0) cRC--;
};
};
};

int cont=0;
sStream.seekg(FileBytes - 768);
iPalette = new unsigned int[768];
for (int i = 0; i < 768; i++) {
cBuffer = sStream.get();
iPalette[i] = cBuffer;
// if ((i+1)%3==0)
// printf("%3d - %3d %3d %3d\n", (i+1)/3, iPalette[i-2], iPalette[i-
1], iPalette[i]);
// if (cBuffer == 0)
// cont++;
}
// printf("%d", cont);

sStream.close();

bool cyclingColor[256];
for (int x = 0; x < qtd; x++) {

/*

for (int x = 0; x < qtd-1; x++) {


for (int i = 0; i < ImageHeight; i++) {
for (int j = 0; j < ImageWidth; j++) {
if (frame[x].GetImagePixels(j,i) ==
frame[x+1].GetImagePixels(j,i)) {

ssStream.put(iPixels[i*ImageHeight+j]);
}
}
};
*/

/* Monta CCY */

ofstream ssStream;
CCYHeader ccyHeader;

ccyHeader.id[0] = 67; /* C */
ccyHeader.id[1] = 67; /* C */
ccyHeader.id[2] = 89; /* Y */

ccyHeader.frames = 1;
ccyHeader.width = ImageWidth;
ccyHeader.height = ImageHeight;
ccyHeader.pixelSize = pcxHeader.BitsPerPixel;
ccyHeader.orientation = 1;
ccyHeader.method = 0;
ccyHeader.paleteSize = 768;

// Cria CCY
ssStream.open("jungle.ccy", ios::binary);
if (!ssStream) {
cout << "Falha ao criar o Arquivo " << endl;
return 1;
}

// Cabeçalho
ssStream.write(reinterpret_cast<char*>(&ccyHeader), sizeof(ccyHeader));
// Imagem
for (int i = 0; i < ImageHeight; i++) {
for (int j = 0; j < ImageWidth; j++) {
ssStream.put(iPixels[i*ImageHeight+j]);
}
};
// Paleta
for (int i = 0; i < 768; i++) {
ssStream.put(iPalette[i]);
};

ssStream.close(); // Fecha arquivo CCY

return 0;
}

PCXHeader Novo;
ifstream arq(Path,ios::binary);
//LEITURA HEADER
arq.read(reinterpret_cast<char*>(&Novo),sizeof(Novo)); //leitura do cabeçalho
ImageWidth = (Novo.XEnd - Novo.XStart + 1);
ImageHeight = (Novo.YEnd - Novo.YStart + 1);
nLines = (ImageWidth*ImageHeight)/Novo.BytesPerLine;

arq.readsome(indices,altura x largura) // lendo o tamanho da tela

//LEITURA DA IMAGE DATA


int i;
unsigned char buff = 0;
unsigned char runcount = 0;
ipixels = new unsigned int[ImageWidth*ImageHeight];
for(i=0;i<ImageWidth*ImageHeight;i++) ipixels[i] = 0;
arq.seekg(128);

for (int h=nLines-1;h>=0;h--)


for (int rgb=0;rgb<Novo.NumBitPlanes;rgb++)
for (int c=0;c<Novo.BytesPerLine;c++){
if(runcount==0){
buff = arq.get();
if(buff > 192){
runcount = buff - 192;
buff = arq.get();
ipixels[h*ImageWidth+c] |= buff <<
rgb*Novo.BitsPerPixel;
runcount --;
}else
ipixels[h*ImageWidth+c] |= buff <<
rgb*Novo.BitsPerPixel;
}else{
ipixels[h*ImageWidth+c] |= buff <<
rgb*Novo.BitsPerPixel;
runcount --;
}
}

//FIM DA LEITURA DA IMAGE DATA

You might also like