diff options
Diffstat (limited to 'Build/source/libs/gd/libgd-2.1.1/examples')
16 files changed, 915 insertions, 0 deletions
diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/CMakeLists.txt b/Build/source/libs/gd/libgd-2.1.1/examples/CMakeLists.txt new file mode 100644 index 00000000000..e31e67d408a --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/CMakeLists.txt @@ -0,0 +1,39 @@ +include_directories (BEFORE "${GD_SOURCE_DIR}/src" "${CMAKE_BINARY_DIR}") + +SET(TESTS_FILES + arc + crop + gif + nnquant + tgaread +) + +if (JPEG_FOUND) + LIST(APPEND TESTS_FILES copyrotated flip) +endif (JPEG_FOUND) + +if (TIFF_FOUND) + LIST(APPEND TESTS_FILES tiffread) +endif (TIFF_FOUND) + +FOREACH(test_name ${TESTS_FILES}) + add_executable(${test_name} "${test_name}.c") + target_link_libraries (${test_name} ${GD_LIB}) +ENDFOREACH(test_name) + +if(JPEG_FOUND) + if(UNIX) + target_link_libraries(copyrotated m) + endif(UNIX) +endif(JPEG_FOUND) + +if (WIN32 AND NOT MINGW AND NOT MSYS) + add_executable(windows WIN32 "windows.c") + target_link_libraries (windows ${GD_LIB}) +else (WIN32 AND NOT MINGW AND NOT MSYS) + if (MINGW OR MSYS) + add_executable(windows "windows.c") + add_definitions("-mwindows") + target_link_libraries(windows ${GD_LIB} stdc++ gdi32) + endif (MINGW OR MSYS) +endif (WIN32 AND NOT MINGW AND NOT MSYS) diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/arc.c b/Build/source/libs/gd/libgd-2.1.1/examples/arc.c new file mode 100644 index 00000000000..8e862833855 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/arc.c @@ -0,0 +1,31 @@ +/* $Id$ */ +#include "gd.h" +#include <stdio.h> +#include <stdlib.h> + +int main() +{ + gdImagePtr im; + FILE *fp; + int cor_rad = 60; + im = gdImageCreateTrueColor(400, 400); + gdImageFilledRectangle(im, 0, 0, 399, 399, 0x00FFFFFF); + + gdImageFilledArc (im, cor_rad, 399 - cor_rad, cor_rad *2, cor_rad *2, 90, 180, 0x0, gdPie); + + fp = fopen("b.png", "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image.\n"); + gdImageDestroy(im); + return 1; + } +#ifdef HAVE_LIBPNG + gdImagePng(im, fp); +#else + printf("No PNG support. Cannot save image.\n"); +#endif + fclose(fp); + + gdImageDestroy(im); + return 0; +} diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/copyrotated.c b/Build/source/libs/gd/libgd-2.1.1/examples/copyrotated.c new file mode 100644 index 00000000000..03e493d0ce0 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/copyrotated.c @@ -0,0 +1,91 @@ +/* $Id$ */ +#include "gd.h" +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +gdImagePtr loadImage(const char *name) +{ + FILE *fp; + gdImagePtr im; + + fp = fopen(name, "rb"); + if (!fp) { + fprintf(stderr, "Can't open jpeg file\n"); + return NULL; + } + + im = gdImageCreateFromJpeg(fp); + fclose(fp); + return im; +} + +int savePngImage(gdImagePtr im, const char *name) +{ + FILE *fp; + fp = fopen(name, "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image fromtiff.png\n"); + return 0; + } + gdImagePng(im, fp); + fclose(fp); + return 1; +} + +int main(int argc, char **arg) +{ + gdImagePtr im, im2; + int new_width, new_height; + double angle, a2; + + if (argc < 3) { + fprintf(stderr, "Usage: copyrotated [angle in degree] [filename.png]\n"); + return 1; + } + angle = strtod(arg[1], 0); + im = loadImage(arg[2]); + + if (!im) { + fprintf(stderr, "Can't load PNG file <%s>", arg[1]); + return 1; + } + + /* + cos adj hyp (cos = adjacent / hypothenus) + sin op hyp (sin adjacent / hypothenus) + + 10 pixels margin + */ + + /* to radian */ + a2 = angle * .0174532925; + + new_width = ceil(cos(a2) * gdImageSX(im)) + + fabs(sin(a2) * gdImageSY(im)); + new_height = ceil(cos(a2) * gdImageSY(im)) + + fabs(sin(a2) * gdImageSX(im)); + + + im2 = gdImageCreateTrueColor(new_width, new_height); + if (!im2) { + fprintf(stderr, "Can't create a new image"); + gdImageDestroy(im); + return 1; + } + + gdImageAlphaBlending(im2, 0); + gdImageFilledRectangle(im2, 0, 0, gdImageSX(im2), gdImageSY(im2), gdTrueColorAlpha(127,0,0,127)); + + gdImageCopyRotated(im2, im, new_width/2, new_height/2, 0, 0, gdImageSX(im), gdImageSY(im), angle); + gdImageSaveAlpha(im2, 1); + if (!savePngImage(im2, "rotated.png")) { + fprintf(stderr, "Can't save PNG file rotated.png"); + gdImageDestroy(im); + gdImageDestroy(im2); + return 1; + } + + gdImageDestroy(im2); + gdImageDestroy(im); + return 0; +} diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/crop.c b/Build/source/libs/gd/libgd-2.1.1/examples/crop.c new file mode 100644 index 00000000000..bafb6df849c --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/crop.c @@ -0,0 +1,78 @@ +/* $Id$ */ + +#include "gd.h" +#include <stdio.h> +#include <stdlib.h> + +void save_png(gdImagePtr im, const char *filename) +{ + FILE *fp; + fp = fopen(filename, "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image %s\n", filename); + return; + } +#ifdef HAVE_LIBPNG + gdImagePng(im, fp); +#else + printf("No PNG support. Cannot save image.\n"); +#endif + fclose(fp); +} + +gdImagePtr read_png(const char *filename) +{ + FILE * fp; + gdImagePtr im; + + fp = fopen(filename, "rb"); + if (!fp) { + fprintf(stderr, "Can't read png image %s\n", filename); + return NULL; + } +#ifdef HAVE_LIBPNG + im = gdImageCreateFromPng(fp); +#else + im = NULL; + printf("No PNG support. Cannot read image.\n"); +#endif + fclose(fp); + return im; +} + +int main() +{ + gdImagePtr im, im2; + + im = gdImageCreateTrueColor(400, 400); + + if (!im) { + fprintf(stderr, "Can't create 400x400 TC image\n"); + return 1; + } + + gdImageFilledRectangle(im, 19, 29, 390, 390, 0xFFFFFF); + gdImageRectangle(im, 19, 29, 390, 390, 0xFF0000); + save_png(im, "a1.png"); + + im2 = gdImageCropAuto(im, GD_CROP_SIDES); + if (im2) { + save_png(im2, "a2.png"); + gdImageDestroy(im2); + } + gdImageDestroy(im); + + im = read_png("test_crop_threshold.png"); + if (!im) { + return 1; + } + + im2 = gdImageCropThreshold(im, 0xFFFFFF, 0.6); + if (im2) { + save_png(im2, "a4.png"); + gdImageDestroy(im2); + } + + gdImageDestroy(im); + return 0; +} diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/flip.c b/Build/source/libs/gd/libgd-2.1.1/examples/flip.c new file mode 100644 index 00000000000..2399457b81b --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/flip.c @@ -0,0 +1,75 @@ +/* $Id$ */ +#include "gd.h" +#include <stdio.h> +#include <stdlib.h> +gdImagePtr loadImage(const char *name) +{ + FILE *fp; + gdImagePtr im; + + fp = fopen(name, "rb"); + if (!fp) { + fprintf(stderr, "Can't open jpeg file\n"); + return NULL; + } + + im = gdImageCreateFromJpeg(fp); + fclose(fp); + return im; +} + +int savePngImage(gdImagePtr im, const char *name) +{ + FILE *fp; + fp = fopen(name, "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image fromtiff.png\n"); + return 0; + } + gdImagePng(im, fp); + fclose(fp); + return 1; +} + +int main(int argc, char **arg) +{ + gdImagePtr im; + int returncode = 0; + + if (argc < 2) { + fprintf(stderr, "Usage: flip [filename.png]\n"); + return 1; + } + + im = loadImage(arg[1]); + if (!im) goto error; + gdImageFlipHorizontal(im); + if (!savePngImage(im, "flip_horizontal.png")) { + goto error; + } + gdImageDestroy(im); + + im = loadImage(arg[1]); + if (!im) goto error; + gdImageFlipVertical(im); + if (!savePngImage(im, "flip_vertical.png")) { + goto error; + } + gdImageDestroy(im); + + im = loadImage(arg[1]); + if (!im) goto error; + gdImageFlipBoth(im); + if (!savePngImage(im, "flip_both.png")) { + goto error; + } + gdImageDestroy(im); + + goto done; + +error: + returncode = 1; + +done: + return returncode; +} diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/gif.c b/Build/source/libs/gd/libgd-2.1.1/examples/gif.c new file mode 100644 index 00000000000..d5103f086b3 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/gif.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <stdlib.h> +#include <gd.h> + +int main(void) +{ + int i; + FILE * out; + + gdImagePtr im; + gdImagePtr prev =NULL; + int black; + + im = gdImageCreate(100, 100); + if (!im) { + fprintf(stderr, "can't create image"); + return 1; + } + + out = fopen("anim.gif", "wb"); + if (!out) { + fprintf(stderr, "can't create file %s", "anim.gif"); + return 1; + } + + gdImageColorAllocate(im, 255, 255, 255); /* allocate white as side effect */ + gdImageGifAnimBegin(im, out, 1, -1); + + for(i = 0; i < 20; i++) { + int r,g,b; + im = gdImageCreate(100, 100); + r = rand() % 255; + g = rand() % 255; + b = rand() % 255; + + gdImageColorAllocate(im, 255, 255, 255); /* allocate white as side effect */ + black = gdImageColorAllocate(im, r, g, b); + printf("(%i, %i, %i)\n",r, g, b); + gdImageFilledRectangle(im, rand() % 100, rand() % 100, rand() % 100, rand() % 100, black); + gdImageGifAnimAdd(im, out, 1, 0, 0, 10, 1, prev); + + if(prev) { + gdImageDestroy(prev); + } + prev = im; + } + + gdImageGifAnimEnd(out); + fclose(out); + + return 0; +}
\ No newline at end of file diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/nnquant.c b/Build/source/libs/gd/libgd-2.1.1/examples/nnquant.c new file mode 100644 index 00000000000..33ed523266e --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/nnquant.c @@ -0,0 +1,64 @@ +/* $Id$ */ + +#include "gd.h" +#include <stdio.h> +#include <stdlib.h> + +void save_png(gdImagePtr im, const char *filename) +{ + FILE *fp; + fp = fopen(filename, "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image %s\n", filename); + return; + } +#ifdef HAVE_LIBPNG + gdImagePng(im, fp); +#else + printf("No PNG support. Cannot save image.\n"); +#endif + fclose(fp); +} + +int main() +{ +#ifdef HAVE_JPEG + gdImagePtr im, im2; + FILE *fp; + char path[2048]; + + fp=fopen("resampledbug.jpeg", "rb"); + if (!fp) { + fprintf(stderr, "Can't load /home/pierre/IM3801.jpg\n"); + return 1; + } + + im = gdImageCreateFromJpeg(fp); + fclose(fp); + if (!im) { + fprintf(stderr, "Can't load TIFF image %s\n", path); + return 1; + } + + im2 = gdImageNeuQuant(im, 256, 3); + + if (im2) { + gdImageSaveAlpha(im2, 1); + save_png(im2, "a_nnquant.png"); + gdImageDestroy(im2); + } else { + printf("neu quant failed.\n"); + } + + gdImageTrueColorToPalette(im, 1, 256); + + gdImageSaveAlpha(im, 1); + save_png(im, "a_jquant_dither.png"); + + gdImageDestroy(im); +#else + printf("JPEG support is required for this example. Please recompile GD with JPEG or change this example to use another format as input."); + return 1; +#endif + return 0; +} diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.pic b/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.pic Binary files differnew file mode 100644 index 00000000000..8335d3a2a28 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.pic diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.sgi b/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.sgi Binary files differnew file mode 100644 index 00000000000..98aac523c10 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.sgi diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.tga b/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.tga Binary files differnew file mode 100644 index 00000000000..7063fac97bf --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/noIcon.tga diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/noIconAlpha.tga b/Build/source/libs/gd/libgd-2.1.1/examples/noIconAlpha.tga Binary files differnew file mode 100644 index 00000000000..3af87b47b67 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/noIconAlpha.tga diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/resize.c b/Build/source/libs/gd/libgd-2.1.1/examples/resize.c new file mode 100644 index 00000000000..84543cd434b --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/resize.c @@ -0,0 +1,59 @@ +#include "gd.h" +#include <stdio.h> +#include <stdlib.h> + +int main (int argc, char *argv[]) { + FILE *fp; + gdImagePtr in, out; + int w, h; + + /* Help */ + if (argc<=4) { + printf("%s input.jpg output.jpg width height\n", argv[0]); + return 1; + } + + /* Size */ + w = atoi(argv[3]); + h = atoi(argv[4]); + if (w<=0 || h<=0) { + fprintf(stderr, "Bad size %dx%d\n", h, w); + return 2; + } + + /* Input */ + fp = fopen(argv[1], "rb"); + if (!fp) { + fprintf(stderr, "Can't read image %s\n", argv[1]); + return 3; + } + in = gdImageCreateFromJpeg(fp); + fclose(fp); + if (!in) { + fprintf(stderr, "Can't create image from %s\n", argv[1]); + return 4; + } + + /* Resize */ + gdImageSetInterpolationMethod(in, GD_BILINEAR_FIXED); + out = gdImageScale(in, w, h); + if (!out) { + fprintf(stderr, "gdImageScale fails\n"); + return 5; + } + + /* Output */ + fp = fopen(argv[2], "wb"); + if (!fp) { + fprintf(stderr, "Can't save image %s\n", argv[2]); + return 6; + } + gdImageJpeg(out, fp, 90); + fclose(fp); + + /* Cleanups */ + gdImageDestroy(in); + gdImageDestroy(out); + + return 0; +}
\ No newline at end of file diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/test_crop_threshold.png b/Build/source/libs/gd/libgd-2.1.1/examples/test_crop_threshold.png Binary files differnew file mode 100644 index 00000000000..4a9dff1a815 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/test_crop_threshold.png diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/tgaread.c b/Build/source/libs/gd/libgd-2.1.1/examples/tgaread.c new file mode 100644 index 00000000000..a8e080782b2 --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/tgaread.c @@ -0,0 +1,56 @@ +/* $Id$ */ +/* + * You can fetch a set of samples TIFF images here: + * ftp://ftp.remotesensing.org/pub/libtiff/ + * (pics-x.y.z.tar.gz) + */ + +#include <gd.h> +#include <stdio.h> +#include <stdlib.h> + +int main() +{ + gdImagePtr im; + FILE *fp; + char path[4][2048]; + int i; + char dst[2048]; + + sprintf(path[0], "noIconAlpha.tga"); + sprintf(path[1], "noIcon.tga"); + + for (i = 0; i < 2; i++) { + printf("opening %s\n", path[i]); + fp = fopen(path[i], "rb"); + if (!fp) { + printf("failed, cannot open file\n"); + return 1; + } + + im = gdImageCreateFromTga(fp); + fclose(fp); + if (!im) { + fprintf(stderr, "Can't load TIFF image %s\n", path[i]); + return 1; + } + + + sprintf(dst, "%i.png", i); + + fp = fopen(dst, "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image fromtiff.png\n"); + gdImageDestroy(im); + return 1; + } +#ifdef HAVE_LIBPNG + gdImagePng(im, fp); +#else + printf("No PNG support. Cannot save image.\n"); +#endif + fclose(fp); + gdImageDestroy(im); + } + return 0; +} diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/tiffread.c b/Build/source/libs/gd/libgd-2.1.1/examples/tiffread.c new file mode 100644 index 00000000000..fe63ce00b2c --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/tiffread.c @@ -0,0 +1,59 @@ +/* $Id$ */ +/* + * You can fetch a set of samples TIFF images here: + * ftp://ftp.remotesensing.org/pub/libtiff/ + * (pics-x.y.z.tar.gz) + */ + +#include <gd.h> +#include <stdio.h> +#include <stdlib.h> +int main() +{ + gdImagePtr im; + FILE *fp; + char path[9][2048]; + int i; + char dst[2048]; + + sprintf(path[0], "cramps-tile.tif"); + sprintf(path[1], "cramps.tif"); + sprintf(path[2], "ycbcr-cat.tif"); + sprintf(path[3], "jello.tif"); + sprintf(path[4], "caspian.tif"); + sprintf(path[5], "strike.tif"); + sprintf(path[6], "off_luv24.tif"); + sprintf(path[7], "off_l16.tif"); + sprintf(path[8], "fax2d.tif"); + + for (i = 0; i < 9; i++) { + printf("opening %s\n", path[i]); + fp = fopen(path[i], "rb"); + if (!fp) { + printf("failed, cannot open file\n"); + return 1; + } + + im = gdImageCreateFromTiff(fp); + fclose(fp); + if (!im) { + fprintf(stderr, "Can't load TIFF image %s\n", path[i]); + return 1; + } + + + sprintf(dst, "%i.png", i); + + fp = fopen(dst, "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image fromtiff.png\n"); + gdImageDestroy(im); + return 1; + } + + gdImagePng(im, fp); + fclose(fp); + gdImageDestroy(im); + } + return 0; +} diff --git a/Build/source/libs/gd/libgd-2.1.1/examples/windows.c b/Build/source/libs/gd/libgd-2.1.1/examples/windows.c new file mode 100644 index 00000000000..55976386f4e --- /dev/null +++ b/Build/source/libs/gd/libgd-2.1.1/examples/windows.c @@ -0,0 +1,311 @@ +/* +Sample usage of GD on windows. This little program opens a window, fetch its DIB +and assigns to a GD truecolor image. + +Thanks to Mateusz Loskot (http://mateusz.loskot.net) for the AttachBuffer function! +$Id$ +*/ +#include <windows.h> +#include <gd.h> +#include <gdfontg.h> +#include <gdfontl.h> + + +gdImagePtr gdImageTrueColorAttachBuffer(int* buffer, int sx, int sy, int stride) +{ + int i; + int height; + int* rowptr; + gdImagePtr im; + im = (gdImage *) malloc (sizeof (gdImage)); + if (!im) { + return 0; + } + memset (im, 0, sizeof (gdImage)); + +#if 0 + if (overflow2(sizeof (int *), sy)) { + return 0; + } +#endif + + im->tpixels = (int **) malloc (sizeof (int *) * sy); + if (!im->tpixels) { + free(im); + return 0; + } + + im->polyInts = 0; + im->polyAllocated = 0; + im->brush = 0; + im->tile = 0; + im->style = 0; + + height = sy; + rowptr = buffer; + if (stride < 0) { + int startoff = (height - 1) * stride; + rowptr = buffer - startoff; + } + + i = 0; + while (height--) { + im->tpixels[i] = rowptr; + rowptr += stride; + i++; + } + + im->sx = sx; + im->sy = sy; + im->transparent = (-1); + im->interlace = 0; + im->trueColor = 1; + im->saveAlphaFlag = 0; + im->alphaBlendingFlag = 1; + im->thick = 1; + im->AA = 0; + im->cx1 = 0; + im->cy1 = 0; + im->cx2 = im->sx - 1; + im->cy2 = im->sy - 1; + return im; +} + +void gdImageDetachBuffer(gdImagePtr im) +{ + free(im->tpixels); + free(im); +} + + +BITMAPINFO gdCreateBmp(int width, int height) +{ + BITMAPINFO bmp_info; + + // Configure bitmap properties + + ZeroMemory(&bmp_info, sizeof(BITMAPINFO)); + bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmp_info.bmiHeader.biWidth = width; + bmp_info.bmiHeader.biHeight = height; + bmp_info.bmiHeader.biPlanes = 1; + bmp_info.bmiHeader.biBitCount = 32; + bmp_info.bmiHeader.biCompression = BI_RGB; + bmp_info.bmiHeader.biSizeImage = 0; + bmp_info.bmiHeader.biXPelsPerMeter = 0; + bmp_info.bmiHeader.biYPelsPerMeter = 0; + bmp_info.bmiHeader.biClrUsed = 0; + bmp_info.bmiHeader.biClrImportant = 0; + return bmp_info; +} + +LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; + +int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, + PSTR szCmdLine, int iCmdShow) +{ + static TCHAR szAppName[] = TEXT ("Bezier") ; + HWND hwnd ; + MSG msg ; + WNDCLASS wndclass ; + + wndclass.style = CS_HREDRAW | CS_VREDRAW ; + wndclass.lpfnWndProc = WndProc ; + wndclass.cbClsExtra = 0 ; + wndclass.cbWndExtra = 0 ; + wndclass.hInstance = hInstance ; + wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; + wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; + wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; + wndclass.lpszMenuName = NULL ; + wndclass.lpszClassName = szAppName ; + + if (!RegisterClass (&wndclass)) { + // UNICODE-Compilierung ist die einzige realistische Fehlermöglichkeit + MessageBox (NULL, TEXT ("Programm arbeitet mit Unicode und setzt Windows NT voraus!"), + szAppName, MB_ICONERROR) ; + return 0 ; + } + + hwnd = CreateWindow (szAppName, TEXT ("Bezierkurven"), + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL) ; + + ShowWindow (hwnd, iCmdShow) ; + UpdateWindow (hwnd) ; + + while (GetMessage (&msg, NULL, 0, 0)) { + TranslateMessage (&msg) ; + DispatchMessage (&msg) ; + } + return msg.wParam ; +} + +void DrawBezier (HDC hdc, POINT apt[]) +{ + PolyBezier (hdc, apt, 4) ; + + MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ; + LineTo (hdc, apt[1].x, apt[1].y) ; + + MoveToEx (hdc, apt[2].x, apt[2].y, NULL) ; + LineTo (hdc, apt[3].x, apt[3].y) ; +} + + +void gdDrawImage(HDC hdc, RECT *rc) +{ + HDC mem_dc; + BITMAPINFO bmp_info; + void* bits; + HBITMAP bmp, temp; + gdImagePtr im; + int width, height, stride; + int white, black, blue, red; + char *s = "Hello world!"; + gdFontPtr lfont, gfont; + + width = rc->right - rc->left; + height = rc->bottom - rc->top; + + bmp_info = gdCreateBmp(width, height); + + // Create memory device context + mem_dc = CreateCompatibleDC(hdc); + if (!mem_dc) { + MessageBox(NULL, "Can't create a compatible DC!", "Error!", MB_ICONEXCLAMATION | MB_OK); + return; + } + + // bits points to a shared buffer of pixels + bits = NULL; + bmp = CreateDIBSection(mem_dc, &bmp_info, DIB_RGB_COLORS, (void**)&bits, 0, 0); + + // Selecting the object before doing anything allows you to use libgd + // together with native Windows GDI. + temp = (HBITMAP)SelectObject(mem_dc, bmp); + + /*stride = ((width * 1 + 3) >> 2) << 2;*/ + // always uses 32bit in BMPINFO + stride = width; + im = NULL; + + // Attach shared buffer of pixels to GD image + // Negative stride places 0,0 in upper-left corner + im = gdImageTrueColorAttachBuffer((int*)bits, width, height, -stride); + if (!im) { + MessageBox(NULL, "GD image creation failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); + return; + } + + // Start of GD drawing + white = gdImageColorAllocate(im, 255, 255, 255); + black = gdImageColorAllocate(im, 0, 0, 0); + blue = gdImageColorAllocate(im, 0, 0, 255); + + // Allocate the color red, 50% transparent. + red = gdImageColorAllocateAlpha(im, 255, 0, 0, 64); + + // Erase background with white color + gdImageFilledRectangle(im, 0, 0, width, height, 0xFF0000); + + lfont = gdFontGetLarge(); + gfont = gdFontGetGiant(); + + // Draw a dashed line from the upper left corner to the lower right corner. + gdImageFilledRectangle(im, 25, 25, 100, 100, blue); + + gdImageChar(im, gfont, 35, 35, 'Q', white); + gdImageFilledRectangle(im, 50, 50, 75, 175, red); + gdImageLine(im, 0, 0, 150, 150, black); + + gdImageString(im, gdFontGetLarge(), + im->sx / 2 - (strlen(s) * lfont->w / 2), + im->sy / 2 - lfont->h / 2, + (unsigned char*)s, black); + + // Copy drawing from memory context (shared bitmap buffer) to screen DC. + BitBlt(hdc, rc->left, rc->top, width, height, mem_dc, 0, 0, SRCCOPY); + + // Free + gdImageDetachBuffer(im); + SelectObject(mem_dc, temp); + DeleteObject(bmp); + DeleteObject(mem_dc); +} + +LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + static POINT apt[4] ; + HDC hdc ; + int cxClient, cyClient ; + PAINTSTRUCT ps ; + RECT rc; + + GetClientRect(hwnd, &rc); + + switch (message) { + case WM_SIZE: + cxClient = LOWORD (lParam) ; + cyClient = HIWORD (lParam) ; + + apt[0].x = cxClient / 4 ; + apt[0].y = cyClient / 2 ; + + apt[1].x = cxClient / 2 ; + apt[1].y = cyClient / 4 ; + + apt[2].x = cxClient / 2 ; + apt[2].y = 3 * cyClient / 4 ; + + apt[3].x = 3 * cxClient / 4 ; + apt[3].y = cyClient / 2 ; + return 0 ; + + case WM_LBUTTONDOWN: + case WM_RBUTTONDOWN: + case WM_MOUSEMOVE: + if (wParam & MK_LBUTTON || wParam & MK_RBUTTON) { + hdc = GetDC (hwnd) ; + + // alte Kurve löschen (mit Weiß übermalen) + SelectObject (hdc, GetStockObject (WHITE_PEN)) ; + DrawBezier (hdc, apt) ; + + if (wParam & MK_LBUTTON) { + apt[1].x = LOWORD (lParam) ; + apt[1].y = HIWORD (lParam) ; + } + + if (wParam & MK_RBUTTON) { + apt[2].x = LOWORD (lParam) ; + apt[2].y = HIWORD (lParam) ; + } + + // neue Kurve (mit Schwarz) zeichnen + SelectObject (hdc, GetStockObject (BLACK_PEN)) ; + gdDrawImage(hdc, &rc); + DrawBezier (hdc, apt) ; + ReleaseDC (hwnd, hdc) ; + } + return 0 ; + + + case WM_PAINT: + hdc = BeginPaint (hwnd, &ps) ; + + GetClientRect(hwnd, &rc); + gdDrawImage(hdc, &rc); + DrawBezier (hdc, apt) ; + + EndPaint (hwnd, &ps) ; + return 0 ; + + case WM_DESTROY: + PostQuitMessage (0) ; + return 0 ; + } + return DefWindowProc (hwnd, message, wParam, lParam) ; +} |