+static const char*STATE_NAME[] = {"parallel", "textabovebitmap", "bitmapabovetext"};
+
+int checkAlphaSanity(SplashBitmap*boolbtm, SplashBitmap*alphabtm)
+{
+ 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;
+}
+
+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;
+}
+
+static void update_bitmap(SplashBitmap*bitmap, SplashBitmap*update, int x1, int y1, int x2, int y2, char overwrite)
+{
+ assert(bitmap->getMode()==splashModeMono1);
+ assert(update->getMode()==splashModeMono1);
+
+ int width8 = (bitmap->getWidth()+7)/8;
+ assert(width8 == bitmap->getRowSize());
+ assert(width8 == update->getRowSize());
+ int height = bitmap->getHeight();
+ assert(height == update->getHeight());
+
+ if(!fixBBox(&x1, &y1, &x2, &y2, bitmap->getWidth(), bitmap->getHeight()))
+ return;
+
+ Guchar*b = bitmap->getDataPtr() + y1*width8 + x1/8;
+ Guchar*u = update->getDataPtr() + y1*width8 + x1/8;
+ int yspan = y2-y1;
+ int xspan = (x2+7)/8 - x1/8;
+ int size = (y2-y1)*width8;
+
+ if(overwrite) {
+ int y;
+ for(y=0;y<yspan;y++) {
+ memcpy(b, u, xspan);
+ b += width8;
+ u += width8;
+ }
+ } else {
+ int x,y;
+ for(y=0;y<yspan;y++) {
+ for(x=0;x<xspan;x++) {
+ b[x] |= u[x];
+ }
+ b += width8;
+ u += width8;
+ }
+ }
+}
+
+static void clearBooleanBitmap(SplashBitmap*btm, int x1, int y1, int x2, int y2)
+{
+ if(!fixBBox(&x1, &y1, &x2, &y2, btm->getWidth(), btm->getHeight()))
+ return;
+
+ if(btm->getMode()==splashModeMono1) {
+ int width8 = (btm->getWidth()+7)/8;
+ assert(width8 == btm->getRowSize());
+ int width = btm->getWidth();
+ int height = btm->getHeight();
+ Guchar*data = btm->getDataPtr();
+ memset(data+y1*width8, 0, width8*(y2-y1));
+ } else {
+ int width = btm->getAlphaRowSize();
+ int height = btm->getHeight();
+ memset(btm->getAlphaPtr(), 0, width*height);
+ }
+}
+
+void BitmapOutputDev::dbg_newdata(char*newdata)
+{
+ if(0) {
+ char filename1[80];
+ char filename2[80];
+ char filename3[80];
+ sprintf(filename1, "state%03dboolbitmap_after%s.png", dbg_btm_counter, newdata);
+ sprintf(filename2, "state%03dbooltext_after%s.png", dbg_btm_counter, newdata);
+ sprintf(filename3, "state%03dbitmap_after%s.png", dbg_btm_counter, newdata);
+ msg("<verbose> %s %s %s", filename1, filename2, filename3);
+ writeAlpha(stalepolybitmap, filename1);
+ writeAlpha(booltextbitmap, filename2);
+ writeBitmap(rgbdev->getBitmap(), filename3);
+ }
+ dbg_btm_counter++;
+}
+
+GBool BitmapOutputDev::checkNewText(int x1, int y1, int x2, int y2)