Commits

Ondřej Surý committed e432fca

WS & CS

Comments (0)

Files changed (8)

src/gd_interpolation.c

 
 typedef struct
 {
-   double *Weights;  /* Normalized weights of neighboring pixels */
-   int Left,Right;   /* Bounds of source pixels window */
+	double *Weights;  /* Normalized weights of neighboring pixels */
+	int Left,Right;   /* Bounds of source pixels window */
 } ContributionType;  /* Contirbution information for a single pixel */
 
 typedef struct
 {
-   ContributionType *ContribRow; /* Row (or column) of contribution weights */
-   unsigned int WindowSize,      /* Filter window size (of affecting source pixels) */
-				LineLength;      /* Length of line (no. or rows / cols) */
+	ContributionType *ContribRow; /* Row (or column) of contribution weights */
+	unsigned int WindowSize,      /* Filter window size (of affecting source pixels) */
+		     LineLength;      /* Length of line (no. or rows / cols) */
 } LineContribType;
 
 /* Each core filter has its own radius */
 static double KernelBessel_Order1(double x)
 {
 	double p, q;
-
+	
 	if (x == 0.0)
 		return (0.0f);
 	p = x;
  */
 static double filter_bicubic(const double t)
 {
-  const double abs_t = (double)fabs(t);
-  const double abs_t_sq = abs_t * abs_t;
-  if (abs_t<1) return 1-2*abs_t_sq+abs_t_sq*abs_t;
-  if (abs_t<2) return 4 - 8*abs_t +5*abs_t_sq - abs_t_sq*abs_t;
-  return 0;
+	const double abs_t = (double)fabs(t);
+	const double abs_t_sq = abs_t * abs_t;
+	if (abs_t<1) return 1-2*abs_t_sq+abs_t_sq*abs_t;
+	if (abs_t<2) return 4 - 8*abs_t +5*abs_t_sq - abs_t_sq*abs_t;
+	return 0;
 }
 
 /**
 /* Copied from upstream's libgd */
 static inline int _color_blend (const int dst, const int src)
 {
-    const int src_alpha = gdTrueColorGetAlpha(src);
+	const int src_alpha = gdTrueColorGetAlpha(src);
 
-    if( src_alpha == gdAlphaOpaque ) {
+	if( src_alpha == gdAlphaOpaque ) {
 		return src;
 	} else {
 		const int dst_alpha = gdTrueColorGetAlpha(dst);
 static inline LineContribType * _gdContributionsAlloc(unsigned int line_length, unsigned int windows_size)
 {
 	unsigned int u = 0;
-    LineContribType *res;
+	LineContribType *res;
 
 	res = (LineContribType *) gdMalloc(sizeof(LineContribType));
 	if (!res) {
 		return NULL;
 	}
-    res->WindowSize = windows_size;
-    res->LineLength = line_length;
-    res->ContribRow = (ContributionType *) gdMalloc(line_length * sizeof(ContributionType));
+	res->WindowSize = windows_size;
+	res->LineLength = line_length;
+	res->ContribRow = (ContributionType *) gdMalloc(line_length * sizeof(ContributionType));
 
-    for (u = 0 ; u < line_length ; u++) {
-        res->ContribRow[u].Weights = (double *) gdMalloc(windows_size * sizeof(double));
-    }
-    return res;
+	for (u = 0 ; u < line_length ; u++) {
+		res->ContribRow[u].Weights = (double *) gdMalloc(windows_size * sizeof(double));
+	}
+	return res;
 }
 
 static inline void _gdContributionsFree(LineContribType * p)
 
 static inline LineContribType *_gdContributionsCalc(unsigned int line_size, unsigned int src_size, double scale_d,  const interpolation_method pFilter)
 {
-    double width_d;
-    double scale_f_d = 1.0;
-    const double filter_width_d = DEFAULT_BOX_RADIUS;
+	double width_d;
+	double scale_f_d = 1.0;
+	const double filter_width_d = DEFAULT_BOX_RADIUS;
 	int windows_size;
 	unsigned int u;
 	LineContribType *res;
 
-    if (scale_d < 1.0) {
-        width_d = filter_width_d / scale_d;
-        scale_f_d = scale_d;
-    }  else {
-        width_d= filter_width_d;
-    }
-
-    windows_size = 2 * (int)ceil(width_d) + 1;
-    res = _gdContributionsAlloc(line_size, windows_size);
-
-    for (u = 0; u < line_size; u++) {
-        const double dCenter = (double)u / scale_d;
-        /* get the significant edge points affecting the pixel */
-        register int iLeft = MAX(0, (int)floor (dCenter - width_d));
-        int iRight = MIN((int)ceil(dCenter + width_d), (int)src_size - 1);
-        double dTotalWeight = 0.0;
+	if (scale_d < 1.0) {
+		width_d = filter_width_d / scale_d;
+		scale_f_d = scale_d;
+	}  else {
+		width_d= filter_width_d;
+	}
+
+	windows_size = 2 * (int)ceil(width_d) + 1;
+	res = _gdContributionsAlloc(line_size, windows_size);
+
+	for (u = 0; u < line_size; u++) {
+		const double dCenter = (double)u / scale_d;
+		/* get the significant edge points affecting the pixel */
+		register int iLeft = MAX(0, (int)floor (dCenter - width_d));
+		int iRight = MIN((int)ceil(dCenter + width_d), (int)src_size - 1);
+		double dTotalWeight = 0.0;
 		int iSrc;
 
-        res->ContribRow[u].Left = iLeft;
-        res->ContribRow[u].Right = iRight;
+		res->ContribRow[u].Left = iLeft;
+		res->ContribRow[u].Right = iRight;
 
-        /* Cut edge points to fit in filter window in case of spill-off */
-        if (iRight - iLeft + 1 > windows_size)  {
-            if (iLeft < ((int)src_size - 1 / 2))  {
-                iLeft++;
-            } else {
-                iRight--;
-            }
-        }
+		/* Cut edge points to fit in filter window in case of spill-off */
+		if (iRight - iLeft + 1 > windows_size)  {
+			if (iLeft < ((int)src_size - 1 / 2))  {
+				iLeft++;
+			} else {
+				iRight--;
+			}
+		}
 
-        for (iSrc = iLeft; iSrc <= iRight; iSrc++) {
-            dTotalWeight += (res->ContribRow[u].Weights[iSrc-iLeft] =  scale_f_d * (*pFilter)(scale_f_d * (dCenter - (double)iSrc)));
-        }
+		for (iSrc = iLeft; iSrc <= iRight; iSrc++) {
+			dTotalWeight += (res->ContribRow[u].Weights[iSrc-iLeft] =  scale_f_d * (*pFilter)(scale_f_d * (dCenter - (double)iSrc)));
+		}
 
 		if (dTotalWeight < 0.0) {
 			_gdContributionsFree(res);
 			return NULL;
 		}
 
-        if (dTotalWeight > 0.0) {
-            for (iSrc = iLeft; iSrc <= iRight; iSrc++) {
-                res->ContribRow[u].Weights[iSrc-iLeft] /= dTotalWeight;
-            }
-        }
-   }
-   return res;
+		if (dTotalWeight > 0.0) {
+			for (iSrc = iLeft; iSrc <= iRight; iSrc++) {
+				res->ContribRow[u].Weights[iSrc-iLeft] /= dTotalWeight;
+			}
+		}
+	}
+	return res;
 }
 
 static inline void _gdScaleRow(gdImagePtr pSrc,  unsigned int src_width, gdImagePtr dst, unsigned int dst_width, unsigned int row, LineContribType *contrib)
 {
-    int *p_src_row = pSrc->tpixels[row];
-    int *p_dst_row = dst->tpixels[row];
+	int *p_src_row = pSrc->tpixels[row];
+	int *p_dst_row = dst->tpixels[row];
 	unsigned int x;
 
-    for (x = 0; x < dst_width - 1; x++) {
+	for (x = 0; x < dst_width - 1; x++) {
 		register unsigned char r = 0, g = 0, b = 0, a = 0;
-        const int left = contrib->ContribRow[x].Left;
-        const int right = contrib->ContribRow[x].Right;
+		const int left = contrib->ContribRow[x].Left;
+		const int right = contrib->ContribRow[x].Right;
 		int i;
 
 		/* Accumulate each channel */
-        for (i = left; i <= right; i++) {
+		for (i = left; i <= right; i++) {
 			const int left_channel = i - left;
-            r += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetRed(p_src_row[i])));
-            g += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetGreen(p_src_row[i])));
-            b += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetBlue(p_src_row[i])));
+			r += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetRed(p_src_row[i])));
+			g += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetGreen(p_src_row[i])));
+			b += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetBlue(p_src_row[i])));
 			a += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetAlpha(p_src_row[i])));
-        }
-        p_dst_row[x] = gdTrueColorAlpha(r, g, b, a);
-    }
+		}
+		p_dst_row[x] = gdTrueColorAlpha(r, g, b, a);
+	}
 }
 
 static inline void _gdScaleHoriz(gdImagePtr pSrc, unsigned int src_width, unsigned int src_height, gdImagePtr pDst,  unsigned int dst_width, unsigned int dst_height)
 	 * correct areas of file opened and modifieable by the gdIOCtx functions
 	 */
 	tiff = TIFFClientOpen("", "w", th,	tiff_readproc,
-	                      tiff_writeproc,
-	                      tiff_seekproc,
-	                      tiff_closeproc,
-	                      tiff_sizeproc,
-	                      tiff_mapproc,
-	                      tiff_unmapproc);
+			      tiff_writeproc,
+			      tiff_seekproc,
+			      tiff_closeproc,
+			      tiff_sizeproc,
+			      tiff_mapproc,
+			      tiff_unmapproc);
 
 	TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width);
 	TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height);
 	TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
 	TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 	TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC,
-	             (bitDepth == 24) ? PHOTOMETRIC_RGB : PHOTOMETRIC_PALETTE);
+		     (bitDepth == 24) ? PHOTOMETRIC_RGB : PHOTOMETRIC_PALETTE);
 
 	bitsPerSample = (bitDepth == 24 || bitDepth == 8) ? 8 : 1;
 	TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, bitsPerSample);
 		}
 
 		TIFFSetField(tiff, TIFFTAG_COLORMAP, colorMapRed, colorMapGreen,
-		             colorMapBlue);
+			     colorMapBlue);
 		samplesPerPixel = 1;
 	}
 
 	/* here, we check if the 'save alpha' flag is set on the source gd image */
-	if(	(bitDepth == 24) &&
-	        (image->saveAlphaFlag || image->transparent != -1)) {
+	if ((bitDepth == 24) &&
+	    (image->saveAlphaFlag || image->transparent != -1)) {
 		/* so, we need to store the alpha values too!
 		 * Also, tell TIFF what the extra sample means (associated alpha) */
 		samplesPerPixel = 4;
 
 			/* if this pixel has the same RGB as the transparent colour,
 			 * then set alpha fully transparent */
-			if(	transparentColorR == r &&
-			        transparentColorG == g &&
-			        transparentColorB == b
-			  ) {
+			if (transparentColorR == r &&
+			    transparentColorG == g &&
+			    transparentColorB == b) {
 				a = 0x00;
 			}
 
 }
 
 static void readTiffBw (const unsigned char *src,
-                        gdImagePtr im,
-                        uint16       photometric,
-                        int          startx,
-                        int          starty,
-                        int          width,
-                        int          height,
-                        char         has_alpha,
-                        int          extra,
-                        int          align)
+			gdImagePtr im,
+			uint16       photometric,
+			int          startx,
+			int          starty,
+			int          width,
+			int          height,
+			char         has_alpha,
+			int          extra,
+			int          align)
 {
 	int x = startx, y = starty;
 	int src_x, src_y;
 
 LOCAL (int)
 median_cut (gdImagePtr oim, gdImagePtr nim, my_cquantize_ptr cquantize,
-            boxptr boxlist, int numboxes, int desired_colors)
+	    boxptr boxlist, int numboxes, int desired_colors)
 /* Repeatedly select and split the largest box until we have enough boxes */
 {
 	int n, lb;
 
 LOCAL (void)
 compute_color (gdImagePtr oim, gdImagePtr nim, my_cquantize_ptr cquantize,
-               boxptr boxp, int icolor)
+	       boxptr boxp, int icolor)
 {
 	hist3d histogram = cquantize->histogram;
 	histptr histp;
 	numcolors =
 	    find_nearby_colors (oim, nim, cquantize, minc0, minc1, minc2, colorlist);
 	find_best_colors (oim, nim, cquantize, minc0, minc1, minc2, numcolors,
-	                  colorlist, bestcolor);
+			  colorlist, bestcolor);
 
 	/* Save the best color numbers (plus 1) in the main cache array */
 	c0 <<= BOX_C0_LOG;		/* convert ID back to base cell indexes */
 			/* entry and update the cache */
 			if (*cachep == 0)
 				fill_inverse_cmap (oim, nim, cquantize, cur0 >> C0_SHIFT,
-				                   cur1 >> C1_SHIFT, cur2 >> C2_SHIFT);
+						   cur1 >> C1_SHIFT, cur2 >> C2_SHIFT);
 			/* Now emit the colormap index for this cell */
 			{
 				register int pixcode = *cachep - 1;
 
 	if (argc != 5) {
 		fprintf(stderr, "Usage: giftogd2 filename.gif filename.gd2 cs fmt\n");
-		fprintf(stderr, "    where cs is the chunk size\n");
-		fprintf(stderr, "          fmt is 1 for raw, 2 for compressed\n");
+		fprintf(stderr, "       where cs is the chunk size\n");
+		fprintf(stderr, "	fmt is 1 for raw, 2 for compressed\n");
 		exit(1);
 	}
 	in = fopen(argv[1], "rb");
 
 	if (argc != 5) {
 		fprintf(stderr, "Usage: pngtogd2 filename.png filename.gd2 cs fmt\n");
-		fprintf(stderr, "    where cs is the chunk size\n");
-		fprintf(stderr, "          fmt is 1 for raw, 2 for compressed\n");
+		fprintf(stderr, "       where cs is the chunk size\n");
+		fprintf(stderr, "       fmt is 1 for raw, 2 for compressed\n");
 		exit (1);
 	}
 	in = fopen (argv[1], "rb");
    quality tradeoffs. */
 
 void testDrawing (gdImagePtr im_in,
-                  double scale, int blending, int palette, char *filename);
+		  double scale, int blending, int palette, char *filename);
 
 int
 main (int argc, char *argv[])
    quality tradeoffs. */
 void
 testDrawing (gdImagePtr im_in,
-             double scale, int blending, int palette, char *filename)
+	     double scale, int blending, int palette, char *filename)
 {
 	gdImagePtr im_out;
 	FILE *out;
 	/* Create output image. */
 	im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale),
-	                                 (int) (gdImageSY (im_in) * scale));
+					 (int) (gdImageSY (im_in) * scale));
 	/*
 	   Request alpha blending. This causes future
 	   drawing operations to perform alpha channel blending
 
 	/* Flood with light blue. */
 	gdImageFill (im_out, (int) (gdImageSX (im_in) * scale / 2),
-	             (int) (gdImageSY (im_in) * scale / 2),
-	             gdTrueColor (192, 192, 255));
+		     (int) (gdImageSY (im_in) * scale / 2),
+		     gdTrueColor (192, 192, 255));
 	/* Copy the source image. Alpha blending should result in
 	   compositing against red. With blending turned off, the
 	   browser or viewer will composite against its preferred
 	   we will see the original colors for the pixels that
 	   ought to be transparent or semitransparent. */
 	gdImageCopyResampled (im_out, im_in,
-	                      0, 0,
-	                      0, 0,
-	                      (int) (gdImageSX (im_in) * scale),
-	                      (int) (gdImageSY (im_in) * scale), gdImageSX (im_in),
-	                      gdImageSY (im_in));
+			      0, 0,
+			      0, 0,
+			      (int) (gdImageSX (im_in) * scale),
+			      (int) (gdImageSY (im_in) * scale), gdImageSX (im_in),
+			      gdImageSY (im_in));
 	/* Write PNG */
 	out = fopen (filename, "wb");
 
 	black =  gdImageColorResolveAlpha(im, 0, 0, 0, gdAlphaOpaque);
 	green =  gdImageColorResolveAlpha(im, 0, gdGreenMax, 0, gdAlphaOpaque);
 	transparent = gdImageColorResolveAlpha(im,
-	                                       gdRedMax-1, gdGreenMax, gdBlueMax, gdAlphaTransparent);
+					       gdRedMax-1, gdGreenMax, gdBlueMax, gdAlphaTransparent);
 	gdImageColorTransparent(im, transparent);
 
 	/* Blending must be off to lay a transparent basecolor.
-	            Nothing to blend with anyway. */
+		    Nothing to blend with anyway. */
 	gdImageAlphaBlending(im, FALSE);
 	gdImageFill (im, im->sx/2, im->sy/2, transparent);
 	/* Blend everything else together,
-	        especially fonts over non-transparent backgrounds */
+		especially fonts over non-transparent backgrounds */
 	gdImageAlphaBlending(im, TRUE);
 
 	gdImageFilledRectangle (im, 30, 30, 70, 70, green);
 #endif
 
 	if(	overflow2(sizeof(int), wbmp->width) ||
-	        overflow2(sizeof(int) * wbmp->width, wbmp->height)) {
+		overflow2(sizeof(int) * wbmp->width, wbmp->height)) {
 		gdFree(wbmp);
 		return -1;
 	}