ppm_downscaler.c 5.0 KB

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