mgsLib  1.3
Mermaja's Graphic Screen. A simple C library to build Windows graphic applications from console programs.
magnifier.c

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>
#include "mgsLib.h"
#define MAXW 300
#define MAXH 200
#define MAXMAG 10
// Struct to hold captured image stamp data
struct imgData {
int x, y, mode;
};
int main()
{
// Variables for the screens, image, mouse and some other
int scr, scr2, scr3, image, x, y, ix, iy, btn, whl, mag = 5, recx, recy, color = MGS_CBLACK;
// Flag for the capture mode
unsigned char capture = 1;
// Texts
char magText[20], capText[20], rgbText[20];
// Create three screens: main, magnifier and color display
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);
// Capture a first image to correctly enter the main loop
if ((image = mgsMakeImage(scr2, 0, 0, MAXW, MAXH)) < 0)
mgsShowError(image, "Error creating first image", MGS_EFWAIT | MGS_EFWIN);
// Big font to write messages
mgsSetFont(scr, 40, MGS_CRED, MGS_TRANSPARENT);
// And smaller with background to show RGB coordinates of the color
mgsSetFont(scr3, 25, MGS_CBLACK, MGS_CWHITE);
// S to exit
puts("Press S to exit.");
while(!mgsKeyPressed('S')) {
// Mouse data is used frequently, so get them all once per frame
x = mgsMouseX(scr);
y = mgsMouseY(scr);
btn = mgsMouseButtons(scr);
whl = mgsMouseWheel(scr, 1);
// The advanced mouse functions are used on two windows
// In capture mode update the magnification with the wheel and capture
// the image to be displayed on scr2
if (capture) {
// Apply the magnification between 2 and the maximum
if ((whl > 0) && (mag < MAXMAG))
mag++;
else if ((whl < 0) && (mag > 2))
mag--;
// Recalculate the size to capture accordingly.
// The display size is constant.
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;
// Images do not accumulate, so release the previous one
// Erase the text, only want to capture the background
// Adjust the capturing rectangle for odd recx and recy near the borders
ix = x - recx / 2;
iy = y - recy / 2;
if (ix + recx > 800) ix--;
if (iy + recy > 600) iy--;
// And capture the image
if ((image = mgsMakeImage(scr, ix, iy, recx, recy)) < 0)
mgsShowError(image, "Error capturing image", MGS_EFWAIT | MGS_EFWIN);
}
// Erase the text if not done previosly. We delay it until the last moment,
// but the effect is the same when erasing once before the if sentence -if calculations do
// not take long.
else mgsClearScreen(scr);
// Left click to toggle capture mode
if (mgsLeftMouseClk(scr, 0, 0, 800, 600))
capture = 1 - capture;
// Left click on scr2 to pick a color
if (mgsLeftMouseClk(scr2, 0, 0, MAXW, MAXH))
color = mgsGetColor(scr2, mgsMouseX(scr2), mgsMouseY(scr2));
// Prepare the texts to be shown
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);
// And last, display the objects
// The selected color on scr3
mgsRectangle(scr3, 0, 0, MAXW, MAXH, color);
// The magnified image on scr2. First draw a black rectangle
// to avoid border artifacts due to integer operations.
// Not the best but the simplest solution
mgsRectangle(scr2, 0, 0, MAXW, MAXH, MGS_CBLACK);
mgsDisplayRotatedImage(scr2, image, 0, 0, mag, mag, 0, 0);
// Both info texts on scr
mgsPuts(scr, 10, 5, magText);
mgsPuts(scr, 10, 55, capText);
// RGB text on scr3
mgsPuts(scr3, MAXW / 2 - 70, 5, rgbText);
// Frame synchronization and screen updating
}
exit(EXIT_SUCCESS);
}