AVPKit
IPixelFormat.cpp
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit 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
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #include <stdexcept>
21 
22 #include <com/avpkit/ferry/RefPointer.h>
23 #include <com/avpkit/ferry/Logger.h>
24 #include <com/avpkit/ferry/IBuffer.h>
25 #include <com/avpkit/core/IVideoPicture.h>
26 #include <com/avpkit/core/IPixelFormat.h>
27 #include <com/avpkit/core/PixelFormat.h>
28 
29 VS_LOG_SETUP(VS_CPP_PACKAGE);
30 
31 namespace com { namespace avpkit { namespace core
32 {
33  using namespace com::avpkit::ferry;
34 
35  IPixelFormat :: IPixelFormat()
36  {
37 
38  }
39 
40  IPixelFormat :: ~IPixelFormat()
41  {
42 
43  }
44 
45  unsigned char
46  IPixelFormat :: getYUV420PPixel(IVideoPicture *frame, int x, int y, YUVColorComponent c)
47  {
48  unsigned char result = 0;
49 
50  int offset = getYUV420PPixelOffset(frame, x, y, c);
51  RefPointer<IBuffer> buffer = frame->getData();
52  unsigned char * bytes = (unsigned char*)buffer->getBytes(0, offset+1);
53 
54  if (!bytes)
55  throw std::runtime_error("could not find bytes in frame");
56  result = bytes[offset];
57  return result;
58  }
59 
60  void
61  IPixelFormat :: setYUV420PPixel(IVideoPicture *frame, int x, int y, YUVColorComponent c, unsigned char value)
62  {
63  int offset = getYUV420PPixelOffset(frame, x, y, c);
64  RefPointer<IBuffer> buffer = frame->getData();
65  unsigned char * bytes = (unsigned char*)buffer->getBytes(0, offset+1);
66 
67  if (!bytes)
68  {
69  VS_LOG_DEBUG("Could not find buffer of length: %d", offset+1);
70  throw std::runtime_error("could not find bytes in frame");
71  }
72  bytes[offset] = value;
73  }
74 
75  int
76  IPixelFormat :: getYUV420PPixelOffset(IVideoPicture *frame, int x, int y, YUVColorComponent c)
77  {
78  if (!frame)
79  throw std::runtime_error("no frame");
80 
81  int width = frame->getWidth();
82  if (x < 0 || x >= width)
83  throw std::runtime_error("x value invalid for input frame");
84 
85  int height = frame->getHeight();
86  if (y < 0 || y >= height)
87  throw std::runtime_error("y value invalid for input frame");
88 
89  if (frame->getPixelType() != YUV420P)
90  throw std::runtime_error("pixel type of input frame is incorrect");
91 
92  int offset = PixelFormat::getFastYUV420PPixelOffset(frame->getWidth(), frame->getHeight(), x, y, c);
93  VS_LOG_TRACE("w: %d; h: %d; x: %d; y: %d; c: %d; offset: %d",
94  frame->getWidth(), frame->getHeight(), x, y, c, offset);
95  return offset;
96 
97  }
98 
99 }}}
virtual com::avpkit::ferry::IBuffer * getData()=0
Get any underlying raw data available for this object.
static unsigned char getYUV420PPixel(IVideoPicture *frame, int x, int y, YUVColorComponent c)
Returns the byte for the coordinates at x and y for the color component c.
static void setYUV420PPixel(IVideoPicture *frame, int x, int y, YUVColorComponent c, unsigned char value)
Sets the value of the color component c at the coordinates x and y in the given frame.
static int getYUV420PPixelOffset(IVideoPicture *frame, int x, int y, YUVColorComponent c)
For a given x and y in a frame, and a given color components, this method tells you how far into the ...
Represents one raw (undecoded) picture in a video stream, plus a timestamp for when to display that v...
Definition: IVideoPicture.h:40
virtual int getWidth()=0
What is the width of the picture.
virtual int getHeight()=0
What is the height of the picture.
virtual IPixelFormat::Type getPixelType()=0
Returns the pixel format of the picture.
This class is only useful from C++.
Definition: RefPointer.h:47
This library contains routines used by AVPKit libraries for "ferry"ing Java objects to and from nativ...
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...