Comments (6)
Unintentionally I sent you the original code, below is the code after my changes:
EStatusCode JPEGImageParser::ReadPhotoshopData(JPEGImageInformation& outImageInformation,bool outPhotoshopDataOK)
{
EStatusCode status;
unsigned int intSkip;
unsigned long toSkip;
unsigned int nameSkip;
unsigned long dataLength;
bool resolutionBimNotFound = true;
do {
status = ReadIntValue(intSkip);
if(status != PDFHummus::eSuccess)
break;
toSkip = intSkip-2;
status = SkipTillChar(scEOS,toSkip);
if(status != PDFHummus::eSuccess)
break;
while(toSkip > 0 && resolutionBimNotFound)
{
if (toSkip < 4)
break;
status = ReadStreamToBuffer(4);
if(status !=PDFHummus::eSuccess)
break;
toSkip-=4;
if(0 != memcmp(mReadBuffer,sc8Bim,4))
break; // k. corrupt header. stop here and just skip the next
if (toSkip < 3)
break;
status = ReadStreamToBuffer(3);
if(status !=PDFHummus::eSuccess)
break;
toSkip-=3;
nameSkip = (int)mReadBuffer[2];
if(nameSkip % 2 == 0)
++nameSkip;
if (toSkip < nameSkip)
break;
SkipStream(nameSkip);
toSkip-=nameSkip;
resolutionBimNotFound = (0 != memcmp(mReadBuffer,scResolutionBIMID,2));
if (toSkip < 4)
break;
status = ReadLongValue(dataLength);
if(status != PDFHummus::eSuccess)
break;
toSkip-=4;
if(resolutionBimNotFound)
{
if(dataLength % 2 == 1)
++dataLength;
if (toSkip < dataLength)
break;
toSkip-=dataLength;
SkipStream(dataLength);
}
else
{
if (toSkip < 16)
break;
status = ReadStreamToBuffer(16);
if(status !=PDFHummus::eSuccess)
break;
toSkip-=16;
outImageInformation.PhotoshopInformationExists = true;
outImageInformation.PhotoshopXDensity = GetIntValue(mReadBuffer) + GetFractValue(mReadBuffer + 2);
outImageInformation.PhotoshopYDensity = GetIntValue(mReadBuffer + 8) + GetFractValue(mReadBuffer + 10);
}
}
if(PDFHummus::eSuccess == status)
SkipStream(toSkip);
}while(false);
outPhotoshopDataOK = !resolutionBimNotFound;
return status;
}
from pdf-writer.
Hi Hadas,
seems good. i'll take it.
Thanks.
Gal.
ps. mazel tov.
from pdf-writer.
Thanks :)
from pdf-writer.
Below is the final code - I changed the code to return false in the out parameter "outPhotoshopDataOK" in case of failure.
EStatusCode JPEGImageParser::ReadPhotoshopData(JPEGImageInformation& outImageInformation,bool outPhotoshopDataOK)
{
EStatusCode status;
unsigned int intSkip;
unsigned long toSkip;
unsigned int nameSkip;
unsigned long dataLength;
bool resolutionBimNotFound = true;
do {
status = ReadIntValue(intSkip);
if(status != PDFHummus::eSuccess)
break;
toSkip = intSkip-2;
status = SkipTillChar(scEOS,toSkip);
if(status != PDFHummus::eSuccess)
break;
while(toSkip > 0 && resolutionBimNotFound)
{
if (toSkip < 4)
break;
status = ReadStreamToBuffer(4);
if(status !=PDFHummus::eSuccess)
break;
toSkip-=4;
if(0 != memcmp(mReadBuffer,sc8Bim,4))
break; // k. corrupt header. stop here and just skip the next
if (toSkip < 3)
break;
status = ReadStreamToBuffer(3);
if(status !=PDFHummus::eSuccess)
break;
toSkip-=3;
nameSkip = (int)mReadBuffer[2];
if(nameSkip % 2 == 0)
++nameSkip;
if (toSkip < nameSkip)
break;
SkipStream(nameSkip);
toSkip-=nameSkip;
resolutionBimNotFound = (0 != memcmp(mReadBuffer,scResolutionBIMID,2));
if (toSkip < 4)
{
resolutionBimNotFound = true;
break;
}
status = ReadLongValue(dataLength);
if (status != PDFHummus::eSuccess)
{
resolutionBimNotFound = true;
break;
}
toSkip-=4;
if(resolutionBimNotFound)
{
if(dataLength % 2 == 1)
++dataLength;
if (toSkip < dataLength)
break;
toSkip-=dataLength;
SkipStream(dataLength);
}
else
{
if (toSkip < 16)
{
resolutionBimNotFound = true;
break;
}
status = ReadStreamToBuffer(16);
if(status !=PDFHummus::eSuccess)
{
resolutionBimNotFound = true;
break;
}
toSkip-=16;
outImageInformation.PhotoshopInformationExists = true;
outImageInformation.PhotoshopXDensity = GetIntValue(mReadBuffer) + GetFractValue(mReadBuffer + 2);
outImageInformation.PhotoshopYDensity = GetIntValue(mReadBuffer + 8) + GetFractValue(mReadBuffer + 10);
}
}
if(PDFHummus::eSuccess == status)
SkipStream(toSkip);
}while(false);
outPhotoshopDataOK = !resolutionBimNotFound;
return status;
}
from pdf-writer.
Hi Hadas,
well i figured that the code looks a little bit cluttered with all those checks. I figured the spirit of the solution has to be with - if there's a read error fail the whole process, if there's not enough to read than just ignore this photoshop data.
This led me to rewrite this method a bit. you can see the end result in what i checked in right now here:
https://github.com/galkahana/PDF-Writer/blob/master/PDFWriter/JPEGImageParser.cpp#L283
the code is refactored to let subroutines deal with checking whether there's enough to read, do the read and reduce the accumulated bytes to skip. The result is cleaner code, having the same amount of conditionals as the original.
I think it still maintains the same gist of things.
let me know what you think.
Gal.
from pdf-writer.
Hi Gal,
It's look good and cleaner.
Thanks,
Hadas
from pdf-writer.
Related Issues (20)
- Parse a screenplay into scene objects? HOT 2
- color emojis HOT 16
- Links are removed when documents are merged HOT 8
- Color inversion problem occurs when exporting images HOT 1
- infinite loop HOT 2
- Crash when WriteUsedFontsDefinitions HOT 17
- Publish to github releases without PDFWriterTesting HOT 4
- U3D support, 10 years later HOT 10
- `Segmentation fault (core dumped)` just for adding `PDFWriter pdfWriter` in the `h` file HOT 11
- Fail to draw rectangle to pdf,if rectangle larger than(595x842) HOT 2
- german umlaute diacritic not rendered properly HOT 2
- Make a PDF with an image in memory HOT 4
- Double hex encoding in AbstractContentContext::TJHexLow HOT 3
- Perfectly kosher bug :) HOT 13
- bug: Stack overflow with Invalid PageTree HOT 1
- bug: Stack Overflow when loop in ParseNewObject -> ParseExistingInDirectStreamObject HOT 1
- bug: Requested Allocation Size too big HOT 2
- bug: Heap Use-after-free HOT 1
- bug: Allocation size too big when /Count is high HOT 2
- This line : ` status = pdfWriter.StartPDF(completePathToEmptyPages, ePDFVersion13, logConfiguration)` causes : `Segmentation fault (core dumped)` HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pdf-writer.