Home modules.gotpike.org
Username: Password: [Create Account]
[Forgot Password?]

Modules

ADT
Database
GTK2
GUI
IP
PiJAX
Public
Sql
Stdio
Subversion
System
Tools
Xosd
lua
v4l2
wx

Recent Changes

Public.Parser.XML2 1.50
Public.ZeroMQ 1.1
Public.Template.Mustache 1.0
Public.Protocols.XMPP 1.4
Sql.Provider.jdbc 1.0

Popular Downloads

Public.Parser.JSON2 1.0
Public.Parser.JSON 0.2
GTK2 2.23
Public.Web.FCGI 1.8
Public.Parser.XML2 1.48


Module Information
Public.Image.Subtitles
Viewing contents of Public_Image_Subtitles-1.0/subtitles.c

#include "global.h"

#include 

#include "svalue.h"
#include "object.h"
#include "array.h"
#include "stralloc.h"
#include "interpret.h"
#include "pike_types.h"
#include "pike_memory.h"
#include "threads.h"
#include "module_support.h"
#include "mapping.h"
#include "multiset.h"
#include "builtin_functions.h"
#include "opcodes.h"
#include "pike_macros.h"
#include "version.h"

/* must be included last */
#define ALLOW_DEPRECATED_MODULE_MAGIC
#include "module_magic.h"

#include "libsub/config.h"

#ifdef HAVE_STDLIB_H
# include 
#endif

#ifdef HAVE_UNISTD_H
# include 
#endif

#ifdef HAVE_SYS_TYPES_H
# include 
#endif

# include 
# include 
#include "libsub/libsub.h"

struct subtitle
{
  struct image *image;
  struct subtitle **subs;
  int texture; // if non-zero, render to a GL texture
};


/*! @module Public
 */
/*! @module Image
 */
/*! @module Subtitles
 */

/*! @decl void set_font_directories( array(string) directories );
 *! Set the directores in which font files are located.
 *!
 *! There should at minimum be a font with a name beginning with
 *! 'Arial' in one of the directories.
 */
static void f_set_font_directories( INT32 args )
{
  struct array *a;
  static char **odirs;
  char **dirs;
  int i;
  get_all_args( "set_font_directories", args, "%a", &a );

  dirs = malloc( sizeof( char * ) * (a->size+1) );

  for( i = 0; isize; i++ )
  {
    if( a->item[i].type != T_STRING )
    {
      int j;
      for( j = 0; jitem[i].u.string->str );
  }
  dirs[i] = 0;

  lsub_set_font_directories( dirs );
  if( odirs )
  {
    for( i = 0; odirs[i]; i++ )
      free( odirs[i] );
    free( odirs );
  }
  odirs = dirs;
  
  pop_n_elems( args );
  push_int( 0 );
}

#define THIS ((struct subtitle *)Pike_fp->current_storage)

/*! @class Render
 */

static void init_subtitle( struct object *o )
{
  MEMSET( THIS, 0, sizeof( struct subtitle ) );
}

static void exit_subtitle( struct object *o )
{
  if( THIS->image ) lsub_free_image( THIS->image );
  if( THIS->subs )  lsub_free_subtitles( THIS->subs );
}


/*! @decl int update( float seconds );
 *! Update the current time. If this function returns true a new image
 *! has been generated.
 *!
 *! If a texture has been set using @[set_texture], the image in the
 *! texture will have been updated automatically.
 */
static void f_update( INT32 args )
{
  FLOAT_TYPE t;
  int render = 0;
  get_all_args( "update", args, "%f", &t );
  pop_n_elems( args );

  if( THIS->subs && THIS->image )
  {
    render = lsub_render_subtitles( THIS->subs, (int)(t*100), THIS->image );
    if( render && THIS->texture )
    {
      glBindTexture( GL_TEXTURE_2D, THIS->texture );
/*        printf("setting texture: %.1f %d\n", t, THIS->texture ); */
      glTexSubImage2D (GL_TEXTURE_2D, 0, 
		       0, 0,
		       THIS->image->w, THIS->image->h,
		       GL_RGBA, GL_UNSIGNED_BYTE,
		       THIS->image->data );
    }
  }
  push_int( render );
}

/*! @decl string get_image( )
 *! Returns the raw image data as a string. Each pixel is 4 bytes, and
 *! the order is ABGR.
 */
static void f_get_image( INT32 args )
{
  pop_n_elems(args);
  if( THIS->image )
  {
    push_string( make_shared_binary_string( (unsigned char *)THIS->image->data,
					    THIS->image->w*
					    THIS->image->h*4 ) );
  }
  else
    push_int(0);
}

/*! @decl void set_texture( int texture_id )
 *! Indicates that drawing should be done directly to the OpenGL
 *! texture specified.
 */
static void f_set_texture( INT32 args )
{
  INT_TYPE t;
  get_all_args( "set_texture", args, "%d", &t );
  THIS->texture = t;
}

/*! @decl void set_size( int width, int height )
 *! Set the size of the image that will be generated. Regardless of
 *! the aspect ratio square pixels are always asumed.
 */
static void f_set_size( INT32 args )
{
  INT_TYPE w, h;
  get_all_args( "set_size", args, "%d%d", &w, &h );
  if( THIS->image )
    lsub_free_image( THIS->image );
  THIS->image = lsub_allocate_image( w, h );
}

/*! @decl int set_file( string file )
 *! Read the subtitles from the specified file.
 *! If no recognizable subtitles was found in the file, 0 is returned.
 */
static void f_set_file( INT32 args )
{
  unsigned char *fn;
  get_all_args( "set_file", args, "%s", &fn );
  if( THIS->subs )
    lsub_free_subtitles( THIS->subs );
  THIS->subs = lsub_use_subtitlefile( fn );
  pop_n_elems( args );
  push_int( !(!THIS->subs) );
}

/*! @decl int set_data( string file )
 *! Decode the subtitles from the specified data.
 *! If no recognizable subtitles were found in the data, 0 is returned.
 */
static void f_set_data( INT32 args )
{
  struct pike_string *data;
  get_all_args( "set_data", args, "%S", &data );

  if( THIS->subs )
    lsub_free_subtitles( THIS->subs );

  THIS->subs = lsub_read_subtitles_memory( (char *)data->str, data->len );

  pop_n_elems( args );
  push_int( !(!THIS->subs) );
}


/*! @endclass
 */

/*! @endmodule
 */

/*! @endmodule
 */


void pike_module_init()
{
  add_function("set_font_directories", f_set_font_directories,
	       "function(array(string):void)", 0 );

  start_new_program();
  ADD_STORAGE( struct subtitle );
  {
    set_init_callback( init_subtitle );
    set_exit_callback( exit_subtitle );
    add_function("set_file", f_set_file, "function(string:int)", 0 );
    add_function("set_data", f_set_data, "function(string:int)", 0 );
    add_function("set_size", f_set_size, "function(int,int:void)", 0 );
    add_function("set_texture", f_set_texture, "function(int:void)", 0 );
    add_function("update", f_update, "function(float:int)", 0 );
    add_function("get_image_data", f_get_image, "function(void:string)", 0 );
  }
  end_class( "Render",0 );
}

void pike_module_exit()
{
}


gotpike.org | Copyright © 2004 - 2019 | Pike is a trademark of Department of Computer and Information Science, Linköping University