+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);
+
+ 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);
+
+ if(0) {
+ 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;
+//}
+
+GBool BitmapOutputDev::clip0and1differ(int x1,int y1,int x2,int y2)
+{
+ if(clip0bitmap->getMode()==splashModeMono1) {
+ if(x2<=x1)
+ return gFalse;
+ if(x2<0)
+ return gFalse;
+ if(x1<0)
+ x1 = 0;
+ if(x1>=clip0bitmap->getWidth())
+ return gFalse;
+ if(x2>clip0bitmap->getWidth())
+ x2=clip0bitmap->getWidth();
+
+ if(y2<=y1)
+ return gFalse;
+ if(y2<0)
+ return gFalse;
+ if(y1<0)
+ y1 = 0;
+ if(y1>=clip0bitmap->getHeight())
+ return gFalse;
+ if(y2>clip0bitmap->getHeight())
+ y2=clip0bitmap->getHeight();
+
+ SplashBitmap*clip0 = clip0bitmap;
+ SplashBitmap*clip1 = clip1bitmap;
+ int width8 = (clip0bitmap->getWidth()+7)/8;
+ int height = clip0bitmap->getHeight();
+ 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+x28];
+ 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();
+ return memcmp(clip0->getAlphaPtr(), clip1->getAlphaPtr(), width*height);
+ }
+}
+
+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);
+ }
+}
+
+long long unsigned int compare64(long long unsigned int*data1, long long unsigned int*data2, int len)
+{
+ long long unsigned int c;
+ int t;
+ for(t=0;t<len;t++) {
+ c |= data1[t]&data2[t];
+ }
+ return c;
+}
+
+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-
+ would work if xpdf wouldn't try to dither everything */
+
+ Guchar*polypixels = boolpoly->getDataPtr();
+ Guchar*textpixels = booltext->getDataPtr();
+
+ int width8 = (width+7)/8;
+ int height = boolpoly->getHeight();
+
+ if(x1|y1|x2|y2) {
+ if(y1>=0 && y1<=y2 && y2<=height) {
+ polypixels+=y1*width8;
+ textpixels+=y1*width8;
+ height=y2-y1;
+ }
+ }
+
+ int t;
+ int len = height*width8;
+ unsigned long long int c=0;
+ assert(sizeof(unsigned long long int)==8);
+ {
+ if(((ptroff_t)polypixels&7) || ((ptroff_t)textpixels&7)) {
+ //msg("<warning> Non-optimal alignment");
+ }
+ int l2 = len;
+ len /= sizeof(unsigned long long int);
+ c = compare64((unsigned long long int*)polypixels, (unsigned long long int*)textpixels, len);
+ int l1 = len*sizeof(unsigned long long int);
+ for(t=l1;t<l2;t++) {
+ c |= (unsigned long long int)(polypixels[t]&textpixels[t]);
+ }
+ }
+ if(c)
+ /* if graphic data and the characters overlap, they have common bits */
+ return gTrue;
+ else
+ return gFalse;
+ } else {
+ Guchar*polypixels = boolpoly->getAlphaPtr();
+ Guchar*textpixels = booltext->getAlphaPtr();
+
+ int width = boolpoly->getAlphaRowSize();
+ int height = boolpoly->getHeight();
+
+ int t;
+ int len = height*width;
+ unsigned int c=0;
+ if(len & (sizeof(unsigned int)-1)) {
+ Guchar c2=0;
+ for(t=0;t<len;t++) {
+ if(polypixels[t]&&textpixels[t])
+ return gTrue;
+ }
+ } else {
+ len /= sizeof(unsigned int);
+ for(t=0;t<len;t++) {
+ if((((unsigned int*)polypixels)[t]) & (((unsigned int*)textpixels)[t]))
+ return gTrue;
+ }
+ }
+ return gFalse;
+ }
+}
+
+