实验二:tga格式图片转yuv格式
生活随笔
收集整理的這篇文章主要介紹了
实验二:tga格式图片转yuv格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、 文件格式
Tga常見的格式有非壓縮RGB和壓縮RGB兩種格式,其他格式的我們在這里不做講述。文件的第三個Byte位作為標記:2為非壓縮RGB格式,10為壓縮RGB格式。在此次實驗中,分析未壓縮的tga圖片。
未壓縮格式:
| 圖像信息字段長度 | 0 | 1 | 本字段為1字節無符號整型,指出圖像信息字段的長度,其取值范圍為0~255,當它為0時,表示沒有圖像的信息字段 |
| 顏色表類型 | 1 | 1 | 0表示沒有顏色表,1表示顏色表存在 |
| 圖像類型碼 | 2 | 1 | 該字段總為2,表示無壓縮圖像 |
| 顏色表首址 | 3 | 2 | 顏色表首的入口索引,整型(低位-高位) |
| 顏色表的長度 | 5 | 2 | 顏色表的表項總數,整型(低位-高位) |
| 顏色表項位數 | 7 | 1 | 位數(bit),16代表16位TGA,24位代表24位TGA,32代表32位TGA |
| 圖像X坐標起始位置 | 8 | 2 | 圖像左下角X坐標的整型值 |
| 圖像Y坐標其實位置 | 10 | 2 | 圖像左下角Y坐標的整型值 |
| 圖像寬度 | 12 | 2 | 以像素為單位,圖像寬度的整型 |
| 圖像高度 | 14 | 2 | 以像素為單位,圖像高度的整型 |
| 圖像每像素存儲占用位數 | 16 | 2 | 它的值為16,24或32等等,決定了該圖像時TGA16,TGA24,TGA32等等 |
| 圖像描述字節 | 17 | 1 | bits 3-0 - 每像素對應的屬性位的位數;對于TGA 16,該值為 0 或 1,對于 TGA24,該值為 0,對于 TGA 32,該值為 8。 bit 4 - 保留,必須為 0。bit 5 - 屏幕起始位置標志,0 = 原點在左下角,1 = 原點在左上角對于 truevision 圖像必須為 0。bits 7-6 - 交叉數據存儲標志:00 = 無交叉;01 = 兩路奇/偶交叉;10 = 四路交叉;11 = 保留 |
| 圖像信息字段 | 18 | 可變 | 包含一個自由格式的,長度是圖像由“圖像信息字段”指定。它常常被忽略(即偏移 0 處值為 0 ),注意其最大可以含有 255 個字符。如果需要存儲更多信息,可以放在圖像數據之后 |
| 顏色表數據 | 可變 | 可變 | 如果顏色類型為0,則該域不存在,否則越過該域直接讀取圖像顏色表規格中描述了每項的字節數,為2,3,4之一 |
| 圖像數據 | 可變 | 可變 | RGB顏色數據,存放順序為BGR,數據從圖像左下角開始存儲 |
參考:文件格式分析
二:代碼部分
rgb2yuv.cpp
#include <stdio.h> #include <iostream> #include <malloc.h> #include"TgaHeader.h"int rgb2yuv(void* rgb_s, void* y_s, void* u_s, void* v_s, int W, int H) {unsigned char *r = NULL, *g = NULL, *b = NULL, *rgb = NULL;unsigned char *y, *u, *v, *U, *V;rgb = (unsigned char *)rgb_s;y = (unsigned char *)y_s;u = (unsigned char *)u_s;v = (unsigned char *)v_s;r = (unsigned char *)malloc(sizeof(char)*(W*H));g = (unsigned char *)malloc(sizeof(char)*(W*H));b = (unsigned char *)malloc(sizeof(char)*(W*H));U = (unsigned char *)malloc(sizeof(char)*(W*H));V = (unsigned char *)malloc(sizeof(char)*(W*H));int j=0;for (int i = 0; i < W*H; i++){b[i] = rgb[j];g[i] = rgb[j+1];r[i] = rgb[j+2];j += 3;}for (int i = 0; i < H*W; i++){y[i] = r[i]*0.2990 + g[i]*0.5870 + b[i]*0.1140;U[i] = r[i]*(-0.1684) - g[i]*0.3316 + b[i]*0.5 + 128;V[i] = r[i]*0.5 - g[i]*0.4187 - b[i]*0.0813 + 128;}int k = 0;for (int i = 0; i < H; i = i + 2){for (j = 0; j < W; j = j + 2){u[k] = (U[W*i + j] + U[W*i + j + 1] + U[(i + 1)*W + j] + U[(i + 1)*W + j + 1]) / 4;v[k] = (V[W*i + j] + V[W*i + j + 1] + V[(i + 1)*W + j] + V[(i + 1)*W + j + 1]) / 4;k++;}}return 0; }main.cpp
#include <stdio.h> #include <iostream> #include <malloc.h> #include"TgaHeader.h"using namespace std;struct TGA_HEADER {unsigned short IDLength;unsigned short ImageType;unsigned short ColorMapLen;unsigned char ColorMapBits;unsigned int Width;unsigned int Height;unsigned char bitsPerPixel; }TGAHEADER;int main(int argc, char **argv) {FILE *TgaFile = NULL;FILE *YuvFile = NULL;char *TgaFileName = NULL;char *YuvFileName = NULL;TgaFileName = argv[1];YuvFileName = argv[2];int Height, Width;int offset = 0;if (fopen_s(&TgaFile, TgaFileName, "rb") == 0)printf("The tgafile was opened: %s \n", TgaFileName);else printf("The tgafile cannot open: %s \n", TgaFileName);fopen_s(&YuvFile, YuvFileName, "wb");unsigned char typeHeader[18] = { 0 };fread(typeHeader, 1, 18, TgaFile);TGAHEADER.IDLength = typeHeader[0];TGAHEADER.ImageType = typeHeader[2];TGAHEADER.ColorMapLen = (typeHeader[6] << 8) + typeHeader[5];TGAHEADER.ColorMapBits = typeHeader[7];TGAHEADER.Width = (typeHeader[13] << 8) + typeHeader[12];TGAHEADER.Height = (typeHeader[15] << 8) + typeHeader[14];TGAHEADER.bitsPerPixel = (typeHeader[17] << 8) + typeHeader[16];unsigned short pixel = TGAHEADER.bitsPerPixel / 8;unsigned short ColorPixel = TGAHEADER.ColorMapBits / 8;Width = TGAHEADER.Width;Height = TGAHEADER.Height;printf("該tga圖片的大小為: %d * %d \n", TGAHEADER.Width, TGAHEADER.Height);int i, j;unsigned char *tgaBuffer = NULL;unsigned char *rgbBuffer = NULL;unsigned char *yBuffer = NULL;unsigned char *uBuffer = NULL;unsigned char *vBuffer = NULL;unsigned char *colorBuffer = NULL;unsigned char *colormapBuffer = NULL;yBuffer = (unsigned char *)malloc(Width*Height);uBuffer = (unsigned char *)malloc(Width*Height / 4);vBuffer = (unsigned char *)malloc(Width*Height / 4);tgaBuffer = (unsigned char *)malloc(Width*Height * 3);rgbBuffer = (unsigned char *)malloc(Width*Height * 3);offset = TGAHEADER.IDLength + 18;fseek(TgaFile, offset, SEEK_SET);switch (pixel){case 3:fread(tgaBuffer, 1, Width*Height*pixel, TgaFile);break;case 4:unsigned char *tgaB = NULL;fread(tgaB, 1, Width*Height*pixel, TgaFile);int a = 0, b = 0;for (i = 0; i < Height; i++){for (j = 0; j < Width; j++){tgaBuffer[a] = tgaB[b];tgaBuffer[a + 1] = tgaB[b + 1];tgaBuffer[a + 2] = tgaB[b + 2];a += 3;b += 4;}}free(tgaB);}for(i=0;i<Height;i++)for (j = 0; j < Width * 3; j++){rgbBuffer[Width * 3 * i + j] = tgaBuffer[Width * 3 * (Height - i - 1) + j];}if((rgb2yuv(rgbBuffer,yBuffer,uBuffer,vBuffer,Width,Height)==0))printf("yuvFile has finished\n");else{printf("error\n");exit(1);}fwrite(yBuffer, 1, Width*Height, YuvFile);fwrite(uBuffer, 1, Width*Height/4, YuvFile);fwrite(vBuffer, 1, Width*Height/4, YuvFile);free(yBuffer);free(uBuffer);free(vBuffer);free(tgaBuffer);free(rgbBuffer);fclose(TgaFile);fclose(YuvFile);return 0; }TgaHeader.h
#pragma once int rgb2yuv(void* rgb_s, void* y_s, void* u_s, void* v_s, int W, int H);三:實驗結果
原圖tga:
轉換結果yuv:
總結
以上是生活随笔為你收集整理的实验二:tga格式图片转yuv格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机毕业设计 基于JavaWeb的图书
- 下一篇: 什么是Verilog HDL?