This example shows how to use some of the new functions of this version of mgsLib. It creates images from a screen region, and show them magnified in another screen. The magnification level is controlled using the mouse wheel. From the magnified image it is posible to select a pixel and show its color in a third screen.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#define MAXW 300
#define MAXH 200
#define MAXMAG 10
struct imgData {
int x, y, mode;
};
int main()
{
int scr, scr2, scr3, image, x, y, ix, iy, btn, whl, mag = 5, recx, recy, color = MGS_CBLACK;
unsigned char capture = 1;
char magText[20], capText[20], rgbText[20];
if ((scr =
mgsCreateScreen(800, 600,
"Image to magnify", 0x80C080,
"tomillo.bmp")) < 0)
exit(EXIT_FAILURE);
if ((scr2 =
mgsCreateScreen(MAXW, MAXH,
"Magnified", MGS_CBLACK, NULL)) < 0)
exit(EXIT_FAILURE);
if ((scr3 =
mgsCreateScreen(MAXW, MAXH,
"Selected color", MGS_CBLACK, NULL)) < 0)
exit(EXIT_FAILURE);
mgsShowError(image,
"Error creating first image", MGS_EFWAIT | MGS_EFWIN);
puts("Press S to exit.");
if (capture) {
if ((whl > 0) && (mag < MAXMAG))
mag++;
else if ((whl < 0) && (mag > 2))
mag--;
recx = MAXW / mag;
recy = MAXH / mag;
if (x < recx / 2) x = recx / 2;
else if (x > 800 - recx / 2) x = 800 - recx / 2;
if (y < recy / 2) y = recy / 2;
else if (y > 600 - recy / 2) y = 600 - recy / 2;
ix = x - recx / 2;
iy = y - recy / 2;
if (ix + recx > 800) ix--;
if (iy + recy > 600) iy--;
mgsShowError(image,
"Error capturing image", MGS_EFWAIT | MGS_EFWIN);
}
capture = 1 - capture;
sprintf(magText, "Magnify x %d", mag);
sprintf(capText, "Capture %s", capture ? "ON" : "OFF");
sprintf(rgbText, "RGB: %02X%02X%02X", color & 0xFF, (color >> 8) & 0xFF, (color >> 16) & 0xFF);
mgsPuts(scr3, MAXW / 2 - 70, 5, rgbText);
}
exit(EXIT_SUCCESS);
}