Load_GPL/Save_GPL: clean code, remove warnings
This commit is contained in:
parent
ee992c6b5f
commit
b47ace7a30
@ -104,6 +104,7 @@ void Test_PAL(T_IO_Context * context, FILE * file)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test for GPL (Gimp Palette) file format
|
||||||
void Test_GPL(T_IO_Context * context, FILE * file)
|
void Test_GPL(T_IO_Context * context, FILE * file)
|
||||||
{
|
{
|
||||||
char buffer[16];
|
char buffer[16];
|
||||||
@ -123,8 +124,7 @@ void Test_GPL(T_IO_Context * context, FILE * file)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip the padding before a space-padded field.
|
/// skip the padding before a space-padded field.
|
||||||
|
|
||||||
static int skip_padding(FILE *file, int max_chars)
|
static int skip_padding(FILE *file, int max_chars)
|
||||||
{
|
{
|
||||||
byte b;
|
byte b;
|
||||||
@ -142,91 +142,100 @@ static int skip_padding(FILE *file, int max_chars)
|
|||||||
return chars_read;
|
return chars_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Load file with format GPL -----------------------------------------
|
/// Load GPL (Gimp Palette) file format
|
||||||
void Load_GPL(T_IO_Context * context)
|
void Load_GPL(T_IO_Context * context)
|
||||||
{
|
{
|
||||||
FILE *file;
|
FILE *file;
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
|
|
||||||
File_error=0;
|
File_error = 1;
|
||||||
|
|
||||||
// Open file
|
file = Open_file_read(context);
|
||||||
if ((file=Open_file_read(context)))
|
if (file == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (memcmp(buffer,"GIMP Palette",12) == 0)
|
||||||
{
|
{
|
||||||
|
int i, r, g, b, columns;
|
||||||
|
size_t len;
|
||||||
|
// Name: xxxxx
|
||||||
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
||||||
{
|
|
||||||
File_error = 1;
|
|
||||||
return;
|
return;
|
||||||
}
|
len = strlen(buffer);
|
||||||
|
while (len > 0)
|
||||||
if (memcmp(buffer,"GIMP Palette",12) == 0)
|
|
||||||
{
|
{
|
||||||
int i, r, g, b, columns;
|
len--;
|
||||||
size_t len;
|
if (buffer[len] == '\r' || buffer[len] == '\n')
|
||||||
// Name: xxxxx
|
buffer[len] = '\0';
|
||||||
|
}
|
||||||
|
GFX2_Log(GFX2_DEBUG, "GPL %s\n", buffer);
|
||||||
|
if (0 == memcmp(buffer, "Name: ", 6))
|
||||||
|
snprintf(context->Comment, sizeof(context->Comment), "GPL: %s", buffer + 6);
|
||||||
|
|
||||||
|
// Columns: 16
|
||||||
|
if (fscanf(file, "Columns: %d", &columns) != 1)
|
||||||
|
return;
|
||||||
|
Read_byte_line(file, buffer, sizeof(buffer));
|
||||||
|
/// @todo set grafx2 columns setting to match.
|
||||||
|
// #<newline>
|
||||||
|
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (i = 0; i < 256; i++)
|
||||||
|
{
|
||||||
|
skip_padding(file, 32);
|
||||||
|
if (fscanf(file, "%d", &r) != 1)
|
||||||
|
break;
|
||||||
|
skip_padding(file, 32);
|
||||||
|
if (fscanf(file, "%d", &g) != 1)
|
||||||
|
break;
|
||||||
|
skip_padding(file, 32);
|
||||||
|
if (fscanf(file, "%d\t", &b) != 1)
|
||||||
|
break;
|
||||||
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
||||||
|
break;
|
||||||
|
len = strlen(buffer);
|
||||||
|
while (len > 1)
|
||||||
{
|
{
|
||||||
File_error = 1;
|
len--;
|
||||||
return;
|
if (buffer[len] == '\r' || buffer[len] == '\n')
|
||||||
}
|
buffer[len] = '\0';
|
||||||
// Columns: 16
|
|
||||||
fscanf(file, "Columns: %d", &columns);
|
|
||||||
Read_byte_line(file, buffer, sizeof(buffer));
|
|
||||||
// TODO: set grafx2 columns setting to match.
|
|
||||||
// #<newline>
|
|
||||||
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
|
||||||
{
|
|
||||||
File_error = 1;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
/// @todo analyze color names to build shade table
|
||||||
|
|
||||||
for (i = 0; i < 256; i++)
|
GFX2_Log(GFX2_DEBUG, "GPL: %3d: RGB(%3d,%3d,%3d) %s\n", i, r,g,b, buffer);
|
||||||
{
|
context->Palette[i].R = r;
|
||||||
skip_padding(file, 32);
|
context->Palette[i].G = g;
|
||||||
fscanf(file, "%d", &r);
|
context->Palette[i].B = b;
|
||||||
skip_padding(file, 32);
|
}
|
||||||
fscanf(file, "%d", &g);
|
if (i > 0) // at least one color was read
|
||||||
skip_padding(file, 32);
|
File_error = 0;
|
||||||
fscanf(file, "%d\t", &b);
|
|
||||||
if (!Read_byte_line(file, buffer, sizeof(buffer)))
|
|
||||||
break;
|
|
||||||
len = strlen(buffer);
|
|
||||||
while (len > 1)
|
|
||||||
{
|
|
||||||
len--;
|
|
||||||
if (buffer[len] == '\r' || buffer[len] == '\n')
|
|
||||||
buffer[len] = '\0';
|
|
||||||
}
|
|
||||||
// TODO: analyze color names to build shade table
|
|
||||||
|
|
||||||
//printf("DBG: %3d: RGB(%3d,%3d,%3d) %s\n", i, r,g,b, buffer);
|
|
||||||
context->Palette[i].R = r;
|
|
||||||
context->Palette[i].G = g;
|
|
||||||
context->Palette[i].B = b;
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
File_error = 2;
|
|
||||||
|
|
||||||
// close the file
|
|
||||||
fclose(file);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
// Si on n'a pas réussi à ouvrir le fichier, alors il y a eu une erreur
|
File_error = 2;
|
||||||
File_error=1;
|
|
||||||
|
// close the file
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Save GPL (Gimp Palette) file format
|
||||||
void
|
void
|
||||||
Save_GPL (T_IO_Context * context)
|
Save_GPL (T_IO_Context * context)
|
||||||
{
|
{
|
||||||
// Gimp is a unix program, so use Unix file endings (LF aka '\n')
|
// Gimp is a unix program, so use Unix file endings (LF aka '\n')
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
|
||||||
File_error=0;
|
file = Open_file_write(context);
|
||||||
|
|
||||||
// Open output file
|
if (file != NULL )
|
||||||
if ((file=Open_file_write(context)) != NULL ){
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
File_error = 0;
|
||||||
fprintf (file, "GIMP Palette\n");
|
fprintf (file, "GIMP Palette\n");
|
||||||
fprintf (file, "Name: %s\n", context->File_name);
|
fprintf (file, "Name: %s\n", context->File_name);
|
||||||
// TODO: use actual columns value
|
// TODO: use actual columns value
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user