Fix memory leak in samples builder
This commit is contained in:
parent
56b4394f3b
commit
7c0088f311
2
Jenkinsfile
vendored
2
Jenkinsfile
vendored
@ -5,7 +5,7 @@ pipeline {
|
||||
}
|
||||
environment {
|
||||
CI_OUTPUT_NAME = "RSETexture"
|
||||
CI_VERSION = "2.0.0"
|
||||
CI_VERSION = "2.0.1"
|
||||
CI_BUILD_NUMBER = "$BUILD_NUMBER"
|
||||
}
|
||||
stages {
|
||||
|
@ -157,12 +157,12 @@ static unsigned short ExtractMaterials(T_RSPTEXTURE_HMT* pHmt, const MEMFILE pMe
|
||||
pHmt->materials[i].type = ((T_HMTFILE_MATERIAL *)(pMemfile +
|
||||
sizeof(T_HMTFILE_HEADER1) + sizeof(T_HMTFILE_MATERIAL) * i))->type;
|
||||
pHmt->materials[i].texture_index = ((T_HMTFILE_MATERIAL *)(pMemfile +
|
||||
sizeof(T_HMTFILE_HEADER1) + sizeof(T_HMTFILE_MATERIAL) * i))->texture_index;
|
||||
sizeof(T_HMTFILE_HEADER1) + sizeof(T_HMTFILE_MATERIAL) * i))->texture_index; // Texture index is only for texture "type" (1)
|
||||
|
||||
if (pParams->verbose_mode) {
|
||||
printf("[INFO] > Material name: %s\n", pHmt->materials[i].name);
|
||||
printf("[INFO] > Material type: %d\n", pHmt->materials[i].type);
|
||||
printf("[INFO] > Material text. index: %d\n", pHmt->materials[i].texture_index);
|
||||
printf("[INFO] > Material text. index: %d (valid if type == 1)\n", pHmt->materials[i].texture_index);
|
||||
}
|
||||
if (pParams->debug_mode) {
|
||||
printf("[DBG] > Material reserved0: %.8f\n", ((T_HMTFILE_MATERIAL *)(pMemfile +
|
||||
@ -202,6 +202,8 @@ static unsigned short ExtractTextures(T_RSPTEXTURE_HMT* pHmt, const MEMFILE pMem
|
||||
|
||||
for ( i = 0; i < pHmt->textures_count; i++ ) {
|
||||
if (pParams->debug_mode) printf("\n-----------------------Begin of texture #%d-------------------------\n", i);
|
||||
_palette = NULL;
|
||||
_samples = NULL;
|
||||
|
||||
// Get texture size infos
|
||||
pHmt->textures[i].width = ((T_HMTFILE_TEXTURE_HEADER *)(pMemfile + pHmt->texture_offset +
|
||||
@ -274,7 +276,7 @@ static unsigned short ExtractTextures(T_RSPTEXTURE_HMT* pHmt, const MEMFILE pMem
|
||||
switch (pHmt->textures[i].palette_entries) {
|
||||
case 16:
|
||||
case 256:
|
||||
_palette = (T_R8G8B8 *)malloc(sizeof(T_R8G8B8) * pHmt->textures[i].palette_entries);
|
||||
_palette = (T_R8G8B8 *)realloc(_palette, sizeof(T_R8G8B8) * pHmt->textures[i].palette_entries);
|
||||
if (_palette)
|
||||
memcpy(_palette, pMemfile + pHmt->textures[i].palette_offset, sizeof(T_R8G8B8) * pHmt->textures[i].palette_entries);
|
||||
else return RSPLIB_ERROR_MEMORY;
|
||||
@ -285,18 +287,21 @@ static unsigned short ExtractTextures(T_RSPTEXTURE_HMT* pHmt, const MEMFILE pMem
|
||||
|
||||
// Retrieve samples from HMT file
|
||||
//TODO: better approach?
|
||||
_samples = (T_SAMPLE *)malloc(pHmt->textures[i].width * pHmt->textures[i].height * pHmt->textures[i].sample_bits / 8);
|
||||
_samples = (T_SAMPLE *)realloc(_samples, pHmt->textures[i].width * pHmt->textures[i].height * pHmt->textures[i].sample_bits / 8);
|
||||
if (_samples) {
|
||||
memcpy(_samples, pMemfile + pHmt->textures[i].pixels_offset, pHmt->textures[i].width * pHmt->textures[i].height * pHmt->textures[i].sample_bits / 8);
|
||||
//if (pHmt->textures[i].type == 2) memcpy(pHmt->textures[i].samples, pMemfile + pHmt->textures[i].pixels_offset, pHmt->textures[i].width * pHmt->textures[i].height * pHmt->textures[i].sample_bits / (8 * 4)); //TODO: manage texture type 2
|
||||
} else return RSPLIB_ERROR_MEMORY;
|
||||
|
||||
// Decode palette and samples to pixels datas
|
||||
if (_palette && _samples) TranslatePixels(&(pHmt->textures[i]), _samples, _palette);
|
||||
TranslatePixels(&(pHmt->textures[i]), _samples, _palette);
|
||||
|
||||
if (pParams->debug_mode) printf("\n-----------------------End of texture #%d---------------------------\n", i);
|
||||
}
|
||||
|
||||
if (_palette) free(_palette);
|
||||
if (_samples) free(_samples);
|
||||
|
||||
return RSPLIB_SUCCESS;
|
||||
}
|
||||
|
||||
@ -312,7 +317,7 @@ static unsigned short ExtractTextures(T_RSPTEXTURE_HMT* pHmt, const MEMFILE pMem
|
||||
static unsigned short TranslatePixels(T_RSPTEXTURE_TEXTURE* pTexture, const T_SAMPLE* pSamples, const T_R8G8B8* pPalette) {
|
||||
unsigned int size;
|
||||
|
||||
if (pTexture == NULL || pSamples == NULL || pPalette == NULL) return RSPLIB_ERROR_ARGS_NULL;
|
||||
if (pTexture == NULL) return RSPLIB_ERROR_ARGS_NULL;
|
||||
if (!(pTexture->type == 0 ||
|
||||
pTexture->type == 1 ||
|
||||
pTexture->type == 2 ||
|
||||
@ -325,10 +330,12 @@ static unsigned short TranslatePixels(T_RSPTEXTURE_TEXTURE* pTexture, const T_SA
|
||||
switch (pTexture->sample_bits) {
|
||||
case 32:
|
||||
pTexture->pixels = (T_R8G8B8A8 *)calloc(size, sizeof(T_R8G8B8A8));
|
||||
if (pTexture->pixels == NULL) printf("[ERR] A texture failed to assert pixels buffer!");
|
||||
memcpy(pTexture->pixels, pSamples, size * sizeof(T_R8G8B8A8));
|
||||
break;
|
||||
case 4:
|
||||
pTexture->pixels = (T_R8G8B8A8 *)calloc(size, sizeof(T_R8G8B8A8));
|
||||
if (pTexture->pixels == NULL) printf("[ERR] A texture failed to assert pixels buffer!");
|
||||
if (pTexture->palette_entries == 0) {
|
||||
convert4bitsGreyTo32bitsRGBA(pTexture->pixels, pSamples, size, pTexture->alpha_color);
|
||||
} else if (pTexture->palette_entries == 16) {
|
||||
@ -338,14 +345,17 @@ static unsigned short TranslatePixels(T_RSPTEXTURE_TEXTURE* pTexture, const T_SA
|
||||
case 8:
|
||||
if (pTexture->palette_entries == 0) {
|
||||
pTexture->pixels = (T_R8G8B8A8 *)calloc(size, 1);
|
||||
if (pTexture->pixels == NULL) printf("[ERR] A texture failed to assert pixels buffer!");
|
||||
memcpy(pTexture->pixels, pSamples, size);
|
||||
} else if (pTexture->palette_entries == 256) {
|
||||
pTexture->pixels = (T_R8G8B8A8 *)calloc(size, sizeof(T_R8G8B8A8));
|
||||
if (pTexture->pixels == NULL) printf("[ERR] A texture failed to assert pixels buffer!");
|
||||
convert8bitsTo32bitsRGBA(pTexture->pixels, pSamples, size, pPalette, pTexture->alpha_color);
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
pTexture->pixels = (T_R8G8B8A8 *)calloc(size, 1);
|
||||
if (pTexture->pixels == NULL) printf("[ERR] A texture failed to assert pixels buffer!");
|
||||
useOddBytes(pTexture->pixels, pSamples, size);
|
||||
break;
|
||||
default:
|
||||
@ -356,11 +366,11 @@ static unsigned short TranslatePixels(T_RSPTEXTURE_TEXTURE* pTexture, const T_SA
|
||||
}
|
||||
|
||||
static unsigned short isTransparentColor(const T_R8G8B8A8 testColor, const T_R8G8B8A8 transp_color) {
|
||||
if (!(testColor.r == transp_color.r)) return RSPLIB_ERROR_PROCESS;
|
||||
if (!(testColor.g == transp_color.g)) return RSPLIB_ERROR_PROCESS;
|
||||
if (!(testColor.b == transp_color.b)) return RSPLIB_ERROR_PROCESS;
|
||||
if (!(testColor.r == transp_color.r)) return 0;
|
||||
if (!(testColor.g == transp_color.g)) return 0;
|
||||
if (!(testColor.b == transp_color.b)) return 0;
|
||||
|
||||
return RSPLIB_SUCCESS;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void convert4bitsGreyTo32bitsRGBA(T_R8G8B8A8* pPixels, const T_SAMPLE* pSamples, const unsigned int sampling, const T_R8G8B8A8 transp_color) {
|
||||
@ -373,11 +383,11 @@ static void convert4bitsGreyTo32bitsRGBA(T_R8G8B8A8* pPixels, const T_SAMPLE* pS
|
||||
pPixels[i * 2].r = div((p >> 4 & 0xF) * 256, 16).quot;
|
||||
pPixels[i * 2].g = div((p >> 4 & 0xF) * 256, 16).quot;
|
||||
pPixels[i * 2].b = div((p >> 4 & 0xF) * 256, 16).quot;
|
||||
pPixels[i * 2].a = isTransparentColor(pPixels[i * 2], transp_color) ? 255 : 255 - transp_color.a;
|
||||
pPixels[i * 2].a = isTransparentColor(pPixels[i * 2], transp_color) ? 255 - transp_color.a : 255;
|
||||
pPixels[i * 2 + 1].r = div((p & 0xF) * 256, 16).quot;
|
||||
pPixels[i * 2 + 1].g = div((p & 0xF) * 256, 16).quot;
|
||||
pPixels[i * 2 + 1].b = div((p & 0xF) * 256, 16).quot;
|
||||
pPixels[i * 2 + 1].a = isTransparentColor(pPixels[i * 2 + 1], transp_color) ? 255 : 255 - transp_color.a;
|
||||
pPixels[i * 2 + 1].a = isTransparentColor(pPixels[i * 2 + 1], transp_color) ? 255 - transp_color.a : 255;
|
||||
}
|
||||
}
|
||||
|
||||
@ -391,11 +401,11 @@ static void convert4bitsTo32bitsRGBA(T_R8G8B8A8* pPixels, const T_SAMPLE* pSampl
|
||||
pPixels[i * 2].r = pal[(p >> 4) & 0xF].r;
|
||||
pPixels[i * 2].g = pal[(p >> 4) & 0xF].g;
|
||||
pPixels[i * 2].b = pal[(p >> 4) & 0xF].b;
|
||||
pPixels[i * 2].a = isTransparentColor(pPixels[i * 2], transp_color) ? 255 : 255 - transp_color.a;
|
||||
pPixels[i * 2].a = isTransparentColor(pPixels[i * 2], transp_color) ? 255 - transp_color.a : 255;
|
||||
pPixels[i * 2 + 1].r = pal[p & 0xF].r;
|
||||
pPixels[i * 2 + 1].g = pal[p & 0xF].g;
|
||||
pPixels[i * 2 + 1].b = pal[p & 0xF].b;
|
||||
pPixels[i * 2 + 1].a = isTransparentColor(pPixels[i * 2 + 1], transp_color) ? 255 : 255 - transp_color.a;
|
||||
pPixels[i * 2 + 1].a = isTransparentColor(pPixels[i * 2 + 1], transp_color) ? 255 - transp_color.a : 255;
|
||||
}
|
||||
}
|
||||
|
||||
@ -408,7 +418,7 @@ static void convert8bitsTo32bitsRGBA(T_R8G8B8A8* pPixels, const T_SAMPLE* pSampl
|
||||
pPixels[i].r = pal[p].r;
|
||||
pPixels[i].g = pal[p].g;
|
||||
pPixels[i].b = pal[p].b;
|
||||
pPixels[i].a = isTransparentColor(pPixels[i], transp_color) ? 255 : 255 - transp_color.a;
|
||||
pPixels[i].a = isTransparentColor(pPixels[i], transp_color) ? 255 - transp_color.a : 255;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user