A 2-D DDA Algorithm for Fast Image Scaling
by Dean Clark

Listing One

/**************************************************************************
** ResizeDDA -- Reads an image from file 'f' one scan line at a time, 
** outputs the image resized by the 'zoom' factor.
*/
void ResizeDDA(FILE *f, int rows, int cols, double zoom)
{
    unsigned char   *aspline, *line;
    int             x, y;
    int             ddax, dday, izoom, i, k;
    extern int      getline(FILE *, int, unsigned char *);

    /* Calculate the differential amount */
    izoom = (int)(1.0/zoom * 1000);
    /* Allocate a buffer for a scan line from original image, and a
    ** resized scan line */
    aspline = (unsigned char *)getmem((t_size)cols);
    line    = (unsigned char *)getmem((t_size)(zoom * cols + 1));
    /* Initialize the output Y value and vertial differential */
    y = 0;
    dday = 0;
    /* Loop over rows in the original image */
    for (i = 0; i < rows; i++) {
        /* Get a scan line from the original image (8-bit values expected) */
        getline(f,cols,aspline);
        /* Adjust the vertical accumulated differential, initialize the
        ** output X pixel and horizontal accumulated differential */
        dday -= 1000;
        x    = 0;
        ddax = 0;
        /* Loop over pixels in the original image */
        for (j = 0; j < cols; j++) {
            /* Adjust the horizontal accumulated differential */
            ddax -= 1000;
            while (ddax < 0) {
                /* Store values from original image scanline into the scaled
                ** buffer until accumulated differential crosses threshold */
                line[x] = aspline[j];
                x++;
                ddax += izoom;
            }
        }
        while (dday < 0) {
            /* The 'outer loop' -- output resized scan lines until the
            ** vertical threshold is crossed */
            dday += izoom;
            for (k = 0; k < x; k++) {
                SetPixel(k, y, line[k]);
            }
            y++;
        }
    }
    free(aspline);
    free(line);
}

/************************************************************
** getmem - memory allocated
*/
void * getmem(size_t size)
{
    void * p;
    if (p = malloc(size)) {
        return p;
    }
    printf("Allocation of %old bytes failed \n size);
    exit(1):
}



2


