plot_intra_maps.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*Daala video codec
  2. Copyright (c) 2012 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ogg/os_types.h>
  27. #include "image.h"
  28. static int get_dimensions_from_filename(int *_nxblocks,int *_nyblocks,
  29. const char *_fin_name){
  30. const char *p;
  31. p=strstr(_fin_name,".intra.map");
  32. if(p!=NULL){
  33. const char *q;
  34. do{
  35. q=p;
  36. p=strstr(p+1,".intra.map");
  37. }
  38. while(p!=NULL);
  39. while(q>_fin_name&&*(q-1)>='0'&&*(q-1)<='9')q--;
  40. if(q>_fin_name&&*--q=='x'){
  41. while(q>_fin_name&&*(q-1)>='0'&&*(q-1)<='9')q--;
  42. if(sscanf(q,"%dx%d",_nxblocks,_nyblocks)==2){
  43. return 0;
  44. }
  45. }
  46. }
  47. return -1;
  48. }
  49. int main(int _argc,const char **_argv){
  50. int ai;
  51. for(ai=1;ai<_argc;ai++){
  52. char fout_name[8192];
  53. unsigned char *map;
  54. FILE *fin;
  55. FILE *fout;
  56. od_rgba16_pixel *colors;
  57. od_rgba16_image image;
  58. int nxblocks;
  59. int nyblocks;
  60. int mapi_max;
  61. int bi;
  62. int bj;
  63. /*Try to extract the map size from the file name.*/
  64. if(get_dimensions_from_filename(&nxblocks,&nyblocks,_argv[ai])<0){
  65. fprintf(stderr,"Could not determine dimensions of '%s'.\n",_argv[ai]);
  66. return EXIT_FAILURE;
  67. }
  68. map=(unsigned char *)_ogg_malloc(nxblocks*(size_t)nyblocks*sizeof(*map));
  69. fin=fopen(_argv[ai],"rb");
  70. if(fin==NULL){
  71. fprintf(stderr,"Could not open '%s' for reading.\n",_argv[ai]);
  72. return EXIT_FAILURE;
  73. }
  74. if(fread(map,nxblocks*(size_t)nyblocks,1,fin)<1){
  75. fprintf(stderr,"Error reading from input file '%s'.\n",_argv[ai]);
  76. return EXIT_FAILURE;
  77. }
  78. fclose(fin);
  79. /*Count how many different intra modes we tried.*/
  80. mapi_max=0;
  81. for(bj=0;bj<nyblocks;bj++){
  82. for(bi=0;bi<nxblocks;bi++){
  83. if(map[bj*nxblocks+bi]>mapi_max)mapi_max=map[bj*nxblocks+bi];
  84. }
  85. }
  86. mapi_max++;
  87. colors=(od_rgba16_pixel *)_ogg_malloc(mapi_max*sizeof(*colors));
  88. intra_map_colors(colors,mapi_max);
  89. od_rgba16_image_init(&image,nxblocks,nyblocks);
  90. for(bj=0;bj<nyblocks;bj++){
  91. for(bi=0;bi<nxblocks;bi++){
  92. od_rgba16_image_draw_point(&image,bi,bj,colors[map[bj*nxblocks+bi]]);
  93. }
  94. }
  95. sprintf(fout_name,"%s.png",_argv[ai]);
  96. fout=fopen(fout_name,"wb");
  97. if(fout==NULL){
  98. fprintf(stderr,"Could not open '%s' for reading.\n",fout_name);
  99. return EXIT_FAILURE;
  100. }
  101. od_rgba16_image_write_png(&image,fout);
  102. fclose(fout);
  103. od_rgba16_image_clear(&image);
  104. _ogg_free(colors);
  105. _ogg_free(map);
  106. }
  107. return EXIT_SUCCESS;
  108. }