image_tools.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*Daala video codec
  2. Copyright (c) 2013 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. #include <string.h>
  21. #include "od_defs.h"
  22. #include "od_filter.h"
  23. #include "od_intra.h"
  24. #include "image_tools.h"
  25. #include "../src/dct.h"
  26. #include <stdlib.h>
  27. od_rgba16_pixel COLORS[OD_INTRA_NMODES];
  28. void image_draw_block(od_rgba16_image *_image,int _x,int _y,
  29. const unsigned char *_block,int _stride){
  30. od_rgba16_pixel color;
  31. int i;
  32. int j;
  33. color[3]=(unsigned short)0xFFFFU;
  34. for(i=0;i<B_SZ;i++){
  35. for(j=0;j<B_SZ;j++){
  36. color[0]=color[1]=color[2]=_block[_stride*i+j]*0x101;
  37. od_rgba16_image_draw_point(_image,_x+j,_y+i,color);
  38. }
  39. }
  40. }
  41. int image_write_png(od_rgba16_image *_image,const char *_name){
  42. char fout_name[8192];
  43. FILE *fout;
  44. sprintf(fout_name,"%s.png",_name);
  45. fout=fopen(fout_name,"wb");
  46. if(fout==NULL){
  47. fprintf(stderr,"Could not open '%s' for reading.\n",fout_name);
  48. return EXIT_FAILURE;
  49. }
  50. od_rgba16_image_write_png(_image,fout);
  51. fclose(fout);
  52. return EXIT_SUCCESS;
  53. }
  54. void image_files_init(image_files *_this,int _nxblocks,int _nyblocks){
  55. od_rgba16_image_init(&_this->raw,B_SZ*_nxblocks,B_SZ*_nyblocks);
  56. od_rgba16_image_init(&_this->map,_nxblocks,_nyblocks);
  57. od_rgba16_image_init(&_this->pred,B_SZ*_nxblocks,B_SZ*_nyblocks);
  58. od_rgba16_image_init(&_this->res,B_SZ*_nxblocks,B_SZ*_nyblocks);
  59. }
  60. void image_files_clear(image_files *_this){
  61. od_rgba16_image_clear(&_this->raw);
  62. od_rgba16_image_clear(&_this->map);
  63. od_rgba16_image_clear(&_this->pred);
  64. od_rgba16_image_clear(&_this->res);
  65. }
  66. void image_files_write(image_files *_this,const char *_name,const char *_suf){
  67. char name[8192];
  68. sprintf(name,"%s-raw%s",_name,_suf==NULL?"":_suf);
  69. image_write_png(&_this->raw,name);
  70. sprintf(name,"%s-map%s",_name,_suf==NULL?"":_suf);
  71. image_write_png(&_this->map,name);
  72. sprintf(name,"%s-pred%s",_name,_suf==NULL?"":_suf);
  73. image_write_png(&_this->pred,name);
  74. sprintf(name,"%s-res%s",_name,_suf==NULL?"":_suf);
  75. image_write_png(&_this->res,name);
  76. }
  77. static void od_pre_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  78. int _in_stride,int _bx,int _by){
  79. int by;
  80. int bx;
  81. int j;
  82. int i;
  83. for(by=0;by<_by;by++){
  84. int y;
  85. y=B_SZ*by;
  86. for(bx=0;bx<_bx;bx++){
  87. int x;
  88. x=B_SZ*bx;
  89. for(i=0;i<B_SZ;i++){
  90. od_coeff col[B_SZ];
  91. #if APPLY_FILTER
  92. for(j=0;j<B_SZ;j++){
  93. col[j]=_in[_in_stride*(y+j)+x+i];
  94. }
  95. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  96. (*NE_PRE_FILTER[B_SZ_LOG-OD_LOG_BSIZE0])(col,col);
  97. #else
  98. # error "Need a prefilter implementation for this block size."
  99. #endif
  100. for(j=0;j<B_SZ;j++){
  101. _out[_out_stride*(y+j)+x+i]=col[j];
  102. }
  103. #else
  104. for(j=0;j<B_SZ;j++){
  105. _out[_out_stride*(y+j)+x+i]=_in[_in_stride*(y+j)+x+i];
  106. }
  107. #endif
  108. }
  109. #if APPLY_FILTER
  110. for(j=0;j<B_SZ;j++){
  111. od_coeff *row;
  112. row=&_out[_out_stride*(y+j)+x];
  113. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  114. (*NE_PRE_FILTER[B_SZ_LOG-OD_LOG_BSIZE0])(row,row);
  115. #else
  116. # error "Need a prefilter implementation for this block size."
  117. #endif
  118. }
  119. #endif
  120. }
  121. }
  122. }
  123. static void od_post_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  124. int _in_stride,int _bx,int _by){
  125. int bx;
  126. int by;
  127. int j;
  128. int i;
  129. for(by=0;by<_by;by++){
  130. int y;
  131. y=B_SZ*by;
  132. for(bx=0;bx<_bx;bx++){
  133. int x;
  134. x=B_SZ*bx;
  135. for(j=0;j<B_SZ;j++){
  136. od_coeff *row;
  137. for(i=0;i<B_SZ;i++){
  138. _out[_out_stride*(y+j)+x+i]=_in[_in_stride*(y+j)+x+i];
  139. }
  140. #if APPLY_FILTER
  141. row=&_out[_out_stride*(y+j)+x];
  142. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  143. (*NE_POST_FILTER[B_SZ_LOG-OD_LOG_BSIZE0])(row,row);
  144. #else
  145. # error "Need a postfilter implementation for this block size."
  146. #endif
  147. #endif
  148. }
  149. #if APPLY_FILTER
  150. for(i=0;i<B_SZ;i++){
  151. od_coeff col[B_SZ];
  152. for(j=0;j<B_SZ;j++){
  153. col[j]=_out[_out_stride*(y+j)+x+i];
  154. }
  155. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  156. (*NE_POST_FILTER[B_SZ_LOG-OD_LOG_BSIZE0])(col,col);
  157. #else
  158. # error "Need a postfilter implementation for this block size."
  159. #endif
  160. for(j=0;j<B_SZ;j++){
  161. _out[_out_stride*(j+y)+x+i]=col[j];
  162. }
  163. }
  164. #endif
  165. }
  166. }
  167. }
  168. static void od_fdct_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  169. int _in_stride,int _bx,int _by){
  170. int bx;
  171. int by;
  172. for(by=0;by<_by;by++){
  173. int y;
  174. y=B_SZ*by;
  175. for(bx=0;bx<_bx;bx++){
  176. int x;
  177. x=B_SZ*bx;
  178. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  179. (*OD_FDCT_2D[B_SZ_LOG-OD_LOG_BSIZE0])(&_out[_out_stride*y+x],_out_stride,
  180. &_in[_in_stride*y+x],_in_stride);
  181. #else
  182. # error "Need an fDCT implementation for this block size."
  183. #endif
  184. }
  185. }
  186. }
  187. static void od_idct_blocks(od_coeff *_out,int _out_stride,od_coeff *_in,
  188. int _in_stride,int _bx,int _by){
  189. int bx;
  190. int by;
  191. for(by=0;by<_by;by++){
  192. int y;
  193. y=B_SZ*by;
  194. for(bx=0;bx<_bx;bx++){
  195. int x;
  196. x=B_SZ*bx;
  197. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  198. (*OD_IDCT_2D[B_SZ_LOG-OD_LOG_BSIZE0])(&_out[_out_stride*y+x],_out_stride,
  199. &_in[_in_stride*y+x],_in_stride);
  200. #else
  201. # error "Need an iDCT implementation for this block size."
  202. #endif
  203. }
  204. }
  205. }
  206. void image_data_init(image_data *_this,const char *_name,int _nxblocks,
  207. int _nyblocks){
  208. int w;
  209. int h;
  210. _this->name=_name;
  211. _this->nxblocks=_nxblocks;
  212. _this->nyblocks=_nyblocks;
  213. _this->mode=(unsigned char *)malloc(sizeof(*_this->mode)*_nxblocks*_nyblocks);
  214. _this->weight=(double *)malloc(sizeof(*_this->weight)*_nxblocks*_nyblocks);
  215. w=B_SZ*(_nxblocks+3);
  216. h=B_SZ*(_nyblocks+3);
  217. _this->pre=(od_coeff *)malloc(sizeof(*_this->pre)*w*h);
  218. _this->pre_stride=w;
  219. w=B_SZ*(_nxblocks+2);
  220. h=B_SZ*(_nyblocks+2);
  221. _this->fdct=(od_coeff *)malloc(sizeof(*_this->fdct)*w*h);
  222. _this->fdct_stride=w;
  223. w=B_SZ*(_nxblocks+0);
  224. h=B_SZ*(_nyblocks+0);
  225. _this->pred=(double *)malloc(sizeof(*_this->pred)*w*h);
  226. _this->pred_stride=w;
  227. w=B_SZ*(_nxblocks+2);
  228. h=B_SZ*(_nyblocks+2);
  229. _this->idct=(od_coeff *)malloc(sizeof(*_this->idct)*w*h);
  230. _this->idct_stride=w;
  231. w=B_SZ*(_nxblocks+1);
  232. h=B_SZ*(_nyblocks+1);
  233. _this->post=(od_coeff *)malloc(sizeof(*_this->post)*w*h);
  234. _this->post_stride=w;
  235. }
  236. void image_data_clear(image_data *_this){
  237. free(_this->mode);
  238. free(_this->weight);
  239. free(_this->pre);
  240. free(_this->fdct);
  241. free(_this->pred);
  242. free(_this->idct);
  243. free(_this->post);
  244. }
  245. void image_data_pre_block(image_data *_this,const unsigned char *_data,
  246. int _stride,int _bi,int _bj){
  247. int x0;
  248. int y0;
  249. int bx;
  250. int by;
  251. int x;
  252. int y;
  253. int bi;
  254. int bj;
  255. int i;
  256. int j;
  257. od_coeff buf[B_SZ*B_SZ];
  258. x0=-(B_SZ>>1);
  259. y0=-(B_SZ>>1);
  260. bx=by=1;
  261. if(_bi==0){
  262. x0-=B_SZ;
  263. bx++;
  264. }
  265. if(_bj==0){
  266. y0-=B_SZ;
  267. by++;
  268. }
  269. if(_bi==_this->nxblocks-1){
  270. bx+=2;
  271. }
  272. if(_bj==_this->nyblocks-1){
  273. by+=2;
  274. }
  275. x=x0+_bi*B_SZ+(3*B_SZ>>1);
  276. y=y0+_bj*B_SZ+(3*B_SZ>>1);
  277. for(bj=0;bj<by;bj++){
  278. for(bi=0;bi<bx;bi++){
  279. for(j=0;j<B_SZ;j++){
  280. for(i=0;i<B_SZ;i++){
  281. buf[B_SZ*j+i]=
  282. (_data[_stride*(y0+B_SZ*bj+j)+x0+B_SZ*bi+i]-128)*INPUT_SCALE;
  283. }
  284. }
  285. od_pre_blocks(&_this->pre[_this->pre_stride*(y+B_SZ*bj)+x+B_SZ*bi],
  286. _this->pre_stride,buf,B_SZ,1,1);
  287. }
  288. }
  289. }
  290. void image_data_fdct_block(image_data *_this,int _bi,int _bj){
  291. int x0;
  292. int y0;
  293. int bx;
  294. int by;
  295. int x;
  296. int y;
  297. x0=_bi*B_SZ+(3*B_SZ>>1);
  298. y0=_bj*B_SZ+(3*B_SZ>>1);
  299. bx=by=1;
  300. if(_bi==0){
  301. x0-=B_SZ;
  302. bx++;
  303. }
  304. if(_bj==0){
  305. y0-=B_SZ;
  306. by++;
  307. }
  308. if(_bi==_this->nxblocks-1){
  309. bx++;
  310. }
  311. if(_bj==_this->nyblocks-1){
  312. by++;
  313. }
  314. x=x0-(B_SZ>>1);
  315. y=y0-(B_SZ>>1);
  316. od_fdct_blocks(&_this->fdct[_this->fdct_stride*y+x],_this->fdct_stride,
  317. &_this->pre[_this->pre_stride*y0+x0],_this->pre_stride,bx,by);
  318. }
  319. void image_data_print_block(image_data *_this,int _bi,int _bj,FILE *_fp){
  320. int by;
  321. int bx;
  322. int j;
  323. int i;
  324. fprintf(_fp,"%i",_this->mode[_this->nxblocks*_bj+_bi]);
  325. for(by=0;by<=1;by++){
  326. for(bx=0;bx<=2-by;bx++){
  327. od_coeff *block;
  328. block=&_this->fdct[_this->fdct_stride*B_SZ*(_bj+by)+B_SZ*(_bi+bx)];
  329. for(j=0;j<B_SZ;j++){
  330. for(i=0;i<B_SZ;i++){
  331. fprintf(_fp," %i",block[_this->fdct_stride*j+i]);
  332. }
  333. }
  334. }
  335. }
  336. fprintf(_fp,"\n");
  337. fflush(_fp);
  338. }
  339. void image_data_pred_block(image_data *_this,int _bi,int _bj){
  340. double *pred;
  341. od_coeff *fdct;
  342. int mode;
  343. pred=&_this->pred[_this->pred_stride*B_SZ*_bj+B_SZ*_bi];
  344. fdct=&_this->fdct[_this->fdct_stride*B_SZ*(_bj+1)+B_SZ*(_bi+1)];
  345. mode=_this->mode[_this->nxblocks*_bj+_bi];
  346. #if B_SZ_LOG>=OD_LOG_BSIZE0&&B_SZ_LOG<OD_LOG_BSIZE0+OD_NBSIZES
  347. (*NE_INTRA_MULT[B_SZ_LOG-OD_LOG_BSIZE0])(pred,_this->pred_stride,fdct,
  348. _this->fdct_stride,mode);
  349. #else
  350. # error "Need a predictor implementation for this block size."
  351. #endif
  352. }
  353. void image_data_stats_block(image_data *_this,const unsigned char *_data,
  354. int _stride,int _bi,int _bj,intra_stats *_stats){
  355. int mode;
  356. od_coeff *ref;
  357. double *pred;
  358. int j;
  359. int i;
  360. double buf[B_SZ*B_SZ];
  361. mode=_this->mode[_this->nxblocks*_bj+_bi];
  362. ref=&_this->fdct[_this->fdct_stride*B_SZ*(_bj+1)+B_SZ*(_bi+1)];
  363. pred=&_this->pred[_this->pred_stride*B_SZ*_bj+B_SZ*_bi];
  364. for(j=0;j<B_SZ;j++){
  365. for(i=0;i<B_SZ;i++){
  366. buf[B_SZ*j+i]=ref[_this->fdct_stride*j+i]-pred[_this->pred_stride*j+i];
  367. }
  368. }
  369. intra_stats_update(_stats,_data,_stride,mode,ref,_this->fdct_stride,buf,B_SZ);
  370. }
  371. void image_data_idct_block(image_data *_this,int _bi,int _bj){
  372. int x0;
  373. int y0;
  374. int x;
  375. int y;
  376. int bx;
  377. int by;
  378. int j;
  379. int i;
  380. double *p;
  381. od_coeff buf[B_SZ*B_SZ];
  382. x0=B_SZ*_bi;
  383. y0=B_SZ*_bj;
  384. x=x0+B_SZ;
  385. y=y0+B_SZ;
  386. bx=by=1;
  387. if(_bi==0){
  388. x-=B_SZ;
  389. bx++;
  390. }
  391. if(_bj==0){
  392. y-=B_SZ;
  393. by++;
  394. }
  395. if(_bi==_this->nxblocks-1){
  396. bx++;
  397. }
  398. if(_bj==_this->nyblocks-1){
  399. by++;
  400. }
  401. /* TODO remove redundant computations here */
  402. if(bx!=1||by!=1){
  403. od_idct_blocks(&_this->idct[_this->idct_stride*y+x],_this->idct_stride,
  404. &_this->fdct[_this->fdct_stride*y+x],_this->fdct_stride,bx,by);
  405. }
  406. p=&_this->pred[_this->pred_stride*y0+x0];
  407. for(j=0;j<B_SZ;j++){
  408. for(i=0;i<B_SZ;i++){
  409. buf[j*B_SZ+i]=(od_coeff)floor(p[_this->pred_stride*j+i]+0.5);
  410. }
  411. }
  412. x=x0+B_SZ;
  413. y=y0+B_SZ;
  414. od_idct_blocks(&_this->idct[_this->idct_stride*y+x],_this->idct_stride,
  415. buf,B_SZ,1,1);
  416. }
  417. void image_data_post_block(image_data *_this,int _bi,int _bj){
  418. int x0;
  419. int y0;
  420. int x;
  421. int y;
  422. int bx;
  423. int by;
  424. x=B_SZ*_bi;
  425. y=B_SZ*_bj;
  426. x0=x+(B_SZ>>1);
  427. y0=y+(B_SZ>>1);
  428. bx=by=1;
  429. if(_bi==_this->nxblocks-1){
  430. bx++;
  431. }
  432. if(_bj==_this->nyblocks-1){
  433. by++;
  434. }
  435. od_post_blocks(&_this->post[_this->post_stride*y+x],_this->post_stride,
  436. &_this->idct[_this->idct_stride*y0+x0],_this->idct_stride,bx,by);
  437. }
  438. void image_data_files_block(image_data *_this,const unsigned char *_data,
  439. int _stride,int _bi,int _bj,image_files *_files){
  440. int mode;
  441. od_coeff *p;
  442. int j;
  443. int i;
  444. od_coeff v;
  445. unsigned char buf[B_SZ*B_SZ];
  446. mode=_this->mode[_bj*_this->nxblocks+_bi];
  447. od_rgba16_image_draw_point(&_files->map,_bi,_bj,COLORS[mode]);
  448. p=&_this->pre[_this->pre_stride*(B_SZ*_bj+(3*B_SZ>>1))+B_SZ*_bi+(3*B_SZ>>1)];
  449. for(j=0;j<B_SZ;j++){
  450. for(i=0;i<B_SZ;i++){
  451. v=(p[_this->pre_stride*j+i]+INPUT_SCALE*128+INPUT_SCALE/2)/INPUT_SCALE;
  452. buf[B_SZ*j+i]=OD_CLAMPI(0,v,255);
  453. }
  454. }
  455. image_draw_block(&_files->raw,B_SZ*_bi,B_SZ*_bj,buf,B_SZ);
  456. p=&_this->post[_this->post_stride*(B_SZ*_bj+(B_SZ>>1))+B_SZ*_bi+(B_SZ>>1)];
  457. for(j=0;j<B_SZ;j++){
  458. for(i=0;i<B_SZ;i++){
  459. v=(p[_this->post_stride*j+i]+INPUT_SCALE*128+INPUT_SCALE/2)/INPUT_SCALE;
  460. buf[B_SZ*j+i]=OD_CLAMPI(0,v,255);
  461. }
  462. }
  463. image_draw_block(&_files->pred,B_SZ*_bi,B_SZ*_bj,buf,B_SZ);
  464. for(j=0;j<B_SZ;j++){
  465. for(i=0;i<B_SZ;i++){
  466. buf[B_SZ*j+i]=OD_CLAMPI(0,_data[_stride*j+i]-buf[B_SZ*j+i]+128,255);
  467. }
  468. }
  469. image_draw_block(&_files->res,B_SZ*_bi,B_SZ*_bj,buf,B_SZ);
  470. }
  471. int image_data_save_map(image_data *_this){
  472. char name[8192];
  473. char *pos;
  474. int eos;
  475. FILE *fout;
  476. strcpy(name,_this->name);
  477. pos=strrchr(name,'.');
  478. if(!pos){
  479. eos=strlen(name);
  480. }
  481. else{
  482. eos=pos-name;
  483. }
  484. sprintf(&name[eos],".map");
  485. fout=fopen(name,"wb");
  486. if(fout==NULL){
  487. fprintf(stderr,"Error opening output file '%s'.\n",name);
  488. return EXIT_FAILURE;
  489. }
  490. if(fwrite(_this->mode,
  491. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->mode),1,fout)<1){
  492. fprintf(stderr,"Error writing to output file '%s'.\n",name);
  493. return EXIT_FAILURE;
  494. }
  495. if(fwrite(_this->weight,
  496. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->weight),1,fout)<1){
  497. fprintf(stderr,"Error writing to output file '%s'.\n",name);
  498. return EXIT_FAILURE;
  499. }
  500. fclose(fout);
  501. return EXIT_SUCCESS;
  502. }
  503. int image_data_load_map(image_data *_this){
  504. char name[8192];
  505. char *pos;
  506. int eos;
  507. FILE *fin;
  508. strcpy(name,_this->name);
  509. pos=strrchr(name,'.');
  510. if(!pos){
  511. eos=strlen(name);
  512. }
  513. else{
  514. eos=pos-name;
  515. }
  516. sprintf(&name[eos],".map");
  517. fin=fopen(name,"rb");
  518. if(fin==NULL){
  519. fprintf(stderr,"Error opening input file '%s'.\n",name);
  520. return EXIT_FAILURE;
  521. }
  522. if(fread(_this->mode,
  523. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->mode),1,fin)<1) {
  524. fprintf(stderr,"Error reading from input file '%s'.\n",name);
  525. return EXIT_FAILURE;
  526. }
  527. if(fread(_this->weight,
  528. _this->nxblocks*(size_t)_this->nyblocks*sizeof(*_this->weight),1,fin)<1) {
  529. fprintf(stderr,"Error reading from input file '%s'.\n",name);
  530. return EXIT_FAILURE;
  531. }
  532. fclose(fin);
  533. return EXIT_SUCCESS;
  534. }