testempty1.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "opj_config.h"
  30. #include "openjpeg.h"
  31. #define J2K_CFMT 0
  32. void error_callback(const char *msg, void *v);
  33. void warning_callback(const char *msg, void *v);
  34. void info_callback(const char *msg, void *v);
  35. void error_callback(const char *msg, void *v) {
  36. (void)msg;
  37. (void)v;
  38. puts(msg);
  39. }
  40. void warning_callback(const char *msg, void *v) {
  41. (void)msg;
  42. (void)v;
  43. puts(msg);
  44. }
  45. void info_callback(const char *msg, void *v) {
  46. (void)msg;
  47. (void)v;
  48. puts(msg);
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. const char * v = opj_version();
  53. const OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_GRAY;
  54. unsigned int numcomps = 1;
  55. unsigned int i;
  56. unsigned int image_width = 256;
  57. unsigned int image_height = 256;
  58. opj_cparameters_t parameters;
  59. unsigned int subsampling_dx = 0;
  60. unsigned int subsampling_dy = 0;
  61. opj_image_cmptparm_t cmptparm;
  62. opj_image_t *image;
  63. opj_codec_t* l_codec = 00;
  64. OPJ_BOOL bSuccess;
  65. opj_stream_t *l_stream = 00;
  66. (void)argc;
  67. (void)argv;
  68. opj_set_default_encoder_parameters(&parameters);
  69. parameters.cod_format = J2K_CFMT;
  70. puts(v);
  71. cmptparm.prec = 8;
  72. cmptparm.bpp = 8;
  73. cmptparm.sgnd = 0;
  74. cmptparm.dx = subsampling_dx;
  75. cmptparm.dy = subsampling_dy;
  76. cmptparm.w = image_width;
  77. cmptparm.h = image_height;
  78. image = opj_image_create(numcomps, &cmptparm, color_space);
  79. assert( image );
  80. for (i = 0; i < image_width * image_height; i++)
  81. {
  82. unsigned int compno;
  83. for(compno = 0; compno < numcomps; compno++)
  84. {
  85. image->comps[compno].data[i] = 0;
  86. }
  87. }
  88. /* catch events using our callbacks and give a local context */
  89. opj_set_info_handler(l_codec, info_callback,00);
  90. opj_set_warning_handler(l_codec, warning_callback,00);
  91. opj_set_error_handler(l_codec, error_callback,00);
  92. l_codec = opj_create_compress(OPJ_CODEC_J2K);
  93. opj_set_info_handler(l_codec, info_callback,00);
  94. opj_set_warning_handler(l_codec, warning_callback,00);
  95. opj_set_error_handler(l_codec, error_callback,00);
  96. opj_setup_encoder(l_codec, &parameters, image);
  97. l_stream = opj_stream_create_default_file_stream("testempty1.j2k",OPJ_FALSE);
  98. assert(l_stream);
  99. bSuccess = opj_start_compress(l_codec,image,l_stream);
  100. if( !bSuccess )
  101. {
  102. opj_stream_destroy(l_stream);
  103. opj_destroy_codec(l_codec);
  104. opj_image_destroy(image);
  105. return 0;
  106. }
  107. assert( bSuccess );
  108. bSuccess = opj_encode(l_codec, l_stream);
  109. assert( bSuccess );
  110. bSuccess = opj_end_compress(l_codec, l_stream);
  111. assert( bSuccess );
  112. opj_stream_destroy(l_stream);
  113. opj_destroy_codec(l_codec);
  114. opj_image_destroy(image);
  115. puts( "end" );
  116. return 0;
  117. }