ppm_downscaler.c 4.6 KB

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