Skip to content

Commit cff69a8

Browse files
committed
frame: permit JPEG -> JPEG conversion
1 parent 4c374e2 commit cff69a8

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/esp32cam/frame.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "frame.hpp"
22
#include "config.hpp"
3-
43
#include <Arduino.h>
54
#include <esp_camera.h>
5+
#include <memory>
66

77
namespace esp32cam {
88

@@ -66,15 +66,30 @@ Frame::isJpeg() const {
6666

6767
bool
6868
Frame::toJpeg(int quality) {
69-
uint8_t* data;
70-
size_t size;
71-
bool ok = fmt2jpg(m_data, m_size, m_width, m_height, static_cast<pixformat_t>(m_pixFormat),
72-
detail::convertJpegQuality(quality), &data, &size);
73-
if (!ok) {
69+
const uint8_t* input = m_data;
70+
size_t size = m_size;
71+
pixformat_t fmt = static_cast<pixformat_t>(m_pixFormat);
72+
std::unique_ptr<uint8_t[]> rgb;
73+
74+
if (isJpeg()) {
75+
size = m_width * m_height * 3;
76+
rgb.reset(new uint8_t[size]);
77+
if (fmt2rgb888(input, size, fmt, rgb.get())) {
78+
input = rgb.get();
79+
fmt = PIXFORMAT_RGB888;
80+
} else {
81+
return false;
82+
}
83+
}
84+
85+
uint8_t* output;
86+
if (!fmt2jpg(const_cast<uint8_t*>(input), size, m_width, m_height, fmt,
87+
detail::convertJpegQuality(quality), &output, &size)) {
7488
return false;
7589
}
90+
7691
releaseFb();
77-
m_data = data;
92+
m_data = output;
7893
m_size = size;
7994
m_pixFormat = PIXFORMAT_JPEG;
8095
return true;

0 commit comments

Comments
 (0)