From b4bc0eb518ce74ffbfdbe0ce1a14bb404804cd57 Mon Sep 17 00:00:00 2001 From: "m. allan noah" Date: Fri, 6 Nov 2015 11:46:17 -0500 Subject: [PATCH] canon_dr backend v53 replace image processing methods with sanei_magic --- backend/Makefile.am | 2 +- backend/Makefile.in | 2 +- backend/canon_dr.c | 1234 +++---------------------------------------- backend/canon_dr.h | 35 +- 4 files changed, 89 insertions(+), 1184 deletions(-) diff --git a/backend/Makefile.am b/backend/Makefile.am index 112bff20d..979b5beb4 100644 --- a/backend/Makefile.am +++ b/backend/Makefile.am @@ -352,7 +352,7 @@ libcanon_dr_la_CPPFLAGS = $(AM_CPPFLAGS) -DBACKEND_NAME=canon_dr nodist_libsane_canon_dr_la_SOURCES = canon_dr-s.c libsane_canon_dr_la_CPPFLAGS = $(AM_CPPFLAGS) -DBACKEND_NAME=canon_dr libsane_canon_dr_la_LDFLAGS = $(DIST_SANELIBS_LDFLAGS) -libsane_canon_dr_la_LIBADD = $(COMMON_LIBS) libcanon_dr.la ../sanei/sanei_init_debug.lo ../sanei/sanei_constrain_value.lo ../sanei/sanei_config.lo ../sanei/sanei_config2.lo sane_strstatus.lo ../sanei/sanei_usb.lo ../sanei/sanei_scsi.lo $(MATH_LIB) $(SCSI_LIBS) $(USB_LIBS) $(RESMGR_LIBS) +libsane_canon_dr_la_LIBADD = $(COMMON_LIBS) libcanon_dr.la ../sanei/sanei_init_debug.lo ../sanei/sanei_constrain_value.lo ../sanei/sanei_config.lo ../sanei/sanei_config2.lo sane_strstatus.lo ../sanei/sanei_usb.lo ../sanei/sanei_scsi.lo ../sanei/sanei_magic.lo $(MATH_LIB) $(SCSI_LIBS) $(USB_LIBS) $(RESMGR_LIBS) EXTRA_DIST += canon_dr.conf.in libcanon_pp_la_SOURCES = canon_pp.c canon_pp.h canon_pp-io.c canon_pp-io.h canon_pp-dev.c canon_pp-dev.h diff --git a/backend/Makefile.in b/backend/Makefile.in index 7e1082f9c..34e08e521 100644 --- a/backend/Makefile.in +++ b/backend/Makefile.in @@ -2214,7 +2214,7 @@ libcanon_dr_la_CPPFLAGS = $(AM_CPPFLAGS) -DBACKEND_NAME=canon_dr nodist_libsane_canon_dr_la_SOURCES = canon_dr-s.c libsane_canon_dr_la_CPPFLAGS = $(AM_CPPFLAGS) -DBACKEND_NAME=canon_dr libsane_canon_dr_la_LDFLAGS = $(DIST_SANELIBS_LDFLAGS) -libsane_canon_dr_la_LIBADD = $(COMMON_LIBS) libcanon_dr.la ../sanei/sanei_init_debug.lo ../sanei/sanei_constrain_value.lo ../sanei/sanei_config.lo ../sanei/sanei_config2.lo sane_strstatus.lo ../sanei/sanei_usb.lo ../sanei/sanei_scsi.lo $(MATH_LIB) $(SCSI_LIBS) $(USB_LIBS) $(RESMGR_LIBS) +libsane_canon_dr_la_LIBADD = $(COMMON_LIBS) libcanon_dr.la ../sanei/sanei_init_debug.lo ../sanei/sanei_constrain_value.lo ../sanei/sanei_config.lo ../sanei/sanei_config2.lo sane_strstatus.lo ../sanei/sanei_usb.lo ../sanei/sanei_scsi.lo ../sanei/sanei_magic.lo $(MATH_LIB) $(SCSI_LIBS) $(USB_LIBS) $(RESMGR_LIBS) libcanon_pp_la_SOURCES = canon_pp.c canon_pp.h canon_pp-io.c canon_pp-io.h canon_pp-dev.c canon_pp-dev.h libcanon_pp_la_CPPFLAGS = $(AM_CPPFLAGS) -DBACKEND_NAME=canon_pp nodist_libsane_canon_pp_la_SOURCES = canon_pp-s.c diff --git a/backend/canon_dr.c b/backend/canon_dr.c index e85cbb696..8839731be 100644 --- a/backend/canon_dr.c +++ b/backend/canon_dr.c @@ -322,6 +322,8 @@ - add must_downsample and must_fully_buffer - improve dropout option handling - add software dropout implementation for downsampled modes + v53 2015-11-06, MAN + - replace image processing methods with sanei_magic SANE FLOW DIAGRAM @@ -366,12 +368,13 @@ #include "../include/sane/sanei_usb.h" #include "../include/sane/saneopts.h" #include "../include/sane/sanei_config.h" +#include "../include/sane/sanei_magic.h" #include "canon_dr-cmd.h" #include "canon_dr.h" #define DEBUG 1 -#define BUILD 52 +#define BUILD 53 /* values for SANE_DEBUG_CANON_DR env var: - errors 5 @@ -7397,102 +7400,44 @@ buffer_deskew(struct scanner *s, int side) { SANE_Status ret = SANE_STATUS_GOOD; - int pwidth = s->i.width; - int width = s->i.Bpl; - int height = s->i.height; - - double TSlope = 0; - int TXInter = 0; - int TYInter = 0; - double TSlopeHalf = 0; - int TOffsetHalf = 0; - - double LSlope = 0; - int LXInter = 0; - int LYInter = 0; - double LSlopeHalf = 0; - int LOffsetHalf = 0; - - int rotateX = 0; - int rotateY = 0; - - int * topBuf = NULL, * botBuf = NULL; + int bg_color = s->lut[s->bg_color]; DBG (10, "buffer_deskew: start\n"); - /* get buffers for edge detection */ - topBuf = getTransitionsY(s,side,1); - if(!topBuf){ - DBG (5, "buffer_deskew: cant gTY\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; - } + /* tweak the bg color based on scanner settings */ + if(s->u.mode == MODE_HALFTONE || s->u.mode == MODE_LINEART) + bg_color = (bg_colorthreshold)?0xff:0x00; - if(0){ - int i; - for(i=0;i=0 && topBuf[i] < height) - s->buffers[side][topBuf[i]*width+i] = 0; + ret = sane_get_parameters((SANE_Handle) s, &s->s_params); + + /*only find skew on first image from a page, or if first image had error */ + if(s->side == SIDE_FRONT || s->u.source == SOURCE_ADF_BACK || s->deskew_stat){ + + s->deskew_stat = sanei_magic_findSkew( + &s->s_params,s->buffers[side],s->u.dpi_x,s->u.dpi_y, + &s->deskew_vals[0],&s->deskew_vals[1],&s->deskew_slope); + + if(s->deskew_stat){ + DBG (5, "buffer_deskew: bad findSkew, bailing\n"); + goto cleanup; } } - - botBuf = getTransitionsY(s,side,0); - if(!botBuf){ - DBG (5, "buffer_deskew: cant gTY\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; + /* backside images can use a 'flipped' version of frontside data */ + else{ + s->deskew_slope *= -1; + s->deskew_vals[0] = s->s_params.pixels_per_line - s->deskew_vals[0]; } - /* find best top line */ - ret = getEdgeIterate (pwidth, height, s->i.dpi_y, topBuf, - &TSlope, &TXInter, &TYInter); + ret = sanei_magic_rotate(&s->s_params,s->buffers[side], + s->deskew_vals[0],s->deskew_vals[1],s->deskew_slope,bg_color); + if(ret){ - DBG(5,"buffer_deskew: gEI error: %d",ret); - goto cleanup; - } - DBG(15,"top: %04.04f %d %d\n",TSlope,TXInter,TYInter); - - /* slope is too shallow, don't want to divide by 0 */ - if(fabs(TSlope) < 0.0001){ - DBG(15,"buffer_deskew: slope too shallow: %0.08f\n",TSlope); - goto cleanup; - } - - /* find best left line, perpendicular to top line */ - LSlope = (double)-1/TSlope; - ret = getEdgeSlope (pwidth, height, topBuf, botBuf, LSlope, - &LXInter, &LYInter); - if(ret){ - DBG(5,"buffer_deskew: gES error: %d",ret); - goto cleanup; - } - DBG(15,"buffer_deskew: left: %04.04f %d %d\n",LSlope,LXInter,LYInter); - - /* find point about which to rotate */ - TSlopeHalf = tan(atan(TSlope)/2); - TOffsetHalf = LYInter; - DBG(15,"buffer_deskew: top half: %04.04f %d\n",TSlopeHalf,TOffsetHalf); - - LSlopeHalf = tan((atan(LSlope) + ((LSlope < 0)?-M_PI_2:M_PI_2))/2); - LOffsetHalf = - LSlopeHalf * TXInter; - DBG(15,"buffer_deskew: left half: %04.04f %d\n",LSlopeHalf,LOffsetHalf); - - rotateX = (LOffsetHalf-TOffsetHalf) / (TSlopeHalf-LSlopeHalf); - rotateY = TSlopeHalf * rotateX + TOffsetHalf; - DBG(15,"buffer_deskew: rotate: %d %d\n",rotateX,rotateY); - - ret = rotateOnCenter (s, side, rotateX, rotateY, TSlope); - if(ret){ - DBG(5,"buffer_deskew: gES error: %d",ret); + DBG(5,"buffer_deskew: rotate error: %d",ret); + ret = SANE_STATUS_GOOD; goto cleanup; } cleanup: - if(topBuf) - free(topBuf); - if(botBuf) - free(botBuf); - DBG (10, "buffer_deskew: finish\n"); return ret; } @@ -7505,173 +7450,50 @@ buffer_crop(struct scanner *s, int side) { SANE_Status ret = SANE_STATUS_GOOD; - int bwidth = s->i.Bpl; - int width = s->i.width; - int height = s->i.height; - - int top = 0; - int bot = 0; - int left = width; - int right = 0; - - int * topBuf = NULL, * botBuf = NULL; - int * leftBuf = NULL, * rightBuf = NULL; - int leftCount = 0, rightCount = 0, botCount = 0; - int i; - DBG (10, "buffer_crop: start\n"); - /* get buffers to find sides and bottom */ - topBuf = getTransitionsY(s,side,1); - if(!topBuf){ - DBG (5, "buffer_crop: no topBuf\n"); - ret = SANE_STATUS_NO_MEM; + ret = sane_get_parameters((SANE_Handle) s, &s->s_params); + + ret = sanei_magic_findEdges( + &s->s_params,s->buffers[side],s->u.dpi_x,s->u.dpi_y, + &s->crop_vals[0],&s->crop_vals[1],&s->crop_vals[2],&s->crop_vals[3]); + + if(ret){ + DBG (5, "buffer_crop: bad edges, bailing\n"); + ret = SANE_STATUS_GOOD; goto cleanup; } - botBuf = getTransitionsY(s,side,0); - if(!botBuf){ - DBG (5, "buffer_crop: no botBuf\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; + DBG (15, "buffer_crop: t:%d b:%d l:%d r:%d\n", + s->crop_vals[0],s->crop_vals[1],s->crop_vals[2],s->crop_vals[3]); + + /* if we will later binarize this image, make sure the width + * is a multiple of 8 pixels, by adjusting the right side */ + if ( must_downsample(s) && s->u.mode < MODE_GRAYSCALE ){ + s->crop_vals[3] -= (s->crop_vals[3]-s->crop_vals[2]) % 8; } - leftBuf = getTransitionsX(s,side,1); - if(!leftBuf){ - DBG (5, "buffer_crop: no leftBuf\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; - } - - rightBuf = getTransitionsX(s,side,0); - if(!rightBuf){ - DBG (5, "buffer_crop: no rightBuf\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; - } - - /* loop thru top and bottom lists, look for l and r extremes */ - for(i=0; i topBuf[i]){ - if(left > i){ - left = i; - } - - leftCount++; - if(leftCount > 3){ - break; - } - } - else{ - leftCount = 0; - left = width; - } - } - - for(i=width-1; i>=0; i--){ - if(botBuf[i] > topBuf[i]){ - if(right < i){ - right = i; - } - - rightCount++; - if(rightCount > 3){ - break; - } - } - else{ - rightCount = 0; - right = -1; - } - } - - /* loop thru left and right lists, look for bottom extreme */ - for(i=height-1; i>=0; i--){ - if(rightBuf[i] > leftBuf[i]){ - if(bot < i){ - bot = i; - } - - botCount++; - if(botCount > 3){ - break; - } - } - else{ - botCount = 0; - bot = -1; - } - } - - DBG (15, "buffer_crop: t:%d b:%d l:%d r:%d\n",top,bot,left,right); - /* now crop the image */ - /*FIXME: crop duplex backside at same time?*/ - if(left < right && top < bot){ + ret = sanei_magic_crop(&s->s_params,s->buffers[side], + s->crop_vals[0],s->crop_vals[1],s->crop_vals[2],s->crop_vals[3]); - int pixels = 0; - int bytes = 0; - unsigned char * line = NULL; - - /*convert left and right to bytes, figure new byte and pixel width */ - switch (s->i.mode) { - - case MODE_COLOR: - pixels = right-left; - bytes = pixels * 3; - left *= 3; - right *= 3; - break; - - case MODE_GRAYSCALE: - pixels = right-left; - bytes = right-left; - break; - - case MODE_LINEART: - case MODE_HALFTONE: - left /= 8; - right = (right+7)/8; - bytes = right-left; - pixels = bytes * 8; - break; - } - - DBG (15, "buffer_crop: l:%d r:%d p:%d b:%d\n",left,right,pixels,bytes); - - line = malloc(bytes); - if(!line){ - DBG (5, "buffer_crop: no line\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; - } - - s->i.bytes_sent[side] = 0; - - for(i=top; ibuffers[side] + i*bwidth + left, bytes); - memcpy(s->buffers[side] + s->i.bytes_sent[side], line, bytes); - s->i.bytes_sent[side] += bytes; - } - - s->i.bytes_tot[side] = s->i.bytes_sent[side]; - s->i.width = pixels; - s->i.height = bot-top; - s->i.Bpl = bytes; - - free(line); + if(ret){ + DBG (5, "buffer_crop: bad crop, bailing\n"); + ret = SANE_STATUS_GOOD; + goto cleanup; } - cleanup: - if(topBuf) - free(topBuf); - if(botBuf) - free(botBuf); - if(leftBuf) - free(leftBuf); - if(rightBuf) - free(rightBuf); - + /* need to update user with new size */ + s->i.width = s->s_params.pixels_per_line; + s->i.height = s->s_params.lines; + s->i.Bpl = s->s_params.bytes_per_line; + + /* update image size counter to new, smaller size */ + s->i.bytes_tot[side] = s->s_params.lines * s->s_params.bytes_per_line; + s->i.bytes_sent[side] = s->i.bytes_tot[side]; + s->u.bytes_sent[side] = 0; + + cleanup: DBG (10, "buffer_crop: finish\n"); return ret; } @@ -7683,941 +7505,23 @@ static SANE_Status buffer_despeck(struct scanner *s, int side) { SANE_Status ret = SANE_STATUS_GOOD; - int i,j,k,l,n; - int w = s->i.Bpl; - int pw = s->i.width; - int h = s->i.height; - int t = w*h; - int d = s->swdespeck; DBG (10, "buffer_despeck: start\n"); - switch (s->i.mode){ + ret = sane_get_parameters((SANE_Handle) s, &s->s_params); - case MODE_COLOR: - for(i=w; ibuffers[side][i + j*3 + k*w + l*3 + n]; - } - - if(tmp < thresh) - thresh = tmp; - } - } - - thresh = (thresh + 255*3 + 255*3)/3; - - /*loop over rows and columns around window */ - for(k=-1; kbuffers[side][i + j*3 + k*w + l*3 + n]; - outer[n] += tmp[n]; - } - if(tmp[0]+tmp[1]+tmp[2] < thresh){ - hits++; - break; - } - } - } - - for(n=0; n<3; n++){ - outer[n] /= (4*d + 4); - } - - /*no hits, overwrite with avg surrounding color*/ - if(!hits){ - for(k=0; kbuffers[side][i + j*3 + k*w + l*3 + n] = outer[n]; - } - } - } - } - - } - } - break; - - case MODE_GRAYSCALE: - for(i=w; ibuffers[side][i + j + k*w + l] < thresh) - thresh = s->buffers[side][i + j + k*w + l]; - } - } - - thresh = (thresh + 255 + 255)/3; - - /*loop over rows and columns around window */ - for(k=-1; kbuffers[side][i + j + k*w + l]; - - if(tmp < thresh){ - hits++; - break; - } - - outer += tmp; - } - } - - outer /= (4*d + 4); - - /*no hits, overwrite with avg surrounding color*/ - if(!hits){ - for(k=0; kbuffers[side][i + j + k*w + l] = outer; - } - } - } - - } - } - break; - - case MODE_LINEART: - case MODE_HALFTONE: - for(i=w; ibuffers[side][i + k*w + (j+l)/8] >> (7-(j+l)%8) & 1; - } - } - - if(!curr) - continue; - - /*loop over rows and columns around window */ - for(k=-1; kbuffers[side][i + k*w + (j+l)/8] >> (7-(j+l)%8) & 1; - - if(hits) - break; - } - } - - /*no hits, overwrite with white*/ - if(!hits){ - for(k=0; kbuffers[side][i + k*w + (j+l)/8] &= ~(1 << (7-(j+l)%8)); - } - } - } - - } - } - break; - - default: - break; + ret = sanei_magic_despeck(&s->s_params,s->buffers[side],s->swdespeck); + if(ret){ + DBG (5, "buffer_despeck: bad despeck, bailing\n"); + ret = SANE_STATUS_GOOD; + goto cleanup; } + cleanup: DBG (10, "buffer_despeck: finish\n"); return ret; } -/* Loop thru the image width and look for first color change in each column. - * Return a malloc'd array. Caller is responsible for freeing. */ -int * -getTransitionsY (struct scanner *s, int side, int top) -{ - int * buff; - - int i, j, k; - int near, far; - int winLen = 9; - - int width = s->i.width; - int height = s->i.height; - int depth = 1; - - /* defaults for bottom-up */ - int firstLine = height-1; - int lastLine = -1; - int direction = -1; - - DBG (10, "getTransitionsY: start\n"); - - buff = calloc(width,sizeof(int)); - if(!buff){ - DBG (5, "getTransitionsY: no buff\n"); - return NULL; - } - - /* override for top-down */ - if(top){ - firstLine = 0; - lastLine = height; - direction = 1; - } - - /* load the buff array with y value for first color change from edge - * gray/color uses a different algo from binary/halftone */ - switch (s->i.mode) { - - case MODE_COLOR: - depth = 3; - - case MODE_GRAYSCALE: - - for(i=0; ibuffers[side][(firstLine*width+i) * depth + k]; - } - near *= winLen; - far = near; - - /* move windows, check delta */ - for(j=firstLine+direction; j!=lastLine; j+=direction){ - - int farLine = j-winLen*2*direction; - int nearLine = j-winLen*direction; - - if(farLine < 0 || farLine >= height){ - farLine = firstLine; - } - if(nearLine < 0 || nearLine >= height){ - nearLine = firstLine; - } - - for(k=0; kbuffers[side][(farLine*width+i)*depth+k]; - far += s->buffers[side][(nearLine*width+i)*depth+k]; - - near -= s->buffers[side][(nearLine*width+i)*depth+k]; - near += s->buffers[side][(j*width+i)*depth+k]; - } - - if(abs(near - far) > winLen*depth*9){ - buff[i] = j; - break; - } - } - } - break; - - case MODE_LINEART: - case MODE_HALFTONE: - for(i=0; ibuffers[side][(firstLine*width+i)/8] >> (7-(i%8)) & 1; - - /* move */ - for(j=firstLine+direction; j!=lastLine; j+=direction){ - if((s->buffers[side][(j*width+i)/8] >> (7-(i%8)) & 1) != near){ - buff[i] = j; - break; - } - } - } - break; - - } - - /* blast any stragglers with no neighbors within .5 inch */ - for(i=0;ii.dpi_y/2) - sum++; - } - if(sum < 2) - buff[i] = lastLine; - } - - DBG (10, "getTransitionsY: finish\n"); - - return buff; -} - -/* Loop thru the image height and look for first color change in each row. - * Return a malloc'd array. Caller is responsible for freeing. */ -int * -getTransitionsX (struct scanner *s, int side, int left) -{ - int * buff; - - int i, j, k; - int near, far; - int winLen = 9; - - int bwidth = s->i.Bpl; - int width = s->i.width; - int height = s->i.height; - int depth = 1; - - /* defaults for right-first */ - int firstCol = width-1; - int lastCol = -1; - int direction = -1; - - DBG (10, "getTransitionsX: start\n"); - - buff = calloc(height,sizeof(int)); - if(!buff){ - DBG (5, "getTransitionsY: no buff\n"); - return NULL; - } - - /* override for left-first*/ - if(left){ - firstCol = 0; - lastCol = width; - direction = 1; - } - - /* load the buff array with x value for first color change from edge - * gray/color uses a different algo from binary/halftone */ - switch (s->i.mode) { - - case MODE_COLOR: - depth = 3; - - case MODE_GRAYSCALE: - - for(i=0; ibuffers[side][i*bwidth + k]; - } - near *= winLen; - far = near; - - /* move windows, check delta */ - for(j=firstCol+direction; j!=lastCol; j+=direction){ - - int farCol = j-winLen*2*direction; - int nearCol = j-winLen*direction; - - if(farCol < 0 || farCol >= width){ - farCol = firstCol; - } - if(nearCol < 0 || nearCol >= width){ - nearCol = firstCol; - } - - for(k=0; kbuffers[side][i*bwidth + farCol*depth + k]; - far += s->buffers[side][i*bwidth + nearCol*depth + k]; - - near -= s->buffers[side][i*bwidth + nearCol*depth + k]; - near += s->buffers[side][i*bwidth + j*depth + k]; - } - - if(abs(near - far) > winLen*depth*9){ - buff[i] = j; - break; - } - } - } - break; - - case MODE_LINEART: - case MODE_HALFTONE: - for(i=0; ibuffers[side][i*bwidth + firstCol/8] >> (7-(firstCol%8)) & 1; - - /* move */ - for(j=firstCol+direction; j!=lastCol; j+=direction){ - if((s->buffers[side][i*bwidth + j/8] >> (7-(j%8)) & 1) != near){ - buff[i] = j; - break; - } - } - } - break; - - } - - /* blast any stragglers with no neighbors within .5 inch */ - for(i=0;ii.dpi_x/2) - sum++; - } - if(sum < 2) - buff[i] = lastCol; - } - - DBG (10, "getTransitionsX: finish\n"); - - return buff; -} - -/* Loop thru a getTransitions array, and use a simplified Hough transform - * to divide likely edges into a 2-d array of bins. Then weight each - * bin based on its angle and offset. Return the 'best' bin. */ -static SANE_Status -getLine (int height, int width, int * buff, - int slopes, double minSlope, double maxSlope, - int offsets, int minOffset, int maxOffset, - double * finSlope, int * finOffset, int * finDensity) -{ - SANE_Status ret = 0; - - int ** lines = NULL; - int i, j; - int rise, run; - double slope; - int offset; - int sIndex, oIndex; - int hWidth = width/2; - - double * slopeCenter = NULL; - int * slopeScale = NULL; - double * offsetCenter = NULL; - int * offsetScale = NULL; - - int maxDensity = 1; - double absMaxSlope = fabs(maxSlope); - double absMinSlope = fabs(minSlope); - int absMaxOffset = abs(maxOffset); - int absMinOffset = abs(minOffset); - - DBG(10,"getLine: start %+0.4f %+0.4f %d %d\n", - minSlope,maxSlope,minOffset,maxOffset); - - /*silence compiler*/ - height = height; - - if(absMaxSlope < absMinSlope) - absMaxSlope = absMinSlope; - - if(absMaxOffset < absMinOffset) - absMaxOffset = absMinOffset; - - /* build an array of pretty-print values for slope */ - slopeCenter = calloc(slopes,sizeof(double)); - if(!slopeCenter){ - DBG(5,"getLine: cant load slopeCenter\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; - } - - /* build an array of scaling factors for slope */ - slopeScale = calloc(slopes,sizeof(int)); - if(!slopeScale){ - DBG(5,"getLine: cant load slopeScale\n"); - ret = SANE_STATUS_NO_MEM; - goto cleanup; - } - - for(j=0;j= maxSlope || slope < minSlope) - continue; - - /* offset in center of width, not y intercept! */ - offset = slope * hWidth + buff[i] - slope * i; - if(offset >= maxOffset || offset < minOffset) - continue; - - sIndex = (slope - minSlope) * slopes/(maxSlope-minSlope); - if(sIndex >= slopes) - continue; - - oIndex = (offset - minOffset) * offsets/(maxOffset-minOffset); - if(oIndex >= offsets) - continue; - - lines[sIndex][oIndex]++; - } - } - - /* go thru array, and find most dense line (highest number) */ - for(i=0;i maxDensity) - maxDensity = lines[i][j]; - } - } - - DBG(15,"getLine: maxDensity %d\n",maxDensity); - - *finSlope = 0; - *finOffset = 0; - *finDensity = 0; - - /* go thru array, and scale densities to % of maximum, plus adjust for - * prefered (smaller absolute value) slope and offset */ - for(i=0;i *finDensity){ - *finDensity = lines[i][j]; - *finSlope = slopeCenter[i]; - *finOffset = offsetCenter[j]; - } - } - } - - if(0){ - DBG(15,"offsetCenter: "); - for(j=0;j topDensity){ - topSlope = slope; - topOffset = offset; - topDensity = density; - } - } - } - - DBG(15,"getEdgeIterate: ok %+0.4f %d %d\n",topSlope,topOffset,topDensity); - - /* did not find anything promising on first pass, - * give up instead of fixating on some small, pointless feature */ - if(pass == 1 && topDensity < width/5){ - DBG(5,"getEdgeIterate: density too small %d %d\n",topDensity,width); - topOffset = 0; - topSlope = 0; - break; - } - - /* if slope can zoom in some more, do so. */ - if(sStep >= 0.0001){ - minSlope = topSlope - sStep; - maxSlope = topSlope + sStep; - go = 1; - } - - /* if offset can zoom in some more, do so. */ - if(oStep){ - minOffset = topOffset - oStep; - maxOffset = topOffset + oStep; - go = 1; - } - - /* cannot zoom in more, bail out */ - if(!go){ - break; - } - - DBG(15,"getEdgeIterate: zoom: %+0.4f %+0.4f %d %d\n", - minSlope,maxSlope,minOffset,maxOffset); - } - - /* topOffset is in the center of the image, - * convert to x and y intercept */ - if(topSlope != 0){ - *finYInter = topOffset - topSlope * width/2; - *finXInter = *finYInter / -topSlope; - *finSlope = topSlope; - } - else{ - *finYInter = 0; - *finXInter = 0; - *finSlope = 0; - } - - DBG(10,"getEdgeIterate: finish\n"); - - return 0; -} - -/* find the left side of paper by moving a line - * perpendicular to top slope across the image - * the 'left-most' point on the paper is the - * one with the smallest X intercept - * return x and y intercepts */ -SANE_Status -getEdgeSlope (int width, int height, int * top, int * bot, - double slope, int * finXInter, int * finYInter) -{ - - int i; - int topXInter, topYInter; - int botXInter, botYInter; - int leftCount; - - DBG(10,"getEdgeSlope: start\n"); - - topXInter = width; - topYInter = 0; - leftCount = 0; - - for(i=0;i txi){ - topXInter = txi; - topYInter = tyi; - } - - leftCount++; - if(leftCount > 5){ - break; - } - } - else{ - topXInter = width; - topYInter = 0; - leftCount = 0; - } - } - - botXInter = width; - botYInter = 0; - leftCount = 0; - - for(i=0;i -1){ - - int byi = bot[i] - (slope * i); - int bxi = byi/-slope; - - if(botXInter > bxi){ - botXInter = bxi; - botYInter = byi; - } - - leftCount++; - if(leftCount > 5){ - break; - } - } - else{ - botXInter = width; - botYInter = 0; - leftCount = 0; - } - } - - if(botXInter < topXInter){ - *finXInter = botXInter; - *finYInter = botYInter; - } - else{ - *finXInter = topXInter; - *finYInter = topYInter; - } - - DBG(10,"getEdgeSlope: finish\n"); - - return 0; -} - -/* function to do a simple rotation by a given slope, around - * a given point. The point can be outside of image to get - * proper edge alignment. Unused areas filled with bg color - * FIXME: Do in-place rotation to save memory */ -SANE_Status -rotateOnCenter (struct scanner *s, int side, - int centerX, int centerY, double slope) -{ - double slopeRad = -atan(slope); - double slopeSin = sin(slopeRad); - double slopeCos = cos(slopeRad); - - int bwidth = s->i.Bpl; - int pwidth = s->i.width; - int height = s->i.height; - int depth = 1; - int bg_color = s->lut[s->bg_color]; - - unsigned char * outbuf; - int i, j, k; - - DBG(10,"rotateOnCenter: start: %d %d\n",centerX,centerY); - - outbuf = malloc(s->i.bytes_tot[side]); - if(!outbuf){ - DBG(15,"rotateOnCenter: no outbuf\n"); - return SANE_STATUS_NO_MEM; - } - - switch (s->i.mode){ - - case MODE_COLOR: - depth = 3; - - case MODE_GRAYSCALE: - memset(outbuf,bg_color,s->i.bytes_tot[side]); - - for (i=0; i= pwidth) - continue; - - sourceY = centerY + (int)(-shiftY * slopeCos + shiftX * slopeSin); - if (sourceY < 0 || sourceY >= height) - continue; - - for (k=0; kbuffers[side][sourceY*bwidth+sourceX*depth+k]; - } - } - } - break; - - case MODE_LINEART: - case MODE_HALFTONE: - memset(outbuf,(bg_colorthreshold)?0xff:0x00,s->i.bytes_tot[side]); - - for (i=0; i= pwidth) - continue; - - sourceY = centerY + (int)(-shiftY * slopeCos + shiftX * slopeSin); - if (sourceY < 0 || sourceY >= height) - continue; - - /* wipe out old bit */ - outbuf[i*bwidth + j/8] &= ~(1 << (7-(j%8))); - - /* fill in new bit */ - outbuf[i*bwidth + j/8] |= - ((s->buffers[side][sourceY*bwidth + sourceX/8] - >> (7-(sourceX%8))) & 1) << (7-(j%8)); - } - } - break; - } - - memcpy(s->buffers[side],outbuf,s->i.bytes_tot[side]); - - free(outbuf); - - DBG(10,"rotateOnCenter: finish\n"); - - return 0; -} - /* certain options require the entire image to * be collected from the scanner before we can * tell the user the size of the image. */ diff --git a/backend/canon_dr.h b/backend/canon_dr.h index 761df87b6..1b47ed389 100644 --- a/backend/canon_dr.h +++ b/backend/canon_dr.h @@ -297,6 +297,24 @@ struct scanner /* the brightness/contrast LUT for dumb scanners */ unsigned char lut[256]; + /* --------------------------------------------------------------------- */ + /* values used by the software enhancment code (deskew, crop, etc) */ + SANE_Status deskew_stat; + int deskew_vals[2]; + double deskew_slope; + + int crop_vals[4]; + + /* this is defined in sane spec as a struct containing: + SANE_Frame format; + SANE_Bool last_frame; + SANE_Int lines; + SANE_Int depth; ( binary=1, gray=8, color=8 (!24) ) + SANE_Int pixels_per_line; + SANE_Int bytes_per_line; + */ + SANE_Parameters s_params; + /* --------------------------------------------------------------------- */ /* values which are set by calibration functions */ int c_res; @@ -570,23 +588,6 @@ static SANE_Status buffer_despeck(struct scanner *s, int side); static SANE_Status buffer_deskew(struct scanner *s, int side); static SANE_Status buffer_crop(struct scanner *s, int side); -int * getTransitionsY (struct scanner *s, int side, int top); -int * getTransitionsX (struct scanner *s, int side, int top); - -SANE_Status getEdgeIterate (int width, int height, int resolution, - int * buff, double * finSlope, int * finXInter, int * finYInter); - -SANE_Status getEdgeSlope (int width, int height, int * top, int * bot, - double slope, int * finXInter, int * finYInter); - -SANE_Status rotateOnCenter (struct scanner *s, int side, - int centerX, int centerY, double slope); - -static SANE_Status getLine (int height, int width, int * buff, - int slopes, double minSlope, double maxSlope, - int offsets, int minOffset, int maxOffset, - double * finSlope, int * finOffset, int * finDensity); - static SANE_Status load_lut (unsigned char * lut, int in_bits, int out_bits, int out_min, int out_max, int slope, int offset);