📄 dec.c
字号:
trans = &transformations; printf("\nReading transformations....."); fflush(stdout); partition_image(0, 0, hsize,vsize ); fclose(input); printf("Done."); fflush(stdout); if (scalefactor != 1.0) { printf("\nScaling image to %d x %d.", scaledhsize,scaledvsize); scale_transformations(scalefactor); } /* when we output the partition, we just read the transformations */ /* in and write them to the outputfile */ if (output_partition) { fprintf(output,"\n%d %d\n %d %d\n%d %d\n\n", 0, 0, scaledhsize, 0, scaledhsize, scaledvsize); printf("\nOutputed partition data in %s\n",outputfilename); fclose(output); return; } for (i=0; i<num_iterations; ++i) apply_transformations(); if (post_process) smooth_image(); i = fwrite(image[0], sizeof(IMAGE_TYPE), scaledhsize*scaledvsize, output); if (i < scaledhsize*scaledvsize) fatal("Couldn't write output... not enough disk space ?."); else printf("\n%d pixels written to output file.\n", i); fclose(output);}/* ************************************************************ *//* Read in the transformation data from *input. *//* This is a recursive routine whose recursion tree follows the *//* recursion done by the encoding program. *//* ************************************************************ */read_transformations(atx,aty,xsize,ysize,depth)int atx,aty,xsize,ysize,depth; { /* Having all these locals in a recursive procedure is hard on the */ /* stack.. but it is more readable. */ int i,j, sym_op, /* A symmetry operation of the square */ ialpha, /* Intgerized scalling factor */ ibeta; /* Intgerized offset */ long domain_ref; double alpha, beta; /* keep breaking it down until we are small enough */ if (depth < min_part) { read_transformations(atx,aty, xsize/2, ysize/2, depth+1); read_transformations(atx+xsize/2,aty, xsize/2, ysize/2, depth+1); read_transformations(atx,aty+ysize/2,xsize/2, ysize/2, depth+1); read_transformations(atx+xsize/2,aty+ysize/2,xsize/2,ysize/2,depth+1); return; } if (depth < max_part && unpack(1,input)) { /* A 1 means we subdivided.. so quadtree */ read_transformations(atx,aty, xsize/2, ysize/2, depth+1); read_transformations(atx+xsize/2,aty, xsize/2, ysize/2, depth+1); read_transformations(atx,aty+ysize/2, xsize/2, ysize/2, depth+1); read_transformations(atx+xsize/2,aty+ysize/2,xsize/2,ysize/2,depth+1); } else { /* we have a transformation to read */ trans->next = (struct transformation_node *) malloc(sizeof(struct transformation_node )); trans = trans->next; ialpha = (int)unpack(s_bits, input); ibeta = (int)unpack(o_bits, input); alpha = (double)ialpha/(double)(1<<s_bits)*(2.0*max_scale)-max_scale; beta = (double)ibeta/(double)((1<<o_bits)-1)* ((1.0+fabs(alpha))*GREY_LEVELS); if (alpha > 0.0) beta -= alpha*GREY_LEVELS; trans->scale = alpha; trans->offset = beta; if (ialpha != zero_ialpha) { trans-> sym_op = (int)unpack(3, input); domain_ref = unpack(bits_needed[depth-min_part], input); trans->dx = (double)(domain_ref % no_h_domains[depth-min_part]) * domain_hstep[depth-min_part]; trans->dy = (double)(domain_ref / no_h_domains[depth-min_part]) * domain_vstep[depth-min_part]; } else { trans-> sym_op = 0; trans-> dx = 0; trans-> dy = 0; } trans->rx = atx; trans->ry = aty; trans->depth = depth; trans->rrx = atx + xsize; trans->rry = aty + ysize; if (output_partition) fprintf(output,"\n%d %d\n %d %d\n%d %d\n\n", atx, vsize-aty-ysize, atx, vsize-aty, atx+xsize, vsize-aty); }} /* ************************************************************ *//* Apply the transformations once to an initially black image. *//* ************************************************************ */apply_transformations(){ IMAGE_TYPE **tempimage; int i,j,i1,j1,count=0; double pixel; trans = &transformations; while (trans->next != NULL) { trans = trans->next; ++count;/* Since the inner loop is the same in each case of the switch below *//* we just define it once for easy modification. */#define COMPUTE_LOOP { \ pixel = (image[j1][i1]+image[j1][i1+1]+image[j1+1][i1]+ \ image[j1+1][i1+1])/4.0; \ image1[j][i] = bound(0.5 + trans->scale*pixel+trans->offset);\ } switch(trans->sym_op) { case 0: for (i=trans->rx, i1 = trans->dx; i<trans->rrx; ++i, i1 += 2) for (j=trans->ry, j1 = trans->dy; j<trans->rry; ++j, j1 += 2) COMPUTE_LOOP break; case 1: for (j=trans->ry, i1 = trans->dx; j<trans->rry; ++j, i1 += 2) for (i=trans->rrx-1, j1 = trans->dy; i>=(int)trans->rx; --i, j1 += 2) COMPUTE_LOOP break; case 2: for (i=trans->rrx-1, i1 = trans->dx; i>=(int)trans->rx; --i, i1 += 2) for (j=trans->rry-1, j1 = trans->dy; j>=(int)trans->ry; --j, j1 += 2) COMPUTE_LOOP break; case 3: for (j=trans->rry-1, i1 = trans->dx; j>=(int)trans->ry; --j, i1 += 2) for (i=trans->rx, j1 = trans->dy; i<trans->rrx; ++i, j1 += 2) COMPUTE_LOOP break; case 4: for (j=trans->ry, i1 = trans->dx; j<trans->rry; ++j, i1 += 2) for (i=trans->rx, j1 = trans->dy; i<trans->rrx; ++i, j1 += 2) COMPUTE_LOOP break; case 5: for (i=trans->rx, i1 = trans->dx; i<trans->rrx; ++i, i1 += 2) for (j=trans->rry-1, j1 = trans->dy; j>=(int)trans->ry; --j, j1 += 2) COMPUTE_LOOP break; case 6: for (j=trans->rry-1, i1 = trans->dx; j>=(int)trans->ry; --j, i1 += 2) for (i=trans->rrx-1, j1 = trans->dy; i>=(int)trans->rx; --i, j1 += 2) COMPUTE_LOOP break; case 7: for (i=trans->rrx-1, i1 = trans->dx; i>=(int)trans->rx; --i, i1 += 2) for (j=trans->ry, j1 = trans->dy; j<trans->rry; ++j, j1 += 2) COMPUTE_LOOP break; } } tempimage = image; image = image1; image1 = tempimage; printf("\n%d transformations applied.",count);}/* This should really be done when they are read in. *//* ************************************************************ */scale_transformations(scalefactor)double scalefactor;{ trans = &transformations; while (trans->next != NULL) { trans = trans->next; trans->rrx *= scalefactor; trans->rry *= scalefactor; trans->rx *= scalefactor; trans->ry *= scalefactor; trans->dx *= scalefactor; trans->dy *= scalefactor; }}/* ************************************************************* *//* Recursively partition an image, finding the largest contained *//* square and call read_transformations . *//* ************************************************************* */partition_image(atx, aty, hsize,vsize )int atx, aty, hsize,vsize;{ int x_exponent, /* the largest power of 2 image size that fits */ y_exponent, /* horizontally or vertically the rectangle. */ exponent, /* The actual size of image that's encoded. */ size, depth; x_exponent = (int)floor(log((double)hsize)/log(2.0)); y_exponent = (int)floor(log((double)vsize)/log(2.0)); /* exponent is min of x_ and y_ exponent */ exponent = (x_exponent > y_exponent ? y_exponent : x_exponent); size = 1<<exponent; depth = max_exponent - exponent; read_transformations(atx,aty,size,size,depth); if (size != hsize) partition_image(atx+size, aty, hsize-size,vsize ); if (size != vsize) partition_image(atx, aty+size, size,vsize-size );} /* ************************************************************* *//* Scan the image and average the transformation boundaries. *//* ************************************************************* */smooth_image(){ IMAGE_TYPE pixel1, pixel2; int i,j; int w1,w2; printf("\nPostprocessing Image."); trans = &transformations; while (trans->next != NULL) { trans = trans->next; if (trans->rx == 0 || trans->ry == 0) continue; if (trans->depth == max_part) { w1 = 5; w2 = 1; } else { w1 = 2; w2 = 1; } for (i=trans->rx; i<trans->rrx; ++i) { pixel1 = image[(int)trans->ry][i]; pixel2 = image[(int)trans->ry-1][i]; image[(int)trans->ry][i] = (w1*pixel1 + w2*pixel2)/(w1+w2); image[(int)trans->ry-1][i] = (w2*pixel1 + w1*pixel2)/(w1+w2); } for (j=trans->ry; j<trans->rry; ++j) { pixel1 = image[j][(int)trans->rx]; pixel2 = image[j][(int)trans->rx-1]; image[j][(int)trans->rx] = (w1*pixel1 + w2*pixel2)/(w1+w2); image[j][(int)trans->rx-1] = (w2*pixel1 + w1*pixel2)/(w1+w2); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -