Skip to content

Commit

Permalink
fix crash when taking screenshot, fixes #193
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Nov 6, 2014
1 parent 1eeed27 commit 5bb1d48
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/Format.cpp
Expand Up @@ -228,11 +228,11 @@ struct PNGChunk

PNGChunk(int length, std::string name)
{
if(name.length()!=4)
if (name.length()!=4)
throw std::runtime_error("Invalid chunk name");
std::copy(name.begin(), name.begin()+4, Name);
Length = length;
if(length)
if (length)
{
Data = new char[length];
std::fill(Data, Data+length, 0);
Expand All @@ -244,7 +244,7 @@ struct PNGChunk
}
unsigned long CRC()
{
if(!Data)
if (!Data)
{
return format::CalculateCRC((unsigned char*)Name, 4);
}
Expand All @@ -260,7 +260,7 @@ struct PNGChunk
}
~PNGChunk()
{
if(Data)
if (Data)
delete[] Data;
}
};
Expand All @@ -270,28 +270,28 @@ std::vector<char> format::VideoBufferToPNG(const VideoBuffer & vidBuf)
std::vector<PNGChunk*> chunks;

//Begin IHDR (Image header) chunk (Image size and depth)
PNGChunk IHDRChunk = PNGChunk(13, "IHDR");
PNGChunk *IHDRChunk = new PNGChunk(13, "IHDR");

//Image Width
IHDRChunk.Data[0] = (vidBuf.Width>>24)&0xFF;
IHDRChunk.Data[1] = (vidBuf.Width>>16)&0xFF;
IHDRChunk.Data[2] = (vidBuf.Width>>8)&0xFF;
IHDRChunk.Data[3] = (vidBuf.Width)&0xFF;
IHDRChunk->Data[0] = (vidBuf.Width>>24)&0xFF;
IHDRChunk->Data[1] = (vidBuf.Width>>16)&0xFF;
IHDRChunk->Data[2] = (vidBuf.Width>>8)&0xFF;
IHDRChunk->Data[3] = (vidBuf.Width)&0xFF;

//Image Height
IHDRChunk.Data[4] = (vidBuf.Height>>24)&0xFF;
IHDRChunk.Data[5] = (vidBuf.Height>>16)&0xFF;
IHDRChunk.Data[6] = (vidBuf.Height>>8)&0xFF;
IHDRChunk.Data[7] = (vidBuf.Height)&0xFF;
IHDRChunk->Data[4] = (vidBuf.Height>>24)&0xFF;
IHDRChunk->Data[5] = (vidBuf.Height>>16)&0xFF;
IHDRChunk->Data[6] = (vidBuf.Height>>8)&0xFF;
IHDRChunk->Data[7] = (vidBuf.Height)&0xFF;

//Bit depth
IHDRChunk.Data[8] = 8; //8bits per channel or 24bpp
IHDRChunk->Data[8] = 8; //8bits per channel or 24bpp

//Colour type
IHDRChunk.Data[9] = 2; //RGB triple
IHDRChunk->Data[9] = 2; //RGB triple

//Everything else is default
chunks.push_back(&IHDRChunk);
chunks.push_back(IHDRChunk);

//Begin image data, format is 8bit RGB (24bit pixel)
int dataPos = 0;
Expand Down Expand Up @@ -356,17 +356,17 @@ std::vector<char> format::VideoBufferToPNG(const VideoBuffer & vidBuf)
if (result != Z_STREAM_END) exit(result);

int compressedSize = compressedBufferSize-zipStream.avail_out;
PNGChunk IDATChunk = PNGChunk(compressedSize, "IDAT");
std::copy(compressedData, compressedData+compressedSize, IDATChunk.Data);
chunks.push_back(&IDATChunk);
PNGChunk *IDATChunk = new PNGChunk(compressedSize, "IDAT");
std::copy(compressedData, compressedData+compressedSize, IDATChunk->Data);
chunks.push_back(IDATChunk);

deflateEnd(&zipStream);

delete[] compressedData;
delete[] uncompressedData;

PNGChunk IENDChunk = PNGChunk(0, "IEND");
chunks.push_back(&IENDChunk);
PNGChunk *IENDChunk = new PNGChunk(0, "IEND");
chunks.push_back(IENDChunk);

//Write chunks to output buffer
int finalDataSize = 8;
Expand Down Expand Up @@ -416,6 +416,8 @@ std::vector<char> format::VideoBufferToPNG(const VideoBuffer & vidBuf)
finalData[finalDataPos++] = (tempCRC>>16)&0xFF;
finalData[finalDataPos++] = (tempCRC>>8)&0xFF;
finalData[finalDataPos++] = (tempCRC)&0xFF;

delete cChunk;
}

std::vector<char> outputData(finalData, finalData+finalDataPos);
Expand Down

0 comments on commit 5bb1d48

Please sign in to comment.