image.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. ppedit - A pattern plate editor for Spiro splines.
  3. Copyright (C) 2007 Raph Levien
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "zmisc.h"
  20. #include "image.h"
  21. /* An image loaded into memory. */
  22. struct _image {
  23. unsigned char *buf;
  24. int width;
  25. int height;
  26. int rowstride;
  27. };
  28. static image *
  29. load_ppm_file(FILE *f, char **reason)
  30. {
  31. image *result;
  32. char line[256];
  33. int xs, ys;
  34. int depth;
  35. int n;
  36. fseek(f, 0, SEEK_SET);
  37. fgets(line, sizeof(line), f);
  38. do {
  39. fgets(line, sizeof(line), f);
  40. } while (line[0] == '#');
  41. n = sscanf(line, "%d %d", &xs, &ys);
  42. if (n != 2) {
  43. *reason = "Error reading ppmraw size line";
  44. fclose(f);
  45. return NULL;
  46. }
  47. do {
  48. fgets(line, sizeof(line), f);
  49. } while (line[0] == '#');
  50. n = sscanf(line, "%d", &depth);
  51. if (n != 1) {
  52. *reason = "Error reading ppmraw depth line";
  53. fclose(f);
  54. return NULL;
  55. }
  56. result = znew(image, 1);
  57. result->rowstride = 3 * xs;
  58. result->buf = zalloc(ys * result->rowstride);
  59. result->width = xs;
  60. result->height = ys;
  61. fread(result->buf, 1, ys * result->rowstride, f);
  62. fclose(f);
  63. return result;
  64. }
  65. image *
  66. load_image_file(const char *fn, char **reason)
  67. {
  68. FILE *f = fopen(fn, "rb");
  69. unsigned char buf[256];
  70. int n;
  71. if (f == NULL) {
  72. *reason = "Error opening file";
  73. return NULL;
  74. }
  75. n = fread(buf, 1, sizeof(buf), f);
  76. if (n < 4) {
  77. *reason = "Short file";
  78. fclose(f);
  79. return NULL;
  80. }
  81. if (buf[0] != 'P' || buf[1] != '6') {
  82. *reason = "Unrecognized magic";
  83. fclose(f);
  84. return NULL;
  85. }
  86. return load_ppm_file(f, reason);
  87. }
  88. void
  89. free_image(image *im)
  90. {
  91. zfree(im->buf);
  92. zfree(im);
  93. }
  94. void
  95. render_image(image *im, const double affine[6],
  96. unsigned char *buf, int rowstride, int x0, int y0, int x1, int y1)
  97. {
  98. int y;
  99. unsigned char *dest_line = buf;
  100. int src_x0 = x0;
  101. for (y = y0; y < y1; y++) {
  102. int src_y = y;
  103. if (src_y >= 0 && src_y < im->height) {
  104. unsigned char *img_line = im->buf + src_y * im->rowstride;
  105. int left_pad = -src_x0;
  106. int img_run, img_off, right_pad;
  107. if (left_pad > x1 - x0) left_pad = x1 - x0;
  108. if (left_pad > 0) {
  109. memset(dest_line, 255, 3 * left_pad);
  110. } else left_pad = 0;
  111. img_off = src_x0;
  112. if (img_off < 0) img_off = 0;
  113. img_run = x1 - x0 - left_pad;
  114. if (img_run > im->width - img_off) img_run = im->width - img_off;
  115. if (img_run > 0) {
  116. memcpy(dest_line + 3 * left_pad, img_line + 3 * img_off, 3 * img_run);
  117. } else img_run = 0;
  118. right_pad = x1 - x0 - left_pad - img_run;
  119. if (right_pad > 0) {
  120. memset(dest_line + 3 * (left_pad + img_run), 255, 3 * right_pad);
  121. }
  122. } else {
  123. memset(dest_line, 255, rowstride);
  124. }
  125. dest_line += rowstride;
  126. }
  127. }