JPEG 解码(C++)

题目链接计算机软件能力认证考试系统

个人解题思路,进行离散余弦变换时,多创造了一个矩阵tmp_matrix[][]先去处理

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const int Maxrange = 8;
void ScanMatrix(int mt[][Maxrange],int n)        
{
    int num = 0;
    for (int i = 0; i < 8; i++)
    {
        if (i % 2 != 0)    //向下走
        {
            for (int x = i; x >= 0; x--)
            {
                int y = i - x;
                if (num < n)
                {
                    cin >> mt[y][x];
                    num++;
                }
                else return;
            }
        }
        else
        {
            for (int y = i; y >= 0; y--)
            {
                int x = i - y;
                if (num < n)
                {
                    cin >> mt[y][x];
                    num++;
                }
                else return;
            }


        }
    }
    for (int i = 8; i < 15; i++)
    {
        if (i % 2 != 0)    //向下走
        {
            for (int x = 7; x > 0; x--)
            {
                int y = i - x;
                if (y > 7) break;
                if (num < n)
                {
                    cin >> mt[y][x];
                    num++;
                }
                else return;
            }
        }
        else
        {
            for (int y = 7; y >0; y--)
            {
                int x = i - y;
                if (x > 7)    break;
                if (num < n&&x<=7)
                {
                    cin >> mt[y][x];
                    num++;
                }
                else return;
            }


        }
    }
}
void OutputMatrix(int mt[][Maxrange] )
{
    for (int i = 0; i < Maxrange; i++)
    {
        for (int j = 0; j < Maxrange; j++)
        {
            cout << mt[i][j] << ' ';
        }
        cout << endl;
    }
}
void MultiplyMatrix(int mt[][Maxrange], int qu[][Maxrange])
{
    for (int i = 0; i < Maxrange; i++)
    {
        for (int j = 0; j < Maxrange; j++)
        {
            mt[i][j] = mt[i][j] * qu[i][j];
        }
    }
}
void PreOperataionMatrix(int mt[][Maxrange],double tmp[][Maxrange])    
{
    for (int i = 0; i < Maxrange; i++)
    {    
        for (int j = 0; j < Maxrange; j++)
        {
            tmp[i][j] = 1;
            if (i == 0) tmp[i][j] *= sqrt(0.5);
            if (j == 0) tmp[i][j] *= sqrt(0.5);
            tmp[i][j] = tmp[i][j]* 0.25*mt[i][j];
        }
    }
}
void AccumulateMatrix(double tmp[][Maxrange], int mt[][Maxrange])
{
    double temp;
    int temp_2;
    for (int i = 0; i < Maxrange; i++)
    {
        for (int j = 0; j < Maxrange; j++)
        {
            temp = 0;
            for (int u = 0; u < Maxrange; u++)
            {
                for (int v = 0; v < Maxrange; v++)
                {
                    temp += tmp[u][v] * cos((acos(-1) / 8) * (i + 0.5) * u) * cos((acos(-1) / 8) * (j + 0.5) * v);
                }
            }
            temp += 128;
            temp_2 = round(temp);
            if (temp_2 > 255)
                mt[i][j] = 255;
            else if (temp_2 < 0)
                mt[i][j] = 0;
            else
                mt[i][j] = temp_2;
        }
    }
}
int main()
{
    int quantization_matrix[Maxrange][Maxrange];
    int data_matrix[Maxrange][Maxrange] = { 0 };
    double tmp_matrix[Maxrange][Maxrange] ;
    for (int i = 0; i < Maxrange; i++)
        for (int j = 0; j < Maxrange; j++)
            cin >> quantization_matrix[i][j];
    int n;    cin >> n;
    int T;    cin >> T;
    if (T == 0)
    {
        ScanMatrix(data_matrix, n);
        OutputMatrix(data_matrix);
    }
    else if(T==1) 
    {
        ScanMatrix(data_matrix, n);
        MultiplyMatrix(data_matrix, quantization_matrix);
        OutputMatrix(data_matrix);
    }
    else if(T==2)
    {
        ScanMatrix(data_matrix, n);
        MultiplyMatrix(data_matrix, quantization_matrix);
        PreOperataionMatrix(data_matrix, tmp_matrix);
        AccumulateMatrix(tmp_matrix, data_matrix);
        OutputMatrix(data_matrix);

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ffmpeg是一个开源的音视频处理工具,可以用于解码和编码各种音视频格式。在C++中使用ffmpeg解码jpeg文件可以按照以下步骤进行: 1. 引入ffmpeg库和头文件: ```cpp extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> } ``` 2. 初始化ffmpeg: ```cpp av_register_all(); ``` 3. 打开输入文件: ```cpp AVFormatContext* formatContext = avformat_alloc_context(); if (avformat_open_input(&formatContext, "input.jpg", NULL, NULL) != 0) { // 打开文件失败的处理 } if (avformat_find_stream_info(formatContext, NULL) < 0) { // 获取流信息失败的处理 } ``` 4. 查找视频流: ```cpp int videoStreamIndex = -1; for (int i = 0; i < formatContext->nb_streams; i++) { if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { // 没有找到视频流的处理 } ``` 5. 获取解码器并打开解码器: ```cpp AVCodecParameters* codecParameters = formatContext->streams[videoStreamIndex]->codecpar; AVCodec* codec = avcodec_find_decoder(codecParameters->codec_id); AVCodecContext* codecContext = avcodec_alloc_context3(codec); if (avcodec_parameters_to_context(codecContext, codecParameters) < 0) { // 获取解码器上下文失败的处理 } if (avcodec_open2(codecContext, codec, NULL) < 0) { // 打开解码器失败的处理 } ``` 6. 创建帧对象和缓冲区: ```cpp AVFrame* frame = av_frame_alloc(); AVPacket* packet = av_packet_alloc(); uint8_t* buffer = (uint8_t*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_RGB24, codecContext->width, codecContext->height, 1)); av_image_fill_arrays(frame->data, frame->linesize, buffer, AV_PIX_FMT_RGB24, codecContext->width, codecContext->height, 1); ``` 7. 解码并保存为图片: ```cpp while (av_read_frame(formatContext, packet) >= 0) { if (packet->stream_index == videoStreamIndex) { avcodec_send_packet(codecContext, packet); avcodec_receive_frame(codecContext, frame); // 将frame保存为图片 } av_packet_unref(packet); } ``` 8. 释放资源: ```cpp av_frame_free(&frame); av_packet_free(&packet); avcodec_free_context(&codecContext); avformat_close_input(&formatContext); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值