Welcome to mirror list, hosted at ThFree Co, Russian Federation.

avi_rgb.c « intern « avi « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffc3d0477fa0a6889d96698da8ae2c3cfea7bc8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2001-2002 NaN Holding BV. All rights reserved. */

/** \file
 * \ingroup avi
 *
 * This is external code. Converts RGB-type AVI files.
 */

#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "AVI_avi.h"
#include "avi_rgb.h"

#include "IMB_imbuf.h"

#include "BLI_utildefines.h"

/* implementation */

void *avi_converter_from_avi_rgb(AviMovie *movie,
                                 int stream,
                                 unsigned char *buffer,
                                 const size_t *size)
{
  unsigned char *buf;
  AviBitmapInfoHeader *bi;
  short bits = 32;

  (void)size; /* unused */

  bi = (AviBitmapInfoHeader *)movie->streams[stream].sf;
  if (bi) {
    bits = bi->BitCount;
  }

  if (bits == 16) {
    unsigned short *pxl;
    unsigned char *to;
#ifdef __BIG_ENDIAN__
    unsigned char *pxla;
#endif

    buf = imb_alloc_pixels(
        movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromavirgbbuf");

    if (buf) {
      size_t y = movie->header->Height;
      to = buf;

      while (y--) {
        pxl = (unsigned short *)(buffer + y * movie->header->Width * 2);

#ifdef __BIG_ENDIAN__
        pxla = (unsigned char *)pxl;
#endif

        size_t x = movie->header->Width;
        while (x--) {
#ifdef __BIG_ENDIAN__
          int i = pxla[0];
          pxla[0] = pxla[1];
          pxla[1] = i;

          pxla += 2;
#endif

          *(to++) = ((*pxl >> 10) & 0x1f) * 8;
          *(to++) = ((*pxl >> 5) & 0x1f) * 8;
          *(to++) = (*pxl & 0x1f) * 8;
          pxl++;
        }
      }
    }

    MEM_freeN(buffer);

    return buf;
  }

  buf = imb_alloc_pixels(
      movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromavirgbbuf");

  if (buf) {
    size_t rowstride = movie->header->Width * 3;
    BLI_assert(bits != 16);
    if (movie->header->Width % 2) {
      rowstride++;
    }

    for (size_t y = 0; y < movie->header->Height; y++) {
      memcpy(&buf[y * movie->header->Width * 3],
             &buffer[((movie->header->Height - 1) - y) * rowstride],
             movie->header->Width * 3);
    }

    for (size_t y = 0; y < (size_t)movie->header->Height * (size_t)movie->header->Width * 3;
         y += 3) {
      int i = buf[y];
      buf[y] = buf[y + 2];
      buf[y + 2] = i;
    }
  }

  MEM_freeN(buffer);

  return buf;
}

void *avi_converter_to_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, size_t *size)
{
  unsigned char *buf;

  (void)stream; /* unused */

  size_t rowstride = movie->header->Width * 3;
  /* AVI files has uncompressed lines 4-byte aligned */
  rowstride = (rowstride + 3) & ~3;

  *size = movie->header->Height * rowstride;
  buf = MEM_mallocN(*size, "toavirgbbuf");

  for (size_t y = 0; y < movie->header->Height; y++) {
    memcpy(&buf[y * rowstride],
           &buffer[((movie->header->Height - 1) - y) * movie->header->Width * 3],
           movie->header->Width * 3);
  }

  for (size_t y = 0; y < movie->header->Height; y++) {
    for (size_t x = 0; x < movie->header->Width * 3; x += 3) {
      int i = buf[y * rowstride + x];
      buf[y * rowstride + x] = buf[y * rowstride + x + 2];
      buf[y * rowstride + x + 2] = i;
    }
  }

  MEM_freeN(buffer);

  return buf;
}