minor update

This commit is contained in:
Blaise Tine 2021-08-05 11:49:05 -07:00
parent 43ad188ccb
commit e7aa93614b
14 changed files with 1728 additions and 1574 deletions

View file

@ -1,9 +1,9 @@
`include "VX_tex_define.vh"
module VX_tex_addr #(
parameter CORE_ID = 0,
parameter REQ_INFO_WIDTH = 1,
parameter NUM_REQS = 1
parameter CORE_ID = 0,
parameter REQ_INFOW = 1,
parameter NUM_REQS = 1
) (
input wire clk,
input wire reset,
@ -19,7 +19,7 @@ module VX_tex_addr #(
input wire [`TEX_ADDR_BITS-1:0] req_baseaddr,
input wire [NUM_REQS-1:0][`TEX_MIPOFF_BITS-1:0] req_mipoff,
input wire [NUM_REQS-1:0][1:0][`TEX_DIM_BITS-1:0] req_logdims,
input wire [REQ_INFO_WIDTH-1:0] req_info,
input wire [REQ_INFOW-1:0] req_info,
output wire req_ready,
// outputs
@ -30,7 +30,7 @@ module VX_tex_addr #(
output wire [`TEX_STRIDE_BITS-1:0] rsp_stride,
output wire [NUM_REQS-1:0][3:0][31:0] rsp_addr,
output wire [NUM_REQS-1:0][1:0][`BLEND_FRAC-1:0] rsp_blends,
output wire [REQ_INFO_WIDTH-1:0] rsp_info,
output wire [REQ_INFOW-1:0] rsp_info,
input wire rsp_ready
);
@ -38,10 +38,10 @@ module VX_tex_addr #(
localparam PITCH_BITS = `ADDER_CARRY_WIDTH(`TEX_DIM_BITS, `TEX_STRIDE_BITS);
wire valid_s0;
wire [NUM_REQS-1:0] tmask_s0;
wire valid_s0;
wire [NUM_REQS-1:0] tmask_s0;
wire [`TEX_FILTER_BITS-1:0] filter_s0;
wire [REQ_INFO_WIDTH-1:0] req_info_s0;
wire [REQ_INFOW-1:0] req_info_s0;
wire [NUM_REQS-1:0][1:0][`FIXED_FRAC-1:0] clamped_lo, clamped_lo_s0;
wire [NUM_REQS-1:0][1:0][`FIXED_FRAC-1:0] clamped_hi, clamped_hi_s0;
wire [`TEX_STRIDE_BITS-1:0] log_stride, log_stride_s0;
@ -90,7 +90,7 @@ module VX_tex_addr #(
end
VX_pipe_register #(
.DATAW (1 + NUM_REQS + `TEX_FILTER_BITS + `TEX_STRIDE_BITS + REQ_INFO_WIDTH + NUM_REQS * (PITCH_BITS + `TEX_DIM_BITS + 32 + 2 * 2 * `FIXED_FRAC)),
.DATAW (1 + NUM_REQS + `TEX_FILTER_BITS + `TEX_STRIDE_BITS + REQ_INFOW + NUM_REQS * (PITCH_BITS + `TEX_DIM_BITS + 32 + 2 * 2 * `FIXED_FRAC)),
.RESETW (1)
) pipe_reg0 (
.clk (clk),
@ -127,7 +127,7 @@ module VX_tex_addr #(
assign stall_out = rsp_valid && ~rsp_ready;
VX_pipe_register #(
.DATAW (1 + NUM_REQS + `TEX_FILTER_BITS + `TEX_STRIDE_BITS + (NUM_REQS * 4 * 32) + (2 * NUM_REQS * `BLEND_FRAC) + REQ_INFO_WIDTH),
.DATAW (1 + NUM_REQS + `TEX_FILTER_BITS + `TEX_STRIDE_BITS + (NUM_REQS * 4 * 32) + (2 * NUM_REQS * `BLEND_FRAC) + REQ_INFOW),
.RESETW (1)
) pipe_reg1 (
.clk (clk),
@ -157,18 +157,18 @@ module VX_tex_addr #(
function logic [(`FIXED_INT+`TEX_STRIDE_BITS)-1:0] scale_to_pitch (input logic [`FIXED_FRAC-1:0] src,
input logic [PITCH_BITS-1:0] dim);
`IGNORE_UNUSED_BEGIN
`IGNORE_WARNINGS_BEGIN
logic [(`FIXED_BITS+`TEX_STRIDE_BITS)-1:0] out;
`IGNORE_UNUSED_END
`IGNORE_WARNINGS_END
out = (`FIXED_BITS+`TEX_STRIDE_BITS)'(src) << dim;
return out[`FIXED_FRAC +: (`FIXED_INT+`TEX_STRIDE_BITS)];
endfunction
function logic [`FIXED_INT-1:0] scale_to_height (input logic [`FIXED_FRAC-1:0] src,
input logic [`TEX_DIM_BITS-1:0] dim);
`IGNORE_UNUSED_BEGIN
`IGNORE_WARNINGS_BEGIN
logic [`FIXED_BITS-1:0] out;
`IGNORE_UNUSED_END
`IGNORE_WARNINGS_END
out = `FIXED_BITS'(src) << dim;
return out[`FIXED_FRAC +: `FIXED_INT];
endfunction

View file

@ -1,8 +1,8 @@
`include "VX_tex_define.vh"
module VX_tex_memory #(
parameter CORE_ID = 0,
parameter REQ_INFO_WIDTH = 1,
parameter NUM_REQS = 1
parameter CORE_ID = 0,
parameter REQ_INFOW = 1,
parameter NUM_REQS = 1
) (
input wire clk,
input wire reset,
@ -17,14 +17,14 @@ module VX_tex_memory #(
input wire [`TEX_FILTER_BITS-1:0] req_filter,
input wire [`TEX_STRIDE_BITS-1:0] req_stride,
input wire [NUM_REQS-1:0][3:0][31:0] req_addr,
input wire [REQ_INFO_WIDTH-1:0] req_info,
input wire [REQ_INFOW-1:0] req_info,
output wire req_ready,
// outputs
output wire rsp_valid,
output wire [NUM_REQS-1:0] rsp_tmask,
output wire [NUM_REQS-1:0][3:0][31:0] rsp_data,
output wire [REQ_INFO_WIDTH-1:0] rsp_info,
output wire [REQ_INFOW-1:0] rsp_info,
input wire rsp_ready
);
@ -59,18 +59,18 @@ module VX_tex_memory #(
wire reqq_push, reqq_pop, reqq_empty, reqq_full;
wire [3:0][NUM_REQS-1:0][29:0] q_req_addr;
wire [NUM_REQS-1:0] q_req_tmask;
wire [`TEX_FILTER_BITS-1:0] q_req_filter;
wire [REQ_INFO_WIDTH-1:0] q_req_info;
wire [`TEX_STRIDE_BITS-1:0] q_req_stride;
wire [3:0][NUM_REQS-1:0][1:0] q_align_offs;
wire [3:0] q_dup_reqs;
wire [3:0][NUM_REQS-1:0][29:0] q_req_addr;
wire [NUM_REQS-1:0] q_req_tmask;
wire [`TEX_FILTER_BITS-1:0] q_req_filter;
wire [REQ_INFOW-1:0] q_req_info;
wire [`TEX_STRIDE_BITS-1:0] q_req_stride;
wire [3:0][NUM_REQS-1:0][1:0] q_align_offs;
wire [3:0] q_dup_reqs;
assign reqq_push = req_valid && req_ready;
VX_fifo_queue #(
.DATAW ((NUM_REQS * 4 * 30) + NUM_REQS + REQ_INFO_WIDTH + `TEX_FILTER_BITS + `TEX_STRIDE_BITS + (4 * NUM_REQS * 2) + 4),
.DATAW ((NUM_REQS * 4 * 30) + NUM_REQS + REQ_INFOW + `TEX_FILTER_BITS + `TEX_STRIDE_BITS + (4 * NUM_REQS * 2) + 4),
.SIZE (`LSUQ_SIZE),
.OUTPUT_REG (1)
) req_queue (
@ -244,7 +244,7 @@ module VX_tex_memory #(
assign reqq_pop = rsp_texels_done && ~stall_out;
VX_pipe_register #(
.DATAW (1 + NUM_REQS + REQ_INFO_WIDTH + (4 * NUM_REQS * 32)),
.DATAW (1 + NUM_REQS + REQ_INFOW + (4 * NUM_REQS * 32)),
.RESETW (1)
) rsp_pipe_reg (
.clk (clk),

View file

@ -1,9 +1,9 @@
`include "VX_tex_define.vh"
module VX_tex_sampler #(
parameter CORE_ID = 0,
parameter REQ_INFO_WIDTH = 1,
parameter NUM_REQS = 1
parameter CORE_ID = 0,
parameter REQ_INFOW = 1,
parameter NUM_REQS = 1
) (
input wire clk,
input wire reset,
@ -14,14 +14,14 @@ module VX_tex_sampler #(
input wire [`TEX_FORMAT_BITS-1:0] req_format,
input wire [NUM_REQS-1:0][1:0][`BLEND_FRAC-1:0] req_blends,
input wire [NUM_REQS-1:0][3:0][31:0] req_data,
input wire [REQ_INFO_WIDTH-1:0] req_info,
input wire [REQ_INFOW-1:0] req_info,
output wire req_ready,
// ouputs
output wire rsp_valid,
output wire [NUM_REQS-1:0] rsp_tmask,
output wire [NUM_REQS-1:0][31:0] rsp_data,
output wire [REQ_INFO_WIDTH-1:0] rsp_info,
output wire [REQ_INFOW-1:0] rsp_info,
input wire rsp_ready
);
@ -29,7 +29,7 @@ module VX_tex_sampler #(
wire valid_s0;
wire [NUM_REQS-1:0] tmask_s0;
wire [REQ_INFO_WIDTH-1:0] req_info_s0;
wire [REQ_INFOW-1:0] req_info_s0;
wire [NUM_REQS-1:0][31:0] texel_ul, texel_uh;
wire [NUM_REQS-1:0][31:0] texel_ul_s0, texel_uh_s0;
wire [NUM_REQS-1:0][`BLEND_FRAC-1:0] blend_v, blend_v_s0;
@ -76,7 +76,7 @@ module VX_tex_sampler #(
end
VX_pipe_register #(
.DATAW (1 + NUM_REQS + REQ_INFO_WIDTH + (NUM_REQS * `BLEND_FRAC) + (2 * NUM_REQS * 32)),
.DATAW (1 + NUM_REQS + REQ_INFOW + (NUM_REQS * `BLEND_FRAC) + (2 * NUM_REQS * 32)),
.RESETW (1)
) pipe_reg0 (
.clk (clk),
@ -103,7 +103,7 @@ module VX_tex_sampler #(
assign stall_out = rsp_valid && ~rsp_ready;
VX_pipe_register #(
.DATAW (1 + NUM_REQS + REQ_INFO_WIDTH + (NUM_REQS * 32)),
.DATAW (1 + NUM_REQS + REQ_INFOW + (NUM_REQS * 32)),
.RESETW (1)
) pipe_reg1 (
.clk (clk),

View file

@ -18,9 +18,9 @@ module VX_tex_unit #(
VX_tex_rsp_if tex_rsp_if
);
localparam REQ_INFO_WIDTH_S = `NR_BITS + 1 + `NW_BITS + 32;
localparam REQ_INFO_WIDTH_A = `TEX_FORMAT_BITS + REQ_INFO_WIDTH_S;
localparam REQ_INFO_WIDTH_M = (2 * `NUM_THREADS * `BLEND_FRAC) + REQ_INFO_WIDTH_A;
localparam REQ_INFOW_S = `NR_BITS + 1 + `NW_BITS + 32;
localparam REQ_INFOW_A = `TEX_FORMAT_BITS + REQ_INFOW_S;
localparam REQ_INFOW_M = (2 * `NUM_THREADS * `BLEND_FRAC) + REQ_INFOW_A;
reg [`TEX_MIPOFF_BITS-1:0] tex_mipoff [`NUM_TEX_UNITS-1:0][(1 << `TEX_LOD_BITS)-1:0];
reg [1:0][`TEX_DIM_BITS-1:0] tex_dims [`NUM_TEX_UNITS-1:0][(1 << `TEX_LOD_BITS)-1:0];
@ -96,13 +96,13 @@ module VX_tex_unit #(
wire [`TEX_STRIDE_BITS-1:0] mem_req_stride;
wire [`NUM_THREADS-1:0][1:0][`BLEND_FRAC-1:0] mem_req_blends;
wire [`NUM_THREADS-1:0][3:0][31:0] mem_req_addr;
wire [REQ_INFO_WIDTH_A-1:0] mem_req_info;
wire [REQ_INFOW_A-1:0] mem_req_info;
wire mem_req_ready;
VX_tex_addr #(
.CORE_ID (CORE_ID),
.REQ_INFO_WIDTH (REQ_INFO_WIDTH_A),
.NUM_REQS (`NUM_THREADS)
.CORE_ID (CORE_ID),
.REQ_INFOW (REQ_INFOW_A),
.NUM_REQS (`NUM_THREADS)
) tex_addr (
.clk (clk),
.reset (reset),
@ -134,13 +134,13 @@ module VX_tex_unit #(
wire mem_rsp_valid;
wire [`NUM_THREADS-1:0] mem_rsp_tmask;
wire [`NUM_THREADS-1:0][3:0][31:0] mem_rsp_data;
wire [REQ_INFO_WIDTH_M-1:0] mem_rsp_info;
wire [REQ_INFOW_M-1:0] mem_rsp_info;
wire mem_rsp_ready;
VX_tex_memory #(
.CORE_ID (CORE_ID),
.REQ_INFO_WIDTH (REQ_INFO_WIDTH_M),
.NUM_REQS (`NUM_THREADS)
.CORE_ID (CORE_ID),
.REQ_INFOW (REQ_INFOW_M),
.NUM_REQS (`NUM_THREADS)
) tex_memory (
.clk (clk),
.reset (reset),
@ -170,14 +170,14 @@ module VX_tex_unit #(
wire [`NUM_THREADS-1:0][1:0][`BLEND_FRAC-1:0] rsp_blends;
wire [`TEX_FORMAT_BITS-1:0] rsp_format;
wire [REQ_INFO_WIDTH_S-1:0] rsp_info;
wire [REQ_INFOW_S-1:0] rsp_info;
assign {rsp_blends, rsp_format, rsp_info} = mem_rsp_info;
VX_tex_sampler #(
.CORE_ID (CORE_ID),
.REQ_INFO_WIDTH (REQ_INFO_WIDTH_S),
.NUM_REQS (`NUM_THREADS)
.CORE_ID (CORE_ID),
.REQ_INFOW (REQ_INFOW_S),
.NUM_REQS (`NUM_THREADS)
) tex_sampler (
.clk (clk),
.reset (reset),

View file

@ -2,7 +2,7 @@ RISCV_TOOLCHAIN_PATH ?= /opt/riscv-gnu-toolchain
VORTEX_DRV_PATH ?= $(realpath ../../../driver)
VORTEX_RT_PATH ?= $(wildcard ../../../runtime)
OPTS ?= -f1
OPTS ?= -g1
VX_CC = $(RISCV_TOOLCHAIN_PATH)/bin/riscv32-unknown-elf-gcc
VX_CXX = $(RISCV_TOOLCHAIN_PATH)/bin/riscv32-unknown-elf-g++

View file

@ -1,12 +1,20 @@
#include "format.h"
//
// Copyright (c) Blaise Tine. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools
// installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR
// INDEMNITIES.
//
#pragma once
struct SurfaceDesc {
ePixelFormat Format;
uint8_t *pBits;
uint32_t Width;
uint32_t Height;
uint32_t Pitch;
};
#include "surfacedesc.h"
class BlitTable {
public:

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

1313
tests/regression/tex/lupng.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,186 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Jan Solanti
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifdef __cplusplus
extern "C" {
#endif
#pragma once
#if defined(_MSC_VER) && (_MSC_VER < 1600)
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
#else
#include <stdlib.h>
#include <stdint.h>
#endif
typedef struct {
int32_t width;
int32_t height;
uint8_t channels;
uint8_t depth; /* must be 8 or 16 */
size_t dataSize;
uint8_t *data;
} LuImage;
typedef size_t (*PngReadProc)(void *outPtr, size_t size, size_t count, void *userPtr);
typedef size_t (*PngWriteProc)(const void *inPtr, size_t size, size_t count, void *userPtr);
typedef void* (*PngAllocProc)(size_t size, void *userPtr);
typedef void (*PngFreeProc)(void *ptr, void *userPtr);
typedef void (*PngWarnProc)(void *userPtr, const char *fmt, ...);
typedef struct {
/* loader */
PngReadProc readProc;
void *readProcUserPtr;
int skipSig;
/* writer */
PngWriteProc writeProc;
void *writeProcUserPtr;
int compressionLevel;
/* memory allocation */
PngAllocProc allocProc;
void *allocProcUserPtr;
PngFreeProc freeProc;
void *freeProcUserPtr;
/* warnings/error output */
PngWarnProc warnProc; /* set to NULL to disable output altogether */
void *warnProcUserPtr;
/* special case: avoid allocating a LuImage when loading or creating
* an image, just use this one */
LuImage *overrideImage;
} LuUserContext;
/**
* Initializes a LuUserContext to use the defaul malloc implementation.
*
* @param userCtx the LuUserContext to initialize
*/
void luUserContextInitDefault(LuUserContext *userCtx);
/**
* Creates a new Image object with the specified attributes.
* The data store of the Image is allocated but its contents are undefined.
* Only 8 and 16 bits deep images with 1-4 channels are supported.
*
* @param buffer pointer to an existing buffer (which may already contain the
* image data), or NULL to internally allocate a new buffer
* @param userCtx the user context (with the memory allocator function
* pointers to use), or NULL to use the default allocator
* (malloc).
*/
LuImage *luImageCreate(size_t width, size_t height, uint8_t channels, uint8_t depth,
uint8_t *buffer, const LuUserContext *usrCtx);
/**
* Releases the memory associated with the given Image object.
*
* @param userCtx the user context (with the memory deallocator function
* pointers to use), or NULL to use the default deallocator
* (free). The deallocator should match the ones used for
* allocation.
*/
void luImageRelease(LuImage *img, const LuUserContext *usrCtx);
/**
* Extracts the raw image buffer form a LuImage and releases the
* then-orphaned LuImage object. This can be used if you want to use
* the image data in your own structures.
*
* @param userCtx the user context (with the memory deallocator function
* pointers to use), or NULL to use the default deallocator
* (free). The deallocator should match the ones used for
* allocation.
*/
uint8_t *luImageExtractBufAndRelease(LuImage *img, const LuUserContext *userCtx);
/**
* Decodes a PNG image from a file
*
* @param filename the file name (optionally with full path) to read from.
* @param userCtx the user context (with the memory allocator function
* pointers to use), or NULL to use the default allocator
* (malloc).
*/
LuImage *luPngReadFile(const char *filename, LuUserContext *userCtx);
/**
* Decodes a PNG image with the provided read function into a LuImage struct
*
* @param readProc a function pointer to a user-defined function to use for
* reading the PNG data.
* @param userPtr an opaque pointer provided as an argument to readProc
* @param skipSig don't verify PNG signature - the bytes have already been
* removed from the input stream
*/
LuImage *luPngRead(PngReadProc readProc, void *userPtr, int skipSig);
/**
* Decodes a PNG image with the provided user context into a LuImage struct
*
* @param userCtx the LuUserContext to use
*/
LuImage *luPngReadUC(const LuUserContext *userCtx);
/**
* Encodes a LuImage struct to PNG and writes it out to a file.
*
* @param filename the file name (optionally with full path) to write to.
* Existing files will be overwritten!
* @param img the LuImage to encode
*/
int luPngWriteFile(const char *filename, const LuImage *img);
/**
* Encodes a LuImage struct to PNG and writes it out using a user-defined write
* function.
*
* @param writeProc a function pointer to a user-defined function that will be
* used for writing the final PNG data.
* @param userPtr an opaque pointer provided as an argument to writeProc
* @param img the LuImage to encode
*/
int luPngWrite(PngWriteProc writeProc, void *userPtr, const LuImage *img);
/**
* Encodes a LuImage struct to PNG and writes it out with the provided user
* context.
*
* @param userCtx the LuUserContext to use
* @param img the LuImage to encode
*/
int luPngWriteUC(const LuUserContext *userCtx, const LuImage *img);
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,25 @@
//
// Copyright (c) Blaise Tine. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools
// installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR
// INDEMNITIES.
//
#pragma once
#include "format.h"
struct SurfaceDesc {
ePixelFormat Format;
uint8_t *pBits;
uint32_t Width;
uint32_t Height;
uint32_t Pitch;
};

View file

@ -0,0 +1,122 @@
#include "tga.h"
#include <fstream>
#include <iostream>
#include "format.h"
struct __attribute__((__packed__)) tga_header_t {
int8_t idlength;
int8_t colormaptype;
int8_t imagetype;
int16_t colormaporigin;
int16_t colormaplength;
int8_t colormapdepth;
int16_t xoffset;
int16_t yoffset;
int16_t width;
int16_t height;
int8_t bitsperpixel;
int8_t imagedescriptor;
};
int LoadTGA(const char *filename,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height,
uint32_t *bpp) {
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
std::cerr << "couldn't open file: " << filename << "!" << std::endl;
return -1;
}
tga_header_t header;
ifs.read(reinterpret_cast<char *>(&header), sizeof(tga_header_t));
if (ifs.fail()) {
std::cerr << "invalid TGA file header!" << std::endl;
return -1;
}
if (header.imagetype != 2) {
std::cerr << "unsupported TGA encoding format!" << std::endl;
return -1;
}
ifs.seekg(header.idlength, std::ios::cur); // skip string
if (ifs.fail()) {
std::cerr << "invalid TGA file!" << std::endl;
return -1;
}
switch (header.bitsperpixel) {
case 16:
case 24:
case 32: {
// Read pixels data
auto stride = header.bitsperpixel / 8;
pixels.resize(stride * header.width * header.height);
ifs.read((char*)pixels.data(), pixels.size());
if (ifs.fail()) {
std::cerr << "invalid TGA file!" << std::endl;
return -1;
}
*bpp = stride;
break;
}
default:
std::cerr << "unsupported TGA bitsperpixel!" << std::endl;
return -1;
}
*width = header.width;
*height = header.height;
return 0;
}
int SaveTGA(const char *filename,
const std::vector<uint8_t> &pixels,
uint32_t width,
uint32_t height,
uint32_t bpp) {
std::ofstream ofs(filename, std::ios::out | std::ios::binary);
if (!ofs.is_open()) {
std::cerr << "couldn't create file: " << filename << "!" << std::endl;
return -1;
}
if (bpp < 2 || bpp > 4) {
std::cerr << "unsupported pixel stride: " << bpp << "!" << std::endl;
return -1;
}
tga_header_t header;
header.idlength = 0;
header.colormaptype = 0; // no palette
header.imagetype = 2; // color mapped data
header.colormaporigin = 0;
header.colormaplength = 0;
header.colormapdepth = 0;
header.xoffset = 0;
header.yoffset = 0;
header.width = width;
header.height = height;
header.bitsperpixel = bpp * 8;
header.imagedescriptor = 0;
// write header
ofs.write(reinterpret_cast<char *>(&header), sizeof(tga_header_t));
// write pixel data
uint32_t pitch = bpp * width;
const uint8_t* pixel_bytes = pixels.data() + (height - 1) * pitch;
for (uint32_t y = 0; y < height; ++y) {
const uint8_t* pixel_row = pixel_bytes;
for (uint32_t x = 0; x < width; ++x) {
ofs.write((const char*)pixel_row, bpp);
pixel_row += bpp;
}
pixel_bytes -= pitch;
}
return 0;
}

View file

@ -0,0 +1,14 @@
#include <cstdint>
#include <vector>
int LoadTGA(const char *filename,
std::vector<uint8_t> &pixels,
uint32_t *width,
uint32_t *height,
uint32_t *bpp);
int SaveTGA(const char *filename,
const std::vector<uint8_t> &pixels,
uint32_t width,
uint32_t height,
uint32_t bpp);