Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Jan 26, 2018
1 parent dbc51fb commit 18035bc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 90 deletions.
1 change: 0 additions & 1 deletion griffin/griffin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,6 @@ XML
#include "../deps/miniupnpc/minisoap.c"
#endif


/*============================================================
HTTP SERVER
============================================================ */
Expand Down
63 changes: 31 additions & 32 deletions translation/drivers_ocr/ocr_tesseract.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,36 @@

static void* ocr_tesseract_init(int game_character_set)
{
const char* tess_char_set = NULL;
switch (game_character_set)
{
case RETRO_LANGUAGE_JAPANESE:
tess_char_set = "jpn";
break;

case RETRO_LANGUAGE_ENGLISH:
tess_char_set = "eng";
break;

case RETRO_LANGUAGE_SPANISH:
tess_char_set = "spa";
break;
}

if (!tess_char_set)
return NULL;

char* lang_data_dir = NULL;

/* TODO: get lang data from system dir */

bool pass = tess_init(lang_data_dir, tess_char_set);
if (pass)
{
/* data is unused by tesseract */
return (void*)32;
}

return NULL;
bool pass = false;
char* lang_data_dir = NULL;
const char* tess_char_set = NULL;

switch (game_character_set)
{
case RETRO_LANGUAGE_JAPANESE:
tess_char_set = "jpn";
break;

case RETRO_LANGUAGE_ENGLISH:
tess_char_set = "eng";
break;

case RETRO_LANGUAGE_SPANISH:
tess_char_set = "spa";
break;
}

if (!tess_char_set)
return NULL;

/* TODO: get lang data from system dir */
pass = tess_init(lang_data_dir, tess_char_set);

/* data is unused by tesseract */
if (pass)
return (void*)32;

return NULL;
}

static void ocr_tesseract_free(void* data)
Expand Down Expand Up @@ -74,4 +73,4 @@ const ocr_driver_t ocr_tesseract = {
ocr_tesseract_free,
ocr_tesseract_get_text,
"tesseract"
};
};
52 changes: 26 additions & 26 deletions translation/drivers_ocr/tesseract/wrapper/tess_get_text.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <boolean.h>
#include <tesseract/baseapi.h>
#include "tess_get_text.h"

Expand All @@ -8,38 +9,37 @@ static char* one_time_return_pointer = NULL;

char tess_last_error[ERROR_BUFFER_LENGTH];

bool tess_init(const char* lang_data_dir, const char* language)
bool tess_init(const char* lang_data_dir, const char* language)
{
api = new tesseract::TessBaseAPI();
snprintf(tess_last_error, ERROR_BUFFER_LENGTH, "No errors!\n");
api = new tesseract::TessBaseAPI();

if (api->Init(lang_data_dir, language)) {
snprintf(tess_last_error, ERROR_BUFFER_LENGTH, "Could not initialize tesseract.\n");
return false;
}
snprintf(tess_last_error, ERROR_BUFFER_LENGTH, "No errors!\n");

if (api->Init(lang_data_dir, language))
{
snprintf(tess_last_error, ERROR_BUFFER_LENGTH, "Could not initialize tesseract.\n");
return false;
}

return true;
}

void tess_deinit()
void tess_deinit(void)
{
if (one_time_return_pointer)
{
delete [] one_time_return_pointer;
}

if (api)
{
api->End();
}
if (one_time_return_pointer)
delete [] one_time_return_pointer;

if (api)
api->End();
}

char* tess_get_text(tess_image image)
{
if (one_time_return_pointer)
{
delete [] one_time_return_pointer;
}

api->SetImage(image.data, image.width, image.height, image.bytes_per_pixel, image.width * image.bytes_per_pixel/*bytes per line*/);
one_time_return_pointer = api->GetUTF8Text();
return one_time_return_pointer;
}
if (one_time_return_pointer)
delete [] one_time_return_pointer;

api->SetImage(image.data, image.width, image.height,
image.bytes_per_pixel, image.width * image.bytes_per_pixel/*bytes per line*/);
one_time_return_pointer = api->GetUTF8Text();
return one_time_return_pointer;
}
31 changes: 15 additions & 16 deletions translation/drivers_ocr/tesseract/wrapper/tess_get_text.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#ifndef TESS_GET_TEXT
#define TESS_GET_TEXT

#ifdef __cplusplus
extern "C" {
#endif
#include <boolean.h>
#include <retro_common_api.h>

typedef struct {
unsigned width;
unsigned height;
unsigned bytes_per_pixel;
void* data;
}tess_image;
RETRO_BEGIN_DECLS

extern char tess_last_error[];
typedef struct
{
unsigned width;
unsigned height;
unsigned bytes_per_pixel;
void* data;
} tess_image;

//if running in RetroArch language should be "eng" or "jpn"
/* if running in RetroArch language should be "eng" or "jpn" */
bool tess_init(const char* lang_data_dir, const char* language);
void tess_deinit();
void tess_deinit(void);
char* tess_get_text(tess_image image);

#ifdef __cplusplus
}
#endif
extern char tess_last_error[];

RETRO_END_DECLS

#endif
#endif
39 changes: 24 additions & 15 deletions translation/translation_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ static const translation_driver_t *translation_backends[] = {
static const translation_driver_t *current_translation_backend = NULL;
static void *translation_data = NULL;


static const translation_driver_t *translation_find_backend(const char* ident)
static const translation_driver_t *translation_find_backend(
const char* ident)
{
unsigned i;

Expand All @@ -30,7 +30,12 @@ static const translation_driver_t *translation_find_backend(const char* ident)
bool translation_driver_init(void)
{
settings_t *settings = config_get_ptr();
current_translation_backend = translation_find_backend(settings->arrays.translation_driver);

if (!settings)
return false;

current_translation_backend = translation_find_backend(
settings->arrays.translation_driver);
translation_data = NULL;

if (current_translation_backend)
Expand All @@ -40,19 +45,23 @@ bool translation_driver_init(void)

void translation_driver_free(void)
{
if (current_translation_backend && translation_data)
(*current_translation_backend->free)(translation_data);
if (current_translation_backend && translation_data)
(*current_translation_backend->free)(translation_data);
}

char* translation_driver_translate_image(struct ocr_image_info image)
{
char* translated_text = NULL;
if (current_translation_backend && translation_data)
{
if (current_translation_backend->translate_image)
translated_text = (*current_translation_backend->translate_image)(translation_data, image);
else
translated_text = (*current_translation_backend->translate_text)(translation_data, ocr_driver_get_text(image));
}
return translated_text;
}
char* translated_text = NULL;

if (current_translation_backend && translation_data)
{
if (current_translation_backend->translate_image)
translated_text = (*current_translation_backend->translate_image)
(translation_data, image);
else
translated_text = (*current_translation_backend->translate_text)
(translation_data, ocr_driver_get_text(image));
}

return translated_text;
}

0 comments on commit 18035bc

Please sign in to comment.