mirror of
https://github.com/simtactics/mysimulation.git
synced 2025-07-04 13:47:04 -04:00
Added libjpeg-turbo to FileHandler with a working File::ReadImageFile() function. The dependency on SOIL in libvitaboy Renderer has therefore been removed.
Also fixed formatting in recent source files. My next task is implementing IFF chunk reading. I'll also make a tool that can convert the game's behavior.iff file to an HTML page.
This commit is contained in:
parent
83ad5d89d1
commit
f23bcd7b4d
18 changed files with 816 additions and 295 deletions
|
@ -3,6 +3,7 @@ project(FileHandler)
|
|||
|
||||
add_subdirectory(libexpat)
|
||||
add_subdirectory(libfar)
|
||||
add_subdirectory(libjpeg-turbo)
|
||||
add_subdirectory(libpng)
|
||||
add_subdirectory(utk)
|
||||
add_subdirectory(xa)
|
||||
|
@ -14,6 +15,7 @@ set(FILEHANDLER_MINOR 0)
|
|||
|
||||
set(FILEHANDLER_SOURCES
|
||||
File.cpp
|
||||
Image.cpp
|
||||
)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/Libraries/FileHandler)
|
||||
|
@ -32,4 +34,4 @@ set_target_properties(FileHandler_shared PROPERTIES
|
|||
PREFIX ""
|
||||
IMPORT_PREFIX ""
|
||||
CLEAN_DIRECT_OUTPUT 1)
|
||||
target_link_libraries(FileHandler_shared kernel32)
|
||||
target_link_libraries(FileHandler_shared kernel32 jpegturbo_static)
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
libvitaboy - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
|
||||
FileHandler - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
|
@ -14,8 +14,6 @@
|
|||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <windows.h>
|
||||
#include "FileHandler.hpp"
|
||||
|
||||
namespace File {
|
||||
|
@ -23,20 +21,27 @@ namespace File {
|
|||
int Error = 0;
|
||||
unsigned FileSize = 0;
|
||||
|
||||
const uint8_t Signature[][4] = {
|
||||
{0xFF,0xD8,0xFF,0xE0} //JPEG
|
||||
};
|
||||
const uint8_t SignatureSize[] = {
|
||||
4 //JPEG
|
||||
};
|
||||
|
||||
uint8_t * ReadFile(const char * Filename){
|
||||
HANDLE hFile = CreateFile(Filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
||||
if(hFile == INVALID_HANDLE_VALUE){
|
||||
File::Error = (GetLastError() == ERROR_FILE_NOT_FOUND) ? FERR_NOT_FOUND : FERR_OPEN;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
FileSize = GetFileSize(hFile, NULL);
|
||||
if(FileSize == 0){
|
||||
CloseHandle(hFile);
|
||||
File::Error = FERR_BLANK;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
uint8_t * InData = (uint8_t*) malloc(FileSize);
|
||||
if(InData == NULL){
|
||||
CloseHandle(hFile);
|
||||
|
@ -47,7 +52,7 @@ uint8_t * ReadFile(const char * Filename){
|
|||
DWORD bytestransferred;
|
||||
BOOL result = ::ReadFile(hFile, InData, FileSize, &bytestransferred, NULL);
|
||||
CloseHandle(hFile);
|
||||
|
||||
|
||||
if(!result || bytestransferred != FileSize){
|
||||
free(InData);
|
||||
File::Error = FERR_READ;
|
||||
|
|
|
@ -14,19 +14,51 @@
|
|||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef FILEHANDLER_HPP
|
||||
#define FILEHANDLER_HPP
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#ifndef NOWINDOWS
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
struct Asset_t {
|
||||
uint32_t Group;
|
||||
uint32_t File;
|
||||
uint32_t Type;
|
||||
};
|
||||
|
||||
enum FErr {
|
||||
FERR_NOT_FOUND,
|
||||
FERR_OPEN,
|
||||
FERR_BLANK,
|
||||
FERR_MEMORY,
|
||||
FERR_READ
|
||||
FERR_READ,
|
||||
FERR_UNRECOGNIZED
|
||||
};
|
||||
|
||||
enum ImageFormat_t {
|
||||
FIMG_BGR24
|
||||
};
|
||||
|
||||
struct Image_t {
|
||||
unsigned Width, Height;
|
||||
ImageFormat_t Format;
|
||||
uint8_t * Data;
|
||||
};
|
||||
|
||||
namespace File {
|
||||
|
||||
extern int Error;
|
||||
extern unsigned FileSize;
|
||||
extern size_t FileSize;
|
||||
|
||||
uint8_t * ReadFile(const char * Filename);
|
||||
Image_t * ReadImageFile(const char * Filename);
|
||||
uint8_t * ReadJPG(Image_t * Image, const uint8_t * InData, size_t FileSize);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
95
Libraries/FileHandler/Image.cpp
Normal file
95
Libraries/FileHandler/Image.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
FileHandler - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <memory.h>
|
||||
#include "libjpeg-turbo/jpeglib.h"
|
||||
#define NOWINDOWS
|
||||
#include "FileHandler.hpp"
|
||||
|
||||
namespace File {
|
||||
|
||||
Image_t * ReadImageFile(const char * Filename){
|
||||
uint8_t * InData = File::ReadFile(Filename);
|
||||
if(InData == NULL) return NULL;
|
||||
|
||||
Image_t * Image = (Image_t*) malloc(sizeof(Image_t));
|
||||
if(Image == NULL){
|
||||
free(InData);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t * OutData = ReadJPG(Image, InData, File::FileSize);
|
||||
|
||||
free(InData);
|
||||
if(OutData != NULL){
|
||||
return Image;
|
||||
}
|
||||
|
||||
File::Error = FERR_UNRECOGNIZED;
|
||||
free(Image);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t * ReadJPG(Image_t * Image, const uint8_t * InData, size_t FileSize){
|
||||
//Initialize
|
||||
jpeg_decompress_struct cinfo;
|
||||
jpeg_error_mgr jerr;
|
||||
cinfo.err = jpeg_std_error(&jerr);
|
||||
jpeg_create_decompress(&cinfo);
|
||||
jpeg_mem_src(&cinfo, (unsigned char*) InData, FileSize);
|
||||
if(jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK){
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
return NULL;
|
||||
}
|
||||
if(!jpeg_start_decompress(&cinfo)){
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//Read
|
||||
unsigned row_stride = cinfo.output_width * cinfo.output_components;
|
||||
uint8_t * OutData = (uint8_t*) malloc(cinfo.output_width * cinfo.output_height * cinfo.output_components);
|
||||
if(OutData == NULL){
|
||||
jpeg_finish_decompress(&cinfo);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
return NULL;
|
||||
}
|
||||
for(unsigned i=0; cinfo.output_scanline < cinfo.output_height; i++){
|
||||
//According to the libjpeg documentation,
|
||||
//jpeg_read_scanlines can only really read 1 scanline at a time.
|
||||
uint8_t * Location = OutData + i*row_stride;
|
||||
if(!jpeg_read_scanlines(&cinfo, &Location, 1)){
|
||||
free(OutData);
|
||||
jpeg_finish_decompress(&cinfo);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//Close up
|
||||
jpeg_finish_decompress(&cinfo);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
if(Image){
|
||||
Image->Width = cinfo.output_width;
|
||||
Image->Height = cinfo.output_height;
|
||||
Image->Format = FIMG_BGR24;
|
||||
Image->Data = OutData;
|
||||
}
|
||||
return OutData;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue