utils.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * copyright (c) Sebastien Bechet <s.bechet@av7.net>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #define SCALEBITS 8
  25. #define ONE_HALF (1 << (SCALEBITS - 1))
  26. #define FIX(x) ((int) ((x) * (1 << SCALEBITS) + 0.5))
  27. #define err_if(expr) do { \
  28. if (expr) { \
  29. fprintf(stderr, "%s\n", strerror(errno)); \
  30. exit(1); \
  31. } \
  32. } while (0)
  33. static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
  34. unsigned char *cr, const unsigned char *src,
  35. int width, int height)
  36. {
  37. int wrap, wrap3, x, y;
  38. int r, g, b, r1, g1, b1;
  39. const unsigned char *p;
  40. wrap = width;
  41. wrap3 = width * 3;
  42. p = src;
  43. for (y = 0; y < height; y += 2) {
  44. for (x = 0; x < width; x += 2) {
  45. r = p[0];
  46. g = p[1];
  47. b = p[2];
  48. r1 = r;
  49. g1 = g;
  50. b1 = b;
  51. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  52. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  53. r = p[3];
  54. g = p[4];
  55. b = p[5];
  56. r1 += r;
  57. g1 += g;
  58. b1 += b;
  59. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  60. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  61. p += wrap3;
  62. lum += wrap;
  63. r = p[0];
  64. g = p[1];
  65. b = p[2];
  66. r1 += r;
  67. g1 += g;
  68. b1 += b;
  69. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  70. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  71. r = p[3];
  72. g = p[4];
  73. b = p[5];
  74. r1 += r;
  75. g1 += g;
  76. b1 += b;
  77. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  78. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  79. cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
  80. FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  81. cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
  82. FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  83. cb++;
  84. cr++;
  85. p += -wrap3 + 2 * 3;
  86. lum += -wrap + 2;
  87. }
  88. p += wrap3;
  89. lum += wrap;
  90. }
  91. }
  92. /* cif format */
  93. #define DEFAULT_WIDTH 352
  94. #define DEFAULT_HEIGHT 288
  95. #define DEFAULT_NB_PICT 50
  96. static void pgmyuv_save(const char *filename, int w, int h,
  97. const unsigned char *rgb_tab)
  98. {
  99. FILE *f;
  100. int i, h2, w2;
  101. unsigned char *cb, *cr;
  102. unsigned char *lum_tab, *cb_tab, *cr_tab;
  103. lum_tab = malloc(w * h);
  104. cb_tab = malloc(w * h / 4);
  105. cr_tab = malloc(w * h / 4);
  106. rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
  107. if (filename) {
  108. f = fopen(filename, "wb");
  109. fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
  110. } else {
  111. f = stdout;
  112. }
  113. err_if(fwrite(lum_tab, 1, w * h, f) != w * h);
  114. h2 = h / 2;
  115. w2 = w / 2;
  116. cb = cb_tab;
  117. cr = cr_tab;
  118. if (filename) {
  119. for (i = 0; i < h2; i++) {
  120. err_if(fwrite(cb, 1, w2, f) != w2);
  121. err_if(fwrite(cr, 1, w2, f) != w2);
  122. cb += w2;
  123. cr += w2;
  124. }
  125. fclose(f);
  126. } else {
  127. for (i = 0; i < h2; i++) {
  128. err_if(fwrite(cb, 1, w2, f) != w2);
  129. cb += w2;
  130. }
  131. for (i = 0; i < h2; i++) {
  132. err_if(fwrite(cr, 1, w2, f) != w2);
  133. cr += w2;
  134. }
  135. }
  136. free(lum_tab);
  137. free(cb_tab);
  138. free(cr_tab);
  139. }
  140. static unsigned char *rgb_tab;
  141. static int width, height, wrap;
  142. static void put_pixel(int x, int y, int r, int g, int b)
  143. {
  144. unsigned char *p;
  145. if (x < 0 || x >= width ||
  146. y < 0 || y >= height)
  147. return;
  148. p = rgb_tab + y * wrap + x * 3;
  149. p[0] = r;
  150. p[1] = g;
  151. p[2] = b;
  152. }