gddemo.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdio.h>
  2. #include "gd.h"
  3. #include "gdfontg.h"
  4. #include "gdfonts.h"
  5. int
  6. main (void)
  7. {
  8. /* Input and output files */
  9. FILE *in;
  10. FILE *out;
  11. /* Input and output images */
  12. gdImagePtr im_in = 0, im_out = 0;
  13. /* Brush image */
  14. gdImagePtr brush;
  15. /* Color indexes */
  16. int white;
  17. int blue;
  18. int red;
  19. int green;
  20. /* Points for polygon */
  21. gdPoint points[3];
  22. /* Create output image, 256 by 256 pixels, true color. */
  23. im_out = gdImageCreateTrueColor (256, 256);
  24. /* First color allocated is background. */
  25. white = gdImageColorAllocate (im_out, 255, 255, 255);
  26. /* Set transparent color. */
  27. gdImageColorTransparent (im_out, white);
  28. /* Try to load demoin.png and paste part of it into the
  29. output image. */
  30. in = fopen ("demoin.png", "rb");
  31. if (!in)
  32. {
  33. fprintf (stderr, "Can't load source image; this demo\n");
  34. fprintf (stderr, "is much more impressive if demoin.png\n");
  35. fprintf (stderr, "is available.\n");
  36. im_in = 0;
  37. }
  38. else
  39. {
  40. im_in = gdImageCreateFromPng (in);
  41. fclose (in);
  42. /* Now copy, and magnify as we do so */
  43. gdImageCopyResized (im_out, im_in,
  44. 32, 32, 0, 0, 192, 192, 255, 255);
  45. }
  46. red = gdImageColorAllocate (im_out, 255, 0, 0);
  47. green = gdImageColorAllocate (im_out, 0, 255, 0);
  48. blue = gdImageColorAllocate (im_out, 0, 0, 255);
  49. /* Rectangle */
  50. gdImageLine (im_out, 16, 16, 240, 16, green);
  51. gdImageLine (im_out, 240, 16, 240, 240, green);
  52. gdImageLine (im_out, 240, 240, 16, 240, green);
  53. gdImageLine (im_out, 16, 240, 16, 16, green);
  54. /* Circle */
  55. gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue);
  56. /* Arc */
  57. gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue);
  58. /* Flood fill: doesn't do much on a continuously
  59. variable tone jpeg original. */
  60. gdImageFill (im_out, 8, 8, blue);
  61. /* Polygon */
  62. points[0].x = 64;
  63. points[0].y = 0;
  64. points[1].x = 0;
  65. points[1].y = 128;
  66. points[2].x = 128;
  67. points[2].y = 128;
  68. gdImageFilledPolygon (im_out, points, 3, green);
  69. /* Brush. A fairly wild example also involving a line style! */
  70. if (im_in)
  71. {
  72. int style[8];
  73. brush = gdImageCreateTrueColor (16, 16);
  74. gdImageCopyResized (brush, im_in,
  75. 0, 0, 0, 0,
  76. gdImageSX (brush), gdImageSY (brush),
  77. gdImageSX (im_in), gdImageSY (im_in));
  78. gdImageSetBrush (im_out, brush);
  79. /* With a style, so they won't overprint each other.
  80. Normally, they would, yielding a fat-brush effect. */
  81. style[0] = 0;
  82. style[1] = 0;
  83. style[2] = 0;
  84. style[3] = 0;
  85. style[4] = 0;
  86. style[5] = 0;
  87. style[6] = 0;
  88. style[7] = 1;
  89. gdImageSetStyle (im_out, style, 8);
  90. /* Draw the styled, brushed line */
  91. gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed);
  92. }
  93. /* Text */
  94. gdImageString (im_out, gdFontGiant, 32, 32,
  95. (unsigned char *) "hi", red);
  96. gdImageStringUp (im_out, gdFontSmall, 64, 64,
  97. (unsigned char *) "hi", red);
  98. /* Make output image interlaced (progressive, in the case of JPEG) */
  99. gdImageInterlace (im_out, 1);
  100. out = fopen ("demoout.png", "wb");
  101. /* Write PNG */
  102. gdImagePng (im_out, out);
  103. fclose (out);
  104. gdImageDestroy (im_out);
  105. if (im_in)
  106. {
  107. gdImageDestroy (im_in);
  108. }
  109. return 0;
  110. }