ppm-downscaler.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "ppm-downscaler.h"
  17. #include <starpu.h>
  18. #ifdef STARPU_HAVE_MALLOC_H
  19. #include <malloc.h>
  20. #endif
  21. #include <stdlib.h>
  22. #include <assert.h>
  23. #include <stdio.h>
  24. struct ppm_image *allocate_new_ppm(int ncols, int nlines, int coldepth)
  25. {
  26. struct ppm_image *ppm = malloc(sizeof(struct ppm_image));
  27. assert(ppm);
  28. ppm->ncols = ncols;
  29. ppm->nlines = nlines;
  30. ppm->coldepth = coldepth;
  31. #ifdef STARPU_HAVE_MEMALIGN
  32. ppm->data = memalign(16384, ncols*nlines*sizeof(struct ppm_color));
  33. #else
  34. ppm->data = malloc(ncols*nlines*sizeof(struct ppm_color));
  35. #endif
  36. assert(ppm->data);
  37. return ppm;
  38. }
  39. struct ppm_image *file_to_ppm(char *filename)
  40. {
  41. int ret;
  42. struct ppm_image *ppm = malloc(sizeof(struct ppm_image));
  43. assert(ppm);
  44. FILE *file = fopen(filename, "r");
  45. /* read the file's dimensions */
  46. ret = fscanf(file, "P6\n%d %d\n%d\n", &ppm->ncols, &ppm->nlines, &ppm->coldepth);
  47. if (ret != 3) {
  48. fclose(file);
  49. fprintf(stderr, "file %s is not valid\n", filename);
  50. exit(-1);
  51. }
  52. /* allocate a buffer for the image */
  53. #ifdef STARPU_HAVE_MEMALIGN
  54. ppm->data = memalign(16384, ppm->ncols*ppm->nlines*sizeof(struct ppm_color));
  55. #else
  56. ppm->data = malloc(ppm->ncols*ppm->nlines*sizeof(struct ppm_color));
  57. #endif
  58. assert(ppm->data);
  59. fread(ppm->data, sizeof(struct ppm_color), ppm->ncols*ppm->nlines, file);
  60. unsigned i;
  61. for (i = 0; i < ppm->ncols*ppm->nlines; i++)
  62. {
  63. // fprintf(stderr, "READ (index %d) -> r %d g %d b %d\n", i, ppm->data[i].r, ppm->data[i].g, ppm->data[i].b);
  64. }
  65. fclose(file);
  66. return ppm;
  67. }
  68. void ppm_to_file(struct ppm_image *ppm, char *filename)
  69. {
  70. FILE *file = fopen(filename, "w+");
  71. assert(file);
  72. /* read the file's dimensions */
  73. fprintf(file, "P6\n%d %d\n%d\n", ppm->ncols, ppm->nlines, ppm->coldepth);
  74. fwrite(&ppm->data[0], sizeof(struct ppm_color), ppm->ncols*ppm->nlines, file);
  75. fclose(file);
  76. }
  77. char *filename_in = "serpents.ppm";
  78. char *filename_out = "serpents.small.ppm";
  79. void parse_args(int argc, char **argv)
  80. {
  81. if (argc == 3) {
  82. filename_in = argv[1];
  83. filename_out = argv[2];
  84. }
  85. }
  86. /* what is the downscaling factor ? */
  87. #define FACTOR 2
  88. void dummy_downscale(struct ppm_image *input_ppm, struct ppm_image *output_ppm)
  89. {
  90. struct ppm_color *in = input_ppm->data;
  91. struct ppm_color *out = output_ppm->data;
  92. unsigned line, col;
  93. for (line = 0; line < output_ppm->nlines; line++)
  94. {
  95. for (col = 0; col < output_ppm->ncols; col++)
  96. {
  97. unsigned sum_r = 0, sum_g = 0, sum_b = 0;
  98. unsigned big_col = col*FACTOR;
  99. unsigned big_line = line*FACTOR;
  100. /* compute the average value of all components */
  101. unsigned i, j;
  102. for (i = 0; i < FACTOR; i++)
  103. {
  104. for (j = 0; j < FACTOR; j++)
  105. {
  106. unsigned index = (big_col + i)+(big_line + j)*input_ppm->ncols;
  107. // fprintf(stderr, "(col %d, line %d) i %d j %d index %d -> r %d g %d b %d\n", col, line, i, j, index, in[index].r, in[index].g, in[index].b);
  108. sum_r += (unsigned)in[index].r;
  109. sum_g += (unsigned)in[index].g;
  110. sum_b += (unsigned)in[index].b;
  111. }
  112. }
  113. out[col + line*output_ppm->ncols].r = (unsigned char)(sum_r/(FACTOR*FACTOR));
  114. out[col + line*output_ppm->ncols].g = (unsigned char)(sum_g/(FACTOR*FACTOR));
  115. out[col + line*output_ppm->ncols].b = (unsigned char)(sum_b/(FACTOR*FACTOR));
  116. // fprintf(stderr, "col %d line %d -> sum_r = %d out -> %d\n", col, line, sum_r, out[col + line*FACTOR].r);
  117. }
  118. }
  119. }
  120. int main(int argc, char **argv)
  121. {
  122. struct ppm_image *input_ppm, *output_ppm;
  123. parse_args(argc, argv);
  124. input_ppm = file_to_ppm(filename_in);
  125. fprintf(stderr, "Read input ppm file : ncols = %d, nlines = %d, coldept = %d\n",
  126. input_ppm->nlines, input_ppm->ncols, input_ppm->coldepth);
  127. assert(input_ppm->nlines % FACTOR == 0);
  128. assert(input_ppm->ncols % FACTOR == 0);
  129. output_ppm = allocate_new_ppm(input_ppm->ncols/FACTOR, input_ppm->nlines/FACTOR, input_ppm->coldepth);
  130. dummy_downscale(input_ppm, output_ppm);
  131. ppm_to_file(output_ppm, filename_out);
  132. return 0;
  133. }