You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.1KB

  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 <stdio.h>
  21. #include <stdlib.h>
  22. #define SCALEBITS 8
  23. #define ONE_HALF (1 << (SCALEBITS - 1))
  24. #define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
  25. static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
  26. unsigned char *cr, unsigned char *src,
  27. int width, int height)
  28. {
  29. int wrap, wrap3, x, y;
  30. int r, g, b, r1, g1, b1;
  31. unsigned char *p;
  32. wrap = width;
  33. wrap3 = width * 3;
  34. p = src;
  35. for (y = 0; y < height; y += 2) {
  36. for (x = 0; x < width; x += 2) {
  37. r = p[0];
  38. g = p[1];
  39. b = p[2];
  40. r1 = r;
  41. g1 = g;
  42. b1 = b;
  43. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  44. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  45. r = p[3];
  46. g = p[4];
  47. b = p[5];
  48. r1 += r;
  49. g1 += g;
  50. b1 += b;
  51. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  52. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  53. p += wrap3;
  54. lum += wrap;
  55. r = p[0];
  56. g = p[1];
  57. b = p[2];
  58. r1 += r;
  59. g1 += g;
  60. b1 += b;
  61. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  62. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  63. r = p[3];
  64. g = p[4];
  65. b = p[5];
  66. r1 += r;
  67. g1 += g;
  68. b1 += b;
  69. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  70. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  71. cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
  72. FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  73. cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
  74. FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  75. cb++;
  76. cr++;
  77. p += -wrap3 + 2 * 3;
  78. lum += -wrap + 2;
  79. }
  80. p += wrap3;
  81. lum += wrap;
  82. }
  83. }
  84. /* cif format */
  85. #define DEFAULT_WIDTH 352
  86. #define DEFAULT_HEIGHT 288
  87. #define DEFAULT_NB_PICT 50
  88. static void pgmyuv_save(const char *filename, int w, int h,
  89. unsigned char *rgb_tab)
  90. {
  91. FILE *f;
  92. int i, h2, w2;
  93. unsigned char *cb, *cr;
  94. unsigned char *lum_tab, *cb_tab, *cr_tab;
  95. lum_tab = malloc(w * h);
  96. cb_tab = malloc(w * h / 4);
  97. cr_tab = malloc(w * h / 4);
  98. rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
  99. f = fopen(filename, "wb");
  100. fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
  101. fwrite(lum_tab, 1, w * h, f);
  102. h2 = h / 2;
  103. w2 = w / 2;
  104. cb = cb_tab;
  105. cr = cr_tab;
  106. for (i = 0; i < h2; i++) {
  107. fwrite(cb, 1, w2, f);
  108. fwrite(cr, 1, w2, f);
  109. cb += w2;
  110. cr += w2;
  111. }
  112. fclose(f);
  113. free(lum_tab);
  114. free(cb_tab);
  115. free(cr_tab);
  116. }
  117. static unsigned char *rgb_tab;
  118. static int width, height, wrap;
  119. static void put_pixel(int x, int y, int r, int g, int b)
  120. {
  121. unsigned char *p;
  122. if (x < 0 || x >= width ||
  123. y < 0 || y >= height)
  124. return;
  125. p = rgb_tab + y * wrap + x * 3;
  126. p[0] = r;
  127. p[1] = g;
  128. p[2] = b;
  129. }