cpp 类的定义

例如定义一个JpegEncoder类

.h 即为头文件,定义类的相关内容 通过加入 #pragma once 保证该文件只被编译一次

#pragma once

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <turbojpeg.h>
#include <vector>
#include <cstring>
using namespace std;

class JpegEncoder{
public:
    JpegEncoder();
    ~JpegEncoder(){ printf("[JPEG] encoder deleted\n");};

    int encode(vector<uint8_t> rgb_list, vector<uint8_t> &jpeg, int width, int height);
    void testDecoder(vector<uint8_t> compressed_bytes, vector<uint8_t>& decoded_bytes);
private:
    tjhandle handle_;
    int jpegQual_;
    int nbands_;
    int flags_;
    int pixelFormat_;
    int jpegSubsamp_;

};

.cpp 为实现文件,需要#include”xxx.h” 其实就是复制进来.h的代码

#include "JpegEncoder.hpp"
#include <fstream>
#include <cstdio>
JpegEncoder::JpegEncoder()
{
    handle_ = tjInitCompress();

    jpegQual_ =96;
    nbands_ = 3;
    flags_ = 0;
    pixelFormat_ = TJPF_RGB;
    jpegSubsamp_ = TJSAMP_420;
}

int JpegEncoder::encode(vector<uint8_t> rgb_list, vector<uint8_t> &jpeg, int width, int height)
{

    printf("[JPEG] start encode\n");
    uint8_t* srcBuf = new uint8_t[width * height * nbands_];
    memset(srcBuf, 0, width * height * nbands_);
    memcpy(srcBuf, &rgb_list[0], rgb_list.size());

    uint8_t* jpegBuf =NULL;
    unsigned long jpegSize;
    int tj_stat = tjCompress2(handle_, srcBuf, width, width * nbands_, height, pixelFormat_, 
            &(jpegBuf), &jpegSize, jpegSubsamp_, jpegQual_, flags_);

    jpeg.resize(jpegSize, 0);
    memcpy(&jpeg[0], jpegBuf, jpegSize);

    FILE* qFile= fopen("test_image.jpg", "wb");
    fwrite(jpegBuf, sizeof(uint8_t), jpegSize, qFile);
    if(tj_stat != 0)
    {   
        const char *err = (const char *) tjGetErrorStr();
        cerr << "TurboJPEG Error: " << err << " UNABLE TO COMPRESS JPEG IMAGE\n";
        tjDestroy(handle_);
        handle_ = NULL;
        return -1;
    }

    fclose(qFile);

    delete jpegBuf;
    delete []srcBuf;
    return 1;

}

void JpegEncoder::testDecoder(vector<uint8_t> compressed_bytes, vector<uint8_t>& decoded_bytes)
{
    tjhandle handle = tjInitDecompress();
    if(handle < 0) printf("Can't initialize decoder\n");
    int width = 0;
    int height = 0;
    int jpegSubsamp = 0;
    int jpegColorSpace = 0;

    int numCompressedBytes = compressed_bytes.size();

    tjDecompressHeader3(handle, &compressed_bytes[0], numCompressedBytes, &width, &height, &jpegSubsamp,
            &jpegColorSpace);

    unsigned char *imgBuf = (unsigned char *) malloc(width * height * 3);
    printf("Jpeg decoder header (filesize , width, height, jpegSubsamp, jpegColorSpace) %lu %d %d %d %d\n", compressed_bytes.size(), width, height, jpegSubsamp, jpegColorSpace); 
    if( tjDecompress2(handle, &compressed_bytes[0], numCompressedBytes, imgBuf ,width,width * 3, height, jpegColorSpace,  0) < 0)
    {
        printf("decompress failed\n");
    }

    for(int i = 0 ; i  < 30 ; i++)
    {
        printf("%d\n", *(imgBuf+i));
    }
    decoded_bytes.resize(width  *height * 3);
    memcpy(&decoded_bytes[0], imgBuf, width * height * 3);
	}

因此编译生成可执行文件的时候,引入.cpp就ok,如

file(GLOB SOURCES main.cpp JpegEncoder.cpp Frame.cpp)

add_executable(server ${SOURCES})

而在代码中若要使用该类,需要引入xxx.h 如

#include "JpegEncoder.hpp”
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值