fix crash when taking screenshot, fixes #193

This commit is contained in:
jacob1 2014-11-05 20:39:51 -05:00
parent 1eeed277a0
commit 5bb1d484d0

View File

@ -228,11 +228,11 @@ struct PNGChunk
PNGChunk(int length, std::string name) PNGChunk(int length, std::string name)
{ {
if(name.length()!=4) if (name.length()!=4)
throw std::runtime_error("Invalid chunk name"); throw std::runtime_error("Invalid chunk name");
std::copy(name.begin(), name.begin()+4, Name); std::copy(name.begin(), name.begin()+4, Name);
Length = length; Length = length;
if(length) if (length)
{ {
Data = new char[length]; Data = new char[length];
std::fill(Data, Data+length, 0); std::fill(Data, Data+length, 0);
@ -244,7 +244,7 @@ struct PNGChunk
} }
unsigned long CRC() unsigned long CRC()
{ {
if(!Data) if (!Data)
{ {
return format::CalculateCRC((unsigned char*)Name, 4); return format::CalculateCRC((unsigned char*)Name, 4);
} }
@ -260,7 +260,7 @@ struct PNGChunk
} }
~PNGChunk() ~PNGChunk()
{ {
if(Data) if (Data)
delete[] Data; delete[] Data;
} }
}; };
@ -270,28 +270,28 @@ std::vector<char> format::VideoBufferToPNG(const VideoBuffer & vidBuf)
std::vector<PNGChunk*> chunks; std::vector<PNGChunk*> chunks;
//Begin IHDR (Image header) chunk (Image size and depth) //Begin IHDR (Image header) chunk (Image size and depth)
PNGChunk IHDRChunk = PNGChunk(13, "IHDR"); PNGChunk *IHDRChunk = new PNGChunk(13, "IHDR");
//Image Width //Image Width
IHDRChunk.Data[0] = (vidBuf.Width>>24)&0xFF; IHDRChunk->Data[0] = (vidBuf.Width>>24)&0xFF;
IHDRChunk.Data[1] = (vidBuf.Width>>16)&0xFF; IHDRChunk->Data[1] = (vidBuf.Width>>16)&0xFF;
IHDRChunk.Data[2] = (vidBuf.Width>>8)&0xFF; IHDRChunk->Data[2] = (vidBuf.Width>>8)&0xFF;
IHDRChunk.Data[3] = (vidBuf.Width)&0xFF; IHDRChunk->Data[3] = (vidBuf.Width)&0xFF;
//Image Height //Image Height
IHDRChunk.Data[4] = (vidBuf.Height>>24)&0xFF; IHDRChunk->Data[4] = (vidBuf.Height>>24)&0xFF;
IHDRChunk.Data[5] = (vidBuf.Height>>16)&0xFF; IHDRChunk->Data[5] = (vidBuf.Height>>16)&0xFF;
IHDRChunk.Data[6] = (vidBuf.Height>>8)&0xFF; IHDRChunk->Data[6] = (vidBuf.Height>>8)&0xFF;
IHDRChunk.Data[7] = (vidBuf.Height)&0xFF; IHDRChunk->Data[7] = (vidBuf.Height)&0xFF;
//Bit depth //Bit depth
IHDRChunk.Data[8] = 8; //8bits per channel or 24bpp IHDRChunk->Data[8] = 8; //8bits per channel or 24bpp
//Colour type //Colour type
IHDRChunk.Data[9] = 2; //RGB triple IHDRChunk->Data[9] = 2; //RGB triple
//Everything else is default //Everything else is default
chunks.push_back(&IHDRChunk); chunks.push_back(IHDRChunk);
//Begin image data, format is 8bit RGB (24bit pixel) //Begin image data, format is 8bit RGB (24bit pixel)
int dataPos = 0; int dataPos = 0;
@ -356,17 +356,17 @@ std::vector<char> format::VideoBufferToPNG(const VideoBuffer & vidBuf)
if (result != Z_STREAM_END) exit(result); if (result != Z_STREAM_END) exit(result);
int compressedSize = compressedBufferSize-zipStream.avail_out; int compressedSize = compressedBufferSize-zipStream.avail_out;
PNGChunk IDATChunk = PNGChunk(compressedSize, "IDAT"); PNGChunk *IDATChunk = new PNGChunk(compressedSize, "IDAT");
std::copy(compressedData, compressedData+compressedSize, IDATChunk.Data); std::copy(compressedData, compressedData+compressedSize, IDATChunk->Data);
chunks.push_back(&IDATChunk); chunks.push_back(IDATChunk);
deflateEnd(&zipStream); deflateEnd(&zipStream);
delete[] compressedData; delete[] compressedData;
delete[] uncompressedData; delete[] uncompressedData;
PNGChunk IENDChunk = PNGChunk(0, "IEND"); PNGChunk *IENDChunk = new PNGChunk(0, "IEND");
chunks.push_back(&IENDChunk); chunks.push_back(IENDChunk);
//Write chunks to output buffer //Write chunks to output buffer
int finalDataSize = 8; int finalDataSize = 8;
@ -416,6 +416,8 @@ std::vector<char> format::VideoBufferToPNG(const VideoBuffer & vidBuf)
finalData[finalDataPos++] = (tempCRC>>16)&0xFF; finalData[finalDataPos++] = (tempCRC>>16)&0xFF;
finalData[finalDataPos++] = (tempCRC>>8)&0xFF; finalData[finalDataPos++] = (tempCRC>>8)&0xFF;
finalData[finalDataPos++] = (tempCRC)&0xFF; finalData[finalDataPos++] = (tempCRC)&0xFF;
delete cChunk;
} }
std::vector<char> outputData(finalData, finalData+finalDataPos); std::vector<char> outputData(finalData, finalData+finalDataPos);