+// Returns a smaller image composed of the specified crop area from the larger area.
+// If the specified area is larger than is in the original, returns NULL.
+EG_IMAGE * egCropImage(IN EG_IMAGE *Image, IN UINTN StartX, IN UINTN StartY, IN UINTN Width, IN UINTN Height) {
+ EG_IMAGE *NewImage = NULL;
+ UINTN x, y;
+
+ if (((StartX + Width) > Image->Width) || ((StartY + Height) > Image->Height))
+ return NULL;
+
+ NewImage = egCreateImage(Width, Height, Image->HasAlpha);
+ if (NewImage == NULL)
+ return NULL;
+
+ for (y = 0; y < Height; y++) {
+ for (x = 0; x < Width; x++) {
+ NewImage->PixelData[y * NewImage->Width + x] = Image->PixelData[(y + StartY) * Image->Width + x + StartX];
+ }
+ }
+ return NewImage;
+} // EG_IMAGE * egCropImage()
+
+// The following function implements a bilinear image scaling algorithm, based on
+// code presented at http://tech-algorithm.com/articles/bilinear-image-scaling/.
+// Resize an image; returns pointer to resized image if successful, NULL otherwise.
+// Calling function is responsible for freeing allocated memory.
+EG_IMAGE * egScaleImage(IN EG_IMAGE *Image, IN UINTN NewWidth, IN UINTN NewHeight) {
+ EG_IMAGE *NewImage = NULL;
+ EG_PIXEL a, b, c, d;
+ UINTN x, y, Index ;
+ UINTN i, j;
+ UINTN Offset = 0;
+ float x_ratio, y_ratio, x_diff, y_diff;
+
+ if ((Image == NULL) || (Image->Height == 0) || (Image->Width == 0) || (NewWidth == 0) || (NewHeight == 0))
+ return NULL;
+
+ if ((Image->Width == NewWidth) && (Image->Height == NewHeight))
+ return (egCopyImage(Image));
+
+ NewImage = egCreateImage(NewWidth, NewHeight, Image->HasAlpha);
+ if (NewImage == NULL)
+ return NULL;
+
+ x_ratio = ((float)(Image->Width - 1)) / NewWidth;
+ y_ratio = ((float)(Image->Height - 1)) / NewHeight;
+
+ for (i = 0; i < NewHeight; i++) {
+ for (j = 0; j < NewWidth; j++) {
+ x = (UINTN)(x_ratio * j);
+ y = (UINTN)(y_ratio * i);
+ x_diff = (x_ratio * j) - x;
+ y_diff = (y_ratio * i) - y;
+ Index = ((y * Image->Width) + x);
+ a = Image->PixelData[Index];
+ b = Image->PixelData[Index + 1];
+ c = Image->PixelData[Index + Image->Width];
+ d = Image->PixelData[Index + Image->Width + 1];
+
+ // blue element
+ // Yb = Ab(1-Image->Width)(1-Image->Height) + Bb(Image->Width)(1-Image->Height) + Cb(Image->Height)(1-Image->Width) + Db(wh)
+ NewImage->PixelData[Offset].b = (a.b)*(1-x_diff)*(1-y_diff) + (b.b)*(x_diff)*(1-y_diff) +
+ (c.b)*(y_diff)*(1-x_diff) + (d.b)*(x_diff*y_diff);
+
+ // green element
+ // Yg = Ag(1-Image->Width)(1-Image->Height) + Bg(Image->Width)(1-Image->Height) + Cg(Image->Height)(1-Image->Width) + Dg(wh)
+ NewImage->PixelData[Offset].g = (a.g)*(1-x_diff)*(1-y_diff) + (b.g)*(x_diff)*(1-y_diff) +
+ (c.g)*(y_diff)*(1-x_diff) + (d.g)*(x_diff*y_diff);
+
+ // red element
+ // Yr = Ar(1-Image->Width)(1-Image->Height) + Br(Image->Width)(1-Image->Height) + Cr(Image->Height)(1-Image->Width) + Dr(wh)
+ NewImage->PixelData[Offset].r = (a.r)*(1-x_diff)*(1-y_diff) + (b.r)*(x_diff)*(1-y_diff) +
+ (c.r)*(y_diff)*(1-x_diff) + (d.r)*(x_diff*y_diff);
+
+ // alpha element
+ NewImage->PixelData[Offset++].a = (a.a)*(1-x_diff)*(1-y_diff) + (b.a)*(x_diff)*(1-y_diff) +
+ (c.a)*(y_diff)*(1-x_diff) + (d.a)*(x_diff*y_diff);
+
+ } // for (j...)
+ } // for (i...)
+ return NewImage;
+} // EG_IMAGE * egScaleImage()
+