ppm2rgb3.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2014, Mathieu Malaterre <mathieu.malaterre@voxxl.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * Technically on UNIX, one can simply call `ppmtorgb3`, but on my system it
  28. * did not work. So I had to write my own.
  29. */
  30. #include <stdio.h> /* fprintf */
  31. #include <string.h> /* strcmp */
  32. #include <stdlib.h> /* malloc */
  33. static const char magic[] = "P6";
  34. static int readheader( FILE *ppm, int *X, int *Y, int *bpp )
  35. {
  36. char buffer[256];
  37. char strbuffer[256];
  38. char *line;
  39. int n;
  40. *X = *Y = *bpp = 0;
  41. line = fgets(buffer, sizeof(buffer), ppm);
  42. if( !line ) return 0;
  43. n = sscanf(buffer, "%255[^\r\n]", strbuffer);
  44. if( n != 1 ) return 0;
  45. if( strcmp(strbuffer, magic ) != 0 ) return 0;
  46. /* skip comments */
  47. while( fgets(buffer, sizeof(buffer), ppm) && *buffer == '#' )
  48. {
  49. }
  50. n = sscanf(buffer, "%d %d", X,Y);
  51. if( n != 2 ) return 0;
  52. line = fgets(buffer, sizeof(buffer), ppm);
  53. if( !line ) return 0;
  54. n = sscanf(buffer, "%d", bpp);
  55. if( n != 1 ) return 0;
  56. if( *bpp != 255 ) return 0;
  57. return 1;
  58. }
  59. static int writeoutput( const char *fn, FILE *ppm, int X, int Y, int bpp )
  60. {
  61. FILE *outf[] = {NULL, NULL, NULL};
  62. int i, x, y = 0;
  63. char outfn[256];
  64. static const char *exts[3] = {
  65. "red",
  66. "grn",
  67. "blu"
  68. };
  69. char *image_line = NULL;
  70. int ok = 0;
  71. /* write single comp as PGM: P5 */
  72. for( i = 0; i < 3; ++i )
  73. {
  74. #ifdef _MSC_VER
  75. #define snprintf _snprintf /* Visual Studio */
  76. #endif
  77. snprintf( outfn, sizeof(outfn), "%s.%s.pgm", fn, exts[i] );
  78. outf[i] = fopen( outfn, "wb" );
  79. if( !outf[i] ) goto cleanup;
  80. /* write header */
  81. fprintf( outf[i], "P5\n" );
  82. fprintf( outf[i], "%d %d\n", X, Y );
  83. fprintf( outf[i], "%d\n", bpp );
  84. }
  85. /* write pixel data */
  86. image_line = (char*)malloc( (size_t)X * 3 * sizeof(char) );
  87. if( !image_line ) goto cleanup;
  88. while( fread(image_line, sizeof(char), (size_t)X * 3, ppm) == (size_t)X * 3 )
  89. {
  90. for( x = 0; x < X; ++x )
  91. for( i = 0; i < 3; ++i )
  92. if( fputc( image_line[3*x+i], outf[i] ) == EOF ) goto cleanup;
  93. ++y;
  94. }
  95. if( y == Y )
  96. ok = 1;
  97. cleanup:
  98. free(image_line);
  99. for( i = 0; i < 3; ++i )
  100. if( outf[i] ) fclose( outf[i] );
  101. return ok;
  102. }
  103. int main(int argc, char *argv[])
  104. {
  105. const char *fn;
  106. FILE *ppm = NULL;
  107. int X, Y, bpp;
  108. int ok = 0;
  109. if( argc < 2 )
  110. {
  111. fprintf( stderr, "%s input.ppm\n", argv[0] );
  112. goto cleanup;
  113. }
  114. fn = argv[1];
  115. ppm = fopen( fn, "rb" );
  116. if( !readheader( ppm, &X, &Y, &bpp ) ) goto cleanup;
  117. if( !writeoutput(fn, ppm, X, Y, bpp ) ) goto cleanup;
  118. ok = 1;
  119. cleanup:
  120. if(ppm) fclose(ppm);
  121. return ok ? EXIT_SUCCESS : EXIT_FAILURE;
  122. }