write.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Written by Bill Brown, USACERL (brown@zorro.cecer.army.mil)
  2. * May, 1994
  3. *
  4. * This code is in the public domain. Specifically, we give to the public
  5. * domain all rights for future licensing of the source code, all resale
  6. * rights, and all publishing rights.
  7. *
  8. * We ask, but do not require, that the following message be included in
  9. * all derived works:
  10. * "Portions developed at the US Army Construction Engineering
  11. * Research Laboratories, Champaign, Illinois."
  12. *
  13. * USACERL GIVES NO WARRANTY, EXPRESSED OR IMPLIED,
  14. * FOR THE SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT
  15. * LIMITATION, WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A
  16. * PARTICULAR PURPOSE.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <unistd.h>
  23. #include <grass/gis.h>
  24. #include <grass/glocale.h>
  25. #include "rom_proto.h"
  26. #ifndef USE_PPM
  27. /*******************************************************/
  28. void write_ycc(char *tr, char *tg, char *tb, int nrows, int ncols,
  29. int *y_rows, int *y_cols, char *filename)
  30. {
  31. register int x, y;
  32. register unsigned char *dy0, *dy1;
  33. register unsigned char *dcr, *dcb;
  34. register unsigned char src0[6], src1[6];
  35. static int rows, cols;
  36. static int first = 1;
  37. static float mult299[256], mult587[256], mult114[256];
  38. static float mult16874[256], mult33126[256], mult5[256];
  39. static float mult41869[256], mult08131[256];
  40. static unsigned char *cy, *cr, *cb;
  41. FILE *ofp;
  42. *y_rows = nrows;
  43. *y_cols = ncols;
  44. /* 16-pixel align */
  45. *y_rows &= ~0x0f;
  46. *y_cols &= ~0x0f;
  47. if (first) {
  48. register int index;
  49. rows = *y_rows;
  50. cols = *y_cols;
  51. for (index = 0; index <= 255; index++) {
  52. mult299[index] = index * 0.29900;
  53. mult587[index] = index * 0.58700;
  54. mult114[index] = index * 0.11400;
  55. mult16874[index] = -0.16874 * index;
  56. mult33126[index] = -0.33126 * index;
  57. mult5[index] = index * 0.50000;
  58. mult41869[index] = -0.41869 * index;
  59. mult08131[index] = -0.08131 * index;
  60. }
  61. cy = (unsigned char *)G_malloc(rows * cols * sizeof(unsigned char));
  62. cr = (unsigned char *)G_malloc((rows / 2) * (cols / 2)
  63. * sizeof(unsigned char));
  64. cb = (unsigned char *)G_malloc((rows / 2) * (cols / 2)
  65. * sizeof(unsigned char));
  66. first = 0;
  67. }
  68. if (*y_rows != rows || *y_cols != cols)
  69. G_fatal_error(_("Size mismatch error!"));
  70. for (y = 0; y < rows - 1; y += 2) {
  71. dy0 = &cy[y * cols];
  72. dy1 = &cy[(y + 1) * cols];
  73. dcr = &cr[(cols / 2) * (y / 2)];
  74. dcb = &cb[(cols / 2) * (y / 2)];
  75. for (x = 0; x < cols - 1; x += 2, dy0 += 2, dy1 += 2, dcr++, dcb++) {
  76. src0[0] = tr[y * ncols + x];
  77. src0[1] = tg[y * ncols + x];
  78. src0[2] = tb[y * ncols + x];
  79. src0[3] = tr[y * ncols + x + 1];
  80. src0[4] = tg[y * ncols + x + 1];
  81. src0[5] = tb[y * ncols + x + 1];
  82. src0[0] = tr[(y + 1) * ncols + x];
  83. src0[1] = tg[(y + 1) * ncols + x];
  84. src0[2] = tb[(y + 1) * ncols + x];
  85. src0[3] = tr[(y + 1) * ncols + x + 1];
  86. src0[4] = tg[(y + 1) * ncols + x + 1];
  87. src0[5] = tb[(y + 1) * ncols + x + 1];
  88. *dy0 = (mult299[*src0] + mult587[src0[1]] + mult114[src0[2]]);
  89. *dy1 = (mult299[*src1] + mult587[src1[1]] + mult114[src1[2]]);
  90. dy0[1] = (mult299[src0[3]] + mult587[src0[4]] + mult114[src0[5]]);
  91. dy1[1] = (mult299[src1[3]] + mult587[src1[4]] + mult114[src1[5]]);
  92. *dcb = ((mult16874[*src0] +
  93. mult33126[src0[1]] +
  94. mult5[src0[2]] +
  95. mult16874[*src1] +
  96. mult33126[src1[1]] +
  97. mult5[src1[2]] +
  98. mult16874[src0[3]] +
  99. mult33126[src0[4]] +
  100. mult5[src0[5]] +
  101. mult16874[src1[3]] +
  102. mult33126[src1[4]] + mult5[src1[5]]) / 4) + 128;
  103. *dcr = ((mult5[*src0] +
  104. mult41869[src0[1]] +
  105. mult08131[src0[2]] +
  106. mult5[*src1] +
  107. mult41869[src1[1]] +
  108. mult08131[src1[2]] +
  109. mult5[src0[3]] +
  110. mult41869[src0[4]] +
  111. mult08131[src0[5]] +
  112. mult5[src1[3]] +
  113. mult41869[src1[4]] + mult08131[src1[5]]) / 4) + 128;
  114. }
  115. }
  116. if (NULL == (ofp = fopen(filename, "wb")))
  117. G_fatal_error(_("Unable to open output file"));
  118. for (y = 0; y < rows; y++)
  119. fwrite(cy + (y * cols), 1, cols, ofp);
  120. for (y = 0; y < rows / 2; y++)
  121. fwrite(cb + ((y / 2) * cols), 1, cols / 2, ofp);
  122. for (y = 0; y < rows / 2; y++)
  123. fwrite(cr + ((y / 2) * cols), 1, cols / 2, ofp);
  124. fclose(ofp);
  125. }
  126. #endif
  127. /*******************************************************/
  128. void write_ppm(char *tr, char *tg, char *tb, int nrows, int ncols,
  129. int *y_rows, int *y_cols, char *filename)
  130. {
  131. register int x, y;
  132. static int rows, cols;
  133. static int first = 1;
  134. FILE *ofp;
  135. *y_rows = nrows;
  136. *y_cols = ncols;
  137. /* 16-pixel align */
  138. *y_rows &= ~0x0f;
  139. *y_cols &= ~0x0f;
  140. if (first) {
  141. rows = *y_rows;
  142. cols = *y_cols;
  143. first = 0;
  144. }
  145. if (*y_rows != rows || *y_cols != cols)
  146. G_fatal_error(_("Size mismatch error!"));
  147. if (NULL == (ofp = fopen(filename, "w")))
  148. G_fatal_error(_("Unable to open output file"));
  149. fprintf(ofp, "P6\n");
  150. /* Magic number meaning rawbits, 24bit color to ppm format */
  151. fprintf(ofp, "%d %d\n", cols, rows);
  152. /* width & height */
  153. fprintf(ofp, "255\n");
  154. /* max intensity val */
  155. for (y = 0; y < rows; y++) {
  156. for (x = 0; x < cols; x++) {
  157. putc(*tr++, ofp);
  158. putc(*tg++, ofp);
  159. putc(*tb++, ofp);
  160. }
  161. tr += (ncols - cols);
  162. tg += (ncols - cols);
  163. tb += (ncols - cols);
  164. }
  165. fclose(ofp);
  166. }
  167. /*******************************************************/
  168. void write_params(char *mpfilename, char *yfiles[], char *outfile,
  169. int frames, int quality, int y_rows, int y_cols, int fly)
  170. {
  171. FILE *fp;
  172. char dir[1000], *enddir;
  173. int i, dirlen = 0;
  174. if (NULL == (fp = fopen(mpfilename, "w")))
  175. G_fatal_error(_("Unable to create temporary files."));
  176. if (!fly) {
  177. strcpy(dir, yfiles[0]);
  178. enddir = strrchr(dir, '/');
  179. if (enddir) {
  180. *enddir = '\0';
  181. dirlen = strlen(dir) + 1;
  182. }
  183. }
  184. switch (quality) {
  185. case 1:
  186. fprintf(fp, "PATTERN IBPB\n");
  187. break;
  188. case 2:
  189. case 3:
  190. fprintf(fp, "PATTERN IBBPBB\n");
  191. break;
  192. case 4:
  193. case 5:
  194. fprintf(fp, "PATTERN IBBPBBPBB\n");
  195. break;
  196. default:
  197. fprintf(fp, "PATTERN IBBPBB\n");
  198. break;
  199. }
  200. fprintf(fp, "FORCE_ENCODE_LAST_FRAME\n");
  201. fprintf(fp, "OUTPUT %s\n", outfile);
  202. fprintf(fp, "\n");
  203. if (!fly)
  204. fprintf(fp, "INPUT_DIR %s\n", dir);
  205. else
  206. fprintf(fp, "INPUT_DIR %s\n", "in=");
  207. fprintf(fp, "INPUT\n");
  208. if (!fly)
  209. for (i = 0; i < frames; i++)
  210. fprintf(fp, "%s\n", yfiles[i] + dirlen);
  211. else
  212. for (i = 0; i < frames; i++)
  213. fprintf(fp, "%s\n", yfiles[i]);
  214. fprintf(fp, "END_INPUT\n");
  215. #ifdef USE_PPM
  216. fprintf(fp, "BASE_FILE_FORMAT PPM\n");
  217. #else
  218. if (!fly)
  219. fprintf(fp, "BASE_FILE_FORMAT YUV\n");
  220. else
  221. fprintf(fp, "BASE_FILE_FORMAT PPM\n");
  222. #endif
  223. #ifndef USE_PPM
  224. if (!fly)
  225. fprintf(fp, "YUV_SIZE %dx%d\n", y_cols, y_rows);
  226. #endif
  227. if (!fly)
  228. fprintf(fp, "INPUT_CONVERT *\n");
  229. else
  230. fprintf(fp, "INPUT_CONVERT r.out.ppm -q * out=-\n");
  231. fprintf(fp, "GOP_SIZE 30\n");
  232. fprintf(fp, "SLICES_PER_FRAME 1\n");
  233. fprintf(fp, "\n");
  234. fprintf(fp, "PIXEL HALF\n");
  235. fprintf(fp, "RANGE 8\n");
  236. fprintf(fp, "\n");
  237. fprintf(fp, "PSEARCH_ALG TWOLEVEL\n");
  238. fprintf(fp, "BSEARCH_ALG CROSS2\n");
  239. fprintf(fp, "\n");
  240. switch (quality) {
  241. case 1:
  242. fprintf(fp, "IQSCALE 5\n");
  243. fprintf(fp, "PQSCALE 8\n");
  244. fprintf(fp, "BQSCALE 12\n");
  245. break;
  246. case 2:
  247. fprintf(fp, "IQSCALE 6\n");
  248. fprintf(fp, "PQSCALE 10\n");
  249. fprintf(fp, "BQSCALE 14\n");
  250. break;
  251. case 4:
  252. fprintf(fp, "IQSCALE 8\n");
  253. fprintf(fp, "PQSCALE 14\n");
  254. fprintf(fp, "BQSCALE 20\n");
  255. break;
  256. case 5:
  257. fprintf(fp, "IQSCALE 9\n");
  258. fprintf(fp, "PQSCALE 16\n");
  259. fprintf(fp, "BQSCALE 24\n");
  260. break;
  261. default:
  262. fprintf(fp, "IQSCALE 7\n");
  263. fprintf(fp, "PQSCALE 12\n");
  264. fprintf(fp, "BQSCALE 16\n");
  265. break;
  266. }
  267. fprintf(fp, "\n");
  268. fprintf(fp, "REFERENCE_FRAME DECODED\n");
  269. fclose(fp);
  270. }
  271. /*******************************************************/
  272. void clean_files(char *file, char *files[], int num)
  273. {
  274. int i;
  275. remove(file);
  276. for (i = 0; i < num; i++)
  277. remove(files[i]);
  278. }