testempty2.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2012, Mathieu Malaterre
  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. #include <assert.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "opj_config.h"
  31. #include "openjpeg.h"
  32. #define J2K_CFMT 0
  33. void error_callback(const char *msg, void *v);
  34. void warning_callback(const char *msg, void *v);
  35. void info_callback(const char *msg, void *v);
  36. void error_callback(const char *msg, void *v) {
  37. (void)msg;
  38. (void)v;
  39. puts(msg);
  40. }
  41. void warning_callback(const char *msg, void *v) {
  42. (void)msg;
  43. (void)v;
  44. puts(msg);
  45. }
  46. void info_callback(const char *msg, void *v) {
  47. (void)msg;
  48. (void)v;
  49. puts(msg);
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. const char * v = opj_version();
  54. const OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_GRAY;
  55. unsigned int numcomps = 1;
  56. unsigned int i;
  57. unsigned int image_width = 256;
  58. unsigned int image_height = 256;
  59. opj_cparameters_t parameters;
  60. unsigned int subsampling_dx;
  61. unsigned int subsampling_dy;
  62. const char outputfile[] = "testempty2.j2k";
  63. opj_image_cmptparm_t cmptparm;
  64. opj_image_t *image;
  65. opj_codec_t* l_codec = 00;
  66. OPJ_BOOL bSuccess;
  67. opj_stream_t *l_stream = 00;
  68. (void)argc;
  69. (void)argv;
  70. opj_set_default_encoder_parameters(&parameters);
  71. parameters.cod_format = J2K_CFMT;
  72. puts(v);
  73. subsampling_dx = (unsigned int)parameters.subsampling_dx;
  74. subsampling_dy = (unsigned int)parameters.subsampling_dy;
  75. cmptparm.prec = 8;
  76. cmptparm.bpp = 8;
  77. cmptparm.sgnd = 0;
  78. cmptparm.dx = subsampling_dx;
  79. cmptparm.dy = subsampling_dy;
  80. cmptparm.w = image_width;
  81. cmptparm.h = image_height;
  82. strncpy(parameters.outfile, outputfile, sizeof(parameters.outfile)-1);
  83. image = opj_image_create(numcomps, &cmptparm, color_space);
  84. assert( image );
  85. for (i = 0; i < image_width * image_height; i++)
  86. {
  87. unsigned int compno;
  88. for(compno = 0; compno < numcomps; compno++)
  89. {
  90. image->comps[compno].data[i] = 0;
  91. }
  92. }
  93. /* catch events using our callbacks and give a local context */
  94. opj_set_info_handler(l_codec, info_callback,00);
  95. opj_set_warning_handler(l_codec, warning_callback,00);
  96. opj_set_error_handler(l_codec, error_callback,00);
  97. l_codec = opj_create_compress(OPJ_CODEC_J2K);
  98. opj_set_info_handler(l_codec, info_callback,00);
  99. opj_set_warning_handler(l_codec, warning_callback,00);
  100. opj_set_error_handler(l_codec, error_callback,00);
  101. opj_setup_encoder(l_codec, &parameters, image);
  102. l_stream = opj_stream_create_default_file_stream(parameters.outfile,OPJ_FALSE);
  103. if( !l_stream )
  104. {
  105. fprintf( stderr, "Something went wrong during creation of stream\n" );
  106. opj_destroy_codec(l_codec);
  107. opj_image_destroy(image);
  108. opj_stream_destroy(l_stream);
  109. return 1;
  110. }
  111. assert(l_stream);
  112. bSuccess = opj_start_compress(l_codec,image,l_stream);
  113. if( !bSuccess )
  114. {
  115. opj_stream_destroy(l_stream);
  116. opj_destroy_codec(l_codec);
  117. opj_image_destroy(image);
  118. return 0;
  119. }
  120. assert( bSuccess );
  121. bSuccess = opj_encode(l_codec, l_stream);
  122. assert( bSuccess );
  123. bSuccess = opj_end_compress(l_codec, l_stream);
  124. assert( bSuccess );
  125. opj_stream_destroy(l_stream);
  126. opj_destroy_codec(l_codec);
  127. opj_image_destroy(image);
  128. /* read back the generated file */
  129. {
  130. opj_codec_t* d_codec = 00;
  131. opj_dparameters_t dparameters;
  132. d_codec = opj_create_decompress(OPJ_CODEC_J2K);
  133. opj_set_info_handler(d_codec, info_callback,00);
  134. opj_set_warning_handler(d_codec, warning_callback,00);
  135. opj_set_error_handler(d_codec, error_callback,00);
  136. bSuccess = opj_setup_decoder(d_codec, &dparameters);
  137. assert( bSuccess );
  138. l_stream = opj_stream_create_default_file_stream(outputfile,1);
  139. assert( l_stream );
  140. bSuccess = opj_read_header(l_stream, d_codec, &image);
  141. assert( bSuccess );
  142. bSuccess = opj_decode(l_codec, l_stream, image);
  143. assert( bSuccess );
  144. bSuccess = opj_end_decompress(l_codec, l_stream);
  145. assert( bSuccess );
  146. opj_stream_destroy(l_stream);
  147. opj_destroy_codec(d_codec);
  148. opj_image_destroy(image);
  149. }
  150. puts( "end" );
  151. return 0;
  152. }