+ assert(boolbtm->getWidth() == alphabtm->getWidth());
+ assert(boolbtm->getHeight() == alphabtm->getHeight());
+ if(boolbtm->getMode()==splashModeMono1) {
+ return 1;
+ }
+
+ int width = boolbtm->getWidth();
+ int height = boolbtm->getHeight();
+
+ int bad=0;
+ int x,y;
+ for(y=0;y<height;y++) {
+ for(x=0;x<width;x++) {
+ int a1 = alphabtm->getAlpha(x,y);
+ int a2 = boolbtm->getAlpha(x,y);
+ if(a1!=a2) {
+ bad++;
+ }
+ }
+ }
+ double badness = bad/(double)(width*height);
+ if(badness>0.2) {
+ msg("<error> Bitmaps don't correspond: %d out of %d pixels wrong (%.2f%%)", bad, width*height,
+ badness*100.0);
+ return 0;
+ }
+ msg("<notice> %f", badness);
+ return 1;
+}
+
+GBool BitmapOutputDev::checkNewText(int x1, int y1, int x2, int y2)
+{
+ /* called once some new text was drawn on booltextdev, and
+ before the same thing is drawn on gfxdev */
+
+ msg("<trace> Testing new text data against current bitmap data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
+
+ if(0) {
+ char filename1[80];
+ char filename2[80];
+ char filename3[80];
+ sprintf(filename1, "state%dboolbitmap_afternewtext.png", dbg_btm_counter);
+ sprintf(filename2, "state%dbooltext_afternewtext.png", dbg_btm_counter);
+ sprintf(filename3, "state%dbitmap_afternewtext.png", dbg_btm_counter);
+ msg("<verbose> %s %s %s", filename1, filename2, filename3);
+ writeAlpha(boolpolybitmap, filename1);
+ writeAlpha(booltextbitmap, filename2);
+ writeBitmap(rgbdev->getBitmap(), filename3);
+ }
+ dbg_btm_counter++;
+
+ GBool ret = false;
+ if(intersection(x1,y1,x2,y2)) {
+ if(layerstate==STATE_PARALLEL) {
+ /* the new text is above the bitmap. So record that fact,
+ and also clear the bitmap buffer, so we can check for
+ new intersections */
+ msg("<verbose> Text is above current bitmap/polygon data");
+ layerstate=STATE_TEXT_IS_ABOVE;
+ clearBoolPolyDev(x1,y1,x2,y2);
+ } else if(layerstate==STATE_BITMAP_IS_ABOVE) {
+ /* there's a bitmap above the (old) text. So we need
+ to flush out that text, and record that the *new*
+ text is now *above* the bitmap
+ */
+ msg("<verbose> Text is above current bitmap/polygon data (which is above some other text)");
+ flushText();
+ layerstate=STATE_TEXT_IS_ABOVE;
+ /* clear both bool devices- the text device because
+ we just dumped out all the (old) text, and the
+ poly dev so we can check for new intersections */
+ clearBoolPolyDev(x1,y1,x2,y2);
+ /* FIXME this destroys the text pixels we just
+ drew to test for the intersection- however we need
+ those to check for the *new* intersections */
+ clearBoolTextDev(x1,y1,x2,y2);
+ ret = true;
+ } else {
+ /* we already know that the current text section is
+ above the current bitmap section- now just new
+ bitmap data *and* new text data was drawn, and
+ *again* it's above the current bitmap- so clear
+ the polygon bitmap again, so we can check for
+ new intersections */
+ msg("<verbose> Text is still above current bitmap/polygon data");
+ clearBoolPolyDev(x1,y1,x2,y2);
+ }
+ }
+ return ret;
+}
+
+GBool BitmapOutputDev::checkNewBitmap(int x1, int y1, int x2, int y2)
+{
+ /* similar to checkNewText() above, only in reverse */
+ msg("<trace> Testing new graphics data against current text data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
+
+ if(0) {
+ char filename1[80];
+ char filename2[80];
+ char filename3[80];
+ sprintf(filename1, "state%dboolbitmap_afternewgfx.png", dbg_btm_counter);
+ sprintf(filename2, "state%dbooltext_afternewgfx.png", dbg_btm_counter);
+ sprintf(filename3, "state%dbitmap_afternewgfx.png", dbg_btm_counter);
+ msg("<verbose> %s %s %s", filename1, filename2, filename3);
+ writeAlpha(boolpolybitmap, filename1);
+ writeAlpha(booltextbitmap, filename2);
+ writeBitmap(rgbdev->getBitmap(), filename3);
+ }
+ dbg_btm_counter++;
+
+ GBool ret = false;
+ if(intersection(x1,y1,x2,y2)) {
+ if(layerstate==STATE_PARALLEL) {
+ msg("<verbose> Bitmap is above current text data");
+ layerstate=STATE_BITMAP_IS_ABOVE;
+ clearBoolTextDev(x1,y1,x2,y2);
+ } else if(layerstate==STATE_TEXT_IS_ABOVE) {
+ msg("<verbose> Bitmap is above current text data (which is above some bitmap)");
+ flushBitmap();
+ layerstate=STATE_BITMAP_IS_ABOVE;
+ clearBoolTextDev(x1,y1,x2,y2);
+ /* FIXME this destroys the polygon pixels we just
+ drew to test for the intersection- however we need
+ those to check for the *new* intersections */
+ clearBoolPolyDev(x1,y1,x2,y2);
+ ret = true;
+ } else {
+ msg("<verbose> Bitmap is still above current text data");
+ clearBoolTextDev(x1,y1,x2,y2);
+ }
+ }
+ return ret;
+}
+
+//void checkNewText() {
+// Guchar*alpha = rgbbitmap->getAlphaPtr();
+// Guchar*charpixels = clip1bitmap->getDataPtr();
+// int xx,yy;
+// for(yy=0;yy<height;yy++) {
+// Guchar*aline = &alpha[yy*width];
+// Guchar*cline = &charpixels[yy*width8];
+// for(xx=0;xx<width;xx++) {
+// int bit = xx&7;
+// int bytepos = xx>>3;
+// /* TODO: is the bit order correct? */
+// if(aline[xx] && (cline[bytepos]&(1<<bit)))
+// break;
+// }
+// if(xx!=width)
+// break;
+//}
+
+static inline GBool fixBBox(int*x1, int*y1, int*x2, int*y2, int width, int height)
+{
+ if(!(*x1|*y1|*x2|*y2)) {
+ // undefined bbox
+ *x1 = *y1 = 0;
+ *x2 = width;
+ *y2 = height;
+ return gTrue;
+ }
+ if(*x2<=*x1) return gFalse;
+ if(*x2<0) return gFalse;
+ if(*x1<0) *x1 = 0;
+ if(*x1>=width) return gFalse;
+ if(*x2>width) *x2=width;
+
+ if(*y2<=*y1) return gFalse;
+ if(*y2<0) return gFalse;
+ if(*y1<0) *y1 = 0;
+ if(*y1>=height) return gFalse;
+ if(*y2>height) *y2=height;
+ return gTrue;
+}
+
+GBool BitmapOutputDev::clip0and1differ(int x1,int y1,int x2,int y2)
+{
+ if(clip0bitmap->getMode()==splashModeMono1) {
+ int width = clip0bitmap->getWidth();
+ int width8 = (width+7)/8;
+ int height = clip0bitmap->getHeight();
+
+ if(!fixBBox(&x1,&y1,&x2,&y2,width,height)) {
+ /* area is outside or null */
+ return gFalse;
+ }
+
+ SplashBitmap*clip0 = clip0bitmap;
+ SplashBitmap*clip1 = clip1bitmap;
+ int x18 = x1/8;
+ int x28 = (x2+7)/8;
+ int y;
+
+ for(y=y1;y<y2;y++) {
+ unsigned char*row1 = &clip0bitmap->getDataPtr()[width8*y+x18];
+ unsigned char*row2 = &clip1bitmap->getDataPtr()[width8*y+x18];
+ if(memcmp(row1, row2, x28-x18)) {
+ return gTrue;
+ }
+ }
+ return gFalse;
+ } else {
+ SplashBitmap*clip0 = clip0bitmap;
+ SplashBitmap*clip1 = clip1bitmap;
+ int width = clip0->getAlphaRowSize();
+ int height = clip0->getHeight();
+
+ if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
+ x1=y1=0;x2=y2=1;
+ }
+
+ Guchar*a0 = clip0->getAlphaPtr();
+ Guchar*a1 = clip1->getAlphaPtr();
+ int x,y;
+ char differs=0;
+ for(y=y1;y<y2;y++) {
+ for(x=x1;x<x2;x++) {
+ if(a0[y*width+x]!=a1[y*width+x]) {
+ differs=1;
+ break;
+ }
+ }
+ if(differs)
+ break;
+ }
+ char differs2 = memcmp(a0, a1, width*height);
+ if(differs && !differs2)
+ msg("<warning> Strange internal error (2)");
+ else if(!differs && differs2) {
+ msg("<warning> Bad Bounding Box: Difference in clip0 and clip1 outside bbox");
+ msg("<warning> %d %d %d %d", x1, y1, x2, y2);
+ }
+ return differs2;
+ }
+}
+
+static void clearBooleanBitmap(SplashBitmap*btm, int x1, int y1, int x2, int y2)
+{
+ if(!(x1|y1|x2|y2)) {
+ x1 = y1 = 0;
+ x2 = btm->getWidth();
+ y2 = btm->getHeight();
+ }
+ if(btm->getMode()==splashModeMono1) {
+ int width8 = (btm->getWidth()+7)/8;
+ int width = btm->getWidth();
+ int height = btm->getHeight();
+ memset(btm->getDataPtr(), 0, width8*height);
+ } else {
+ int width = btm->getAlphaRowSize();
+ int height = btm->getHeight();
+ memset(btm->getAlphaPtr(), 0, width*height);
+ }
+}
+
+GBool compare8(unsigned char*data1, unsigned char*data2, int len)
+{
+ if(!len)
+ return 0;
+ if(((ptroff_t)data1&7)==((ptroff_t)data2&7)) {
+ // oh good, we can do aligning
+ while((ptroff_t)data1&7) {
+ if(*data1&*data2)
+ return 1;
+ data1++;
+ data2++;
+ if(!--len)
+ return 0;
+ }
+ }
+ /* use 64 bit for the (hopefully aligned) middle section */
+ int l8 = len/8;
+ long long unsigned int*d1 = (long long unsigned int*)data1;
+ long long unsigned int*d2 = (long long unsigned int*)data2;
+ long long unsigned int x = 0;
+ int t;
+ for(t=0;t<l8;t++) {
+ x |= d1[t]&d2[t];
+ }
+ if(x)
+ return 1;
+
+ data1+=l8*8;
+ data2+=l8*8;
+ len -= l8*8;
+ for(t=0;t<len;t++) {
+ if(data1[t]&data2[t]) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+GBool BitmapOutputDev::intersection(int x1, int y1, int x2, int y2)
+{
+ SplashBitmap*boolpoly = boolpolybitmap;
+ SplashBitmap*booltext = booltextbitmap;
+
+ if(boolpoly->getMode()==splashModeMono1) {
+ /* alternative implementation, using one bit per pixel-
+ needs the no-dither patch in xpdf */
+
+ int width = boolpoly->getWidth();
+ int height = boolpoly->getHeight();
+
+ if(!fixBBox(&x1,&y1,&x2,&y2, width, height)) {
+ return gFalse;
+ }
+
+ Guchar*polypixels = boolpoly->getDataPtr();
+ Guchar*textpixels = booltext->getDataPtr();
+
+ int width8 = (width+7)/8;
+ int runx = width8;
+ int runy = height;
+
+ if(x1|y1|x2|y2) {
+ polypixels+=y1*width8+x1/8;
+ textpixels+=y1*width8+x1/8;
+ runx=(x2+7)/8 - x1/8;
+ runy=y2-y1;
+ }
+
+ int t;
+ unsigned char c=0;
+
+ /*assert(sizeof(unsigned long long int)==8);
+ if(((ptroff_t)polypixels&7) || ((ptroff_t)textpixels&7)) {
+ //msg("<warning> Non-optimal alignment");
+ }*/
+
+ int x,y;
+ unsigned char*data1 = (unsigned char*)polypixels;
+ unsigned char*data2 = (unsigned char*)textpixels;
+ msg("<verbose> Testing area (%d,%d,%d,%d), runx=%d,runy=%d", x1,y1,x2,y2, runx, runy);
+ for(y=0;y<runy;y++) {
+ if(compare8(data1,data2,runx))
+ return gTrue;
+ data1+=width8;
+ data2+=width8;
+ }
+ return gFalse;
+ } else {
+ int width = boolpoly->getAlphaRowSize();
+ int height = boolpoly->getHeight();
+
+ if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
+ x1=y1=0;x2=y2=1;
+ }
+ Guchar*polypixels = boolpoly->getAlphaPtr();
+ Guchar*textpixels = booltext->getAlphaPtr();
+
+ int x,y;
+ char overlap1 = 0;
+ char overlap2 = 0;
+ for(x=x1;x<x2;x++) {
+ for(y=y1;y<y2;y++) {
+ if(polypixels[width*y+x]&&textpixels[width*y+x])
+ overlap1 = 1;
+ }
+ }
+ int ax1=0,ay1=0,ax2=0,ay2=0;
+ for(y=0;y<height;y++) {
+ for(x=0;x<width;x++) {
+ if(polypixels[width*y+x]&&textpixels[width*y+x]) {
+ overlap2 = 1;
+ if(!(ax1|ay1|ax2|ay2)) {
+ ax1 = ax2 = x;
+ ay1 = ay2 = y;
+ } else {
+ ax1 = ax1<x?ax1:x;
+ ay1 = ay1<y?ay1:y;
+ ax2 = ax2>x?ax2:x;
+ ay2 = ay2>y?ay2:y;
+ }
+ }
+ }
+ }
+ if(overlap1 && !overlap2)
+ msg("<warning> strange internal error");
+ if(!overlap1 && overlap2) {
+ msg("<warning> Bad bounding box: intersection outside bbox");
+ msg("<warning> given bbox: %d %d %d %d", x1, y1, x2, y2);
+ msg("<warning> changed area: %d %d %d %d", ax1, ay1, ax2, ay2);
+ //writeAlpha(booltextbitmap, "alpha.png");
+ }
+ return overlap2;
+ }
+}
+
+
+void BitmapOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
+{
+ double x1,y1,x2,y2;
+ state->transform(crop_x1,crop_y1,&x1,&y1);
+ state->transform(crop_x2,crop_y2,&x2,&y2);
+ if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
+ if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
+
+ this->movex = -(int)x1 - user_movex;
+ this->movey = -(int)y1 - user_movey;
+
+ if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
+ x1 = user_clipx1;
+ x2 = user_clipx2;
+ y1 = user_clipy1;
+ y2 = user_clipy2;
+ }
+ this->width = (int)(x2-x1);
+ this->height = (int)(y2-y1);
+
+ msg("<debug> startPage");
+ rgbdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
+ boolpolydev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
+ booltextdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
+ clip0dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
+ clip1dev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
+ gfxdev->startPage(pageNum, state, crop_x1, crop_y1, crop_x2, crop_y2);
+
+ boolpolybitmap = boolpolydev->getBitmap();
+ booltextbitmap = booltextdev->getBitmap();
+ clip0bitmap = clip0dev->getBitmap();
+ clip1bitmap = clip1dev->getBitmap();
+ rgbbitmap = rgbdev->getBitmap();
+
+ flushText(); // write out the initial clipping rectangle
+
+ /* just in case any device did draw a white background rectangle
+ into the device */
+ clearBoolTextDev(UNKNOWN_BOUNDING_BOX);
+ clearBoolPolyDev(UNKNOWN_BOUNDING_BOX);
+
+ this->layerstate = STATE_PARALLEL;
+ this->emptypage = 1;
+ msg("<debug> startPage done");