boost::shared_array AbbrevToFullJPEG(size_t *pDstSizeBytes, const void *pHeaders, size_t stHeadersSizeBytes, const void *pAbbrev, size_t stAbbrevSizeBytes) { boost::shared_array res; uint8_t *pBuf = NULL, *p8Ptr = NULL; const uint8_t *p8AbbrevPtr = (const uint8_t *) pAbbrev; const uint8_t *p8HeaderPtr = (const uint8_t *) pHeaders; size_t stAbbrevCopied = 0; size_t stTmp = 0; *pDstSizeBytes = stHeadersSizeBytes + stAbbrevSizeBytes - 4; // - 4 because headers and abbrev have the same first 2 bytes and the same trailing 2 bytes res = boost::shared_array(new uint8_t[*pDstSizeBytes]); pBuf = p8Ptr = res.get(); // grab first 6 bytes from abbrev // this will be enough to tell us how long the JFIF marker segment is (APP0, see official spec) memcpy(p8Ptr, p8AbbrevPtr, 6); p8Ptr += 6; p8AbbrevPtr += 6; stAbbrevCopied += 6; unsigned int uJFIFSegmentLength = pBuf[5] | (pBuf[4] << 8); uJFIFSegmentLength -= 2; // subtract 2 because we already read in the length (2 bytes) // copy over the rest of the JFIF segment memcpy(p8Ptr, p8AbbrevPtr, uJFIFSegmentLength); p8Ptr += uJFIFSegmentLength; p8AbbrevPtr += uJFIFSegmentLength; stAbbrevCopied += uJFIFSegmentLength; // skip first 2 bytes of header since it is a redundant p8HeaderPtr += 2; // copy over the entire remaining header aside from the last two trailing bytes stTmp = stHeadersSizeBytes - 4; // -4 because we don't want the first two or last two bytes memcpy(p8Ptr, p8HeaderPtr, stTmp); p8Ptr += stTmp; // copy over the rest of the abbreviated JPEG, we're done stTmp = stAbbrevSizeBytes - stAbbrevCopied; memcpy(p8Ptr, p8AbbrevPtr, stTmp); p8Ptr += stTmp; p8AbbrevPtr += stTmp; stAbbrevCopied += stTmp; return res; }