<commit_before>/*
    Copyright (C) 1998 by Jorrit Tyberghein and Dan Ogles.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "sysdef.h"

#if defined(OS_WIN32)
#include <windows.h>
#endif

#include <GL/gl.h>
//#include <GL/glu.h>

#include "cs3d/opengl/ogl_txtcache.h"
#include "cs3d/opengl/ogl_txtmgr.h"
#include "cs3d/opengl/ogl_hicache.h"
#include "cs3d/opengl/ogl_g3d.h"
#include "isystem.h"
#include "ilghtmap.h"
#include "igraph3d.h"

// need definitions of R24(), G24(), and B24()
#ifndef NORMAL_LIGHT_LEVEL
#define NORMAL_LIGHT_LEVEL 128
#endif

// these are the number of mipmap levels generated by CS
static int const AUTO_MIPMAP_LEVELS = 4;

OpenGLTextureCache::OpenGLTextureCache(int size, int bitdepth)
  : HighColorCache(size,HIGHCOLOR_TEXCACHE,bitdepth)
{
}

OpenGLTextureCache::~OpenGLTextureCache ()
{
  Clear ();
}

void OpenGLTextureCache::Dump()
{
}

void OpenGLTextureCache::Load (csHighColorCacheData *d)
{
  iTextureHandle* txt_handle = (iTextureHandle*)d->pSource;
  csTextureMM* txt_mm = (csTextureMM*)txt_handle->GetPrivateObject ();
  bool const transparent = txt_handle->GetTransparent ();

  CHK (GLuint *texturehandle = new GLuint);

  // bind all 4 mipmap levels if possible
  glGenTextures (1,texturehandle);

  glBindTexture (GL_TEXTURE_2D, *texturehandle);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, rstate_bilinearmap ? GL_LINEAR : GL_NEAREST);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rstate_bilinearmap ? GL_LINEAR : GL_NEAREST);
  if (txt_mm->for_3d())
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rstate_bilinearmap ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST);

  csTexture* txt_unl = txt_mm->get_texture (0);
  int texture_width = txt_unl->get_width ();
  int texture_height = txt_unl->get_height ();

  CHK (unsigned char * const tempdata = new unsigned char[texture_width*texture_height*4]);

  // generate mipmaps
  int mipmaplevel=0;
  while ( (texture_width > 0) || (texture_height > 0) )
  {
    // the first X mipmap levels are generated by CS
    if (mipmaplevel < AUTO_MIPMAP_LEVELS)
    {
      txt_unl = txt_mm->get_texture (mipmaplevel);
      ULong const *source = (ULong *)txt_unl->get_bitmap ();

      unsigned char * dest = tempdata;
      for (int count = texture_width * texture_height; count > 0; count--)
      {
        dest[0] = R24(*source);
        dest[1] = G24(*source);
        dest[2] = B24(*source);

        if (transparent && (*source == 0))
          dest[3] = 0;
        else
          dest[3] = 255;

        dest+=4; source++;
      }
    }
    else // manually generate the last mipmap levels by scaling down
    	 // the previous mipmap by 2
    {
      ULong *source = (ULong *)tempdata;
      ULong *dest = (ULong *)tempdata;
      for (int newy=0; newy < texture_height; newy++)
      {
        for (int newx=0; newx < texture_width; newx++)
        {
          *dest++ = *source;
	  source += 2;
        }
	source += texture_height*2;
      }
    }

    // now that the texture has been generated, send it to openGL
    if (transparent)
        glTexImage2D(GL_TEXTURE_2D,mipmaplevel,GL_RGBA,texture_width,
		texture_height,0,GL_RGBA,GL_UNSIGNED_BYTE,
		tempdata);
    else
        glTexImage2D(GL_TEXTURE_2D,mipmaplevel,GL_RGB,texture_width,
		texture_height,0,GL_RGBA,GL_UNSIGNED_BYTE,
		tempdata);

    // shrink down to the next mipmap level
    texture_height /= 2;
    texture_width /= 2;
    mipmaplevel++;

    // only 0 level mipmip for a 2d texture!
    if (txt_mm->for_2d()) break;
  }

  delete [] tempdata;

  d->pData = texturehandle;
}

///
void OpenGLTextureCache::Unload (csHighColorCacheData *d)
{
   if (d && d->pData)
   {
     glDeleteTextures(1,(GLuint *)d->pData);
     delete (GLuint *)d->pData;
   }
}

OpenGLLightmapCache::OpenGLLightmapCache(int size, int bitdepth)
  : HighColorCache(size,HIGHCOLOR_LITCACHE,bitdepth)
{
}

OpenGLLightmapCache::~OpenGLLightmapCache ()
{
  Clear ();
}

void OpenGLLightmapCache::Dump()
{
}

///
void OpenGLLightmapCache::Load(csHighColorCacheData *d)
{
  iLightMap *lightmap_info = (iLightMap *)d->pSource;

  int lmwidth = lightmap_info->GetWidth ();
  int lmheight = lightmap_info->GetHeight ();
  int lmrealwidth = lightmap_info->GetRealWidth ();
  int lmrealheight = lightmap_info->GetRealHeight ();

//lmheight = floor(pow(2.0, ceil(log(lmheight)/log(2.0)) ) );
//lmwidth = floor(pow(2.0, ceil(log(lmwidth)/log(2.0)) ) );

  unsigned char *red_data = lightmap_info->GetMap (0);
  unsigned char *green_data = lightmap_info->GetMap (1);
  unsigned char *blue_data = lightmap_info->GetMap (2);

  // @@@ Note @@@
  // The lightmap data used by Crystal Space is in another format
  // then the lightmap data needed by OpenGL. Maybe we should consider
  // switching Crystal Space to this format as it is more standard?
  // For the software renderer this would be almost no problem and
  // it would make the hardware renderers faster as the following
  // conversion would not be needed anymore.
  CHK (unsigned char *lm_data = new unsigned char[lmwidth*lmheight*4]);
  unsigned char *walkdata = lm_data;
  unsigned char *last_data = walkdata+(lmwidth*lmheight*4);
  while (walkdata < last_data)
  {
    *walkdata++ = *red_data++;
    *walkdata++ = *green_data++;
    *walkdata++ = *blue_data++;
    *walkdata++ = NORMAL_LIGHT_LEVEL;
  }

  // Because our actual lightmap is a smaller lightmap in a larger
  // po2 texture we can have problems when using GL_LINEAR for
  // the GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER.
  // We still have to copy the last row and column of the lightmap
  // to just outside the original lightmap boundaries.
  // Otherwise we get a random colored border. Because we use
  // GL_REPEAT the problem is even worse. In that case we also have to
  // copy the first row and column to the last row and column in
  // the po2 lightmap texture.
  int i;
  if (lmwidth != lmrealwidth)
  {
    // Point to last real column.
    unsigned char* lastrealcol = lm_data+(lmrealwidth-1)*4;
    // Copy to next column.
    for (i = 0 ; i < lmheight ; i++)
    {
      lastrealcol[4] = lastrealcol[0];
      lastrealcol[5] = lastrealcol[1];
      lastrealcol[6] = lastrealcol[2];
      lastrealcol += lmwidth*4;
    }

    // Point to last physical column.
    unsigned char* lastcol = lm_data+(lmwidth-1)*4;
    unsigned char* firstcol = lm_data;
    // Copy first column to last.
    for (i = 0 ; i < lmheight ; i++)
    {
      lastcol[0] = firstcol[0];
      lastcol[1] = firstcol[1];
      lastcol[2] = firstcol[2];
      lastcol += lmwidth*4;
      firstcol += lmwidth*4;
    }
  }
  if (lmheight != lmrealheight)
  {
    // Point to last real row.
    unsigned char* lastrealrow = lm_data+lmwidth*(lmrealheight-1)*4;
    // Copy to next row.
    for (i = 0 ; i < lmwidth*4 ; i++) lastrealrow[i+lmwidth*4] = lastrealrow[i];

    // Point to last physical row.
    unsigned char* lastrow = lm_data+lmwidth*(lmheight-1)*4;
    unsigned char* firstrow = lm_data;
    // Copy first row to last.
    for (i = 0 ; i < lmwidth*4 ; i++) lastrow[i] = firstrow[i];
  }

  CHK (GLuint *lightmaphandle = new GLuint);
  glGenTextures(1,lightmaphandle);
  glBindTexture(GL_TEXTURE_2D,*lightmaphandle);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,lmwidth,
		lmheight,0,GL_RGBA,GL_UNSIGNED_BYTE,
		lm_data);

  GLenum errtest;
  errtest = glGetError();
  if (errtest != GL_NO_ERROR)
  {
    //SysPrintf (MSG_DEBUG_0,"openGL error string: %s\n",gluErrorString(errtest) );
  }

  delete []lm_data;

/*
    CsPrintf(MSG_DEBUG_0,"caching lightmap in handle %d\n",*lightmaphandle);
    CsPrintf(MSG_DEBUG_0,"size (%d,%d)\n",lmwidth,lmheight);
    CsPrintf(MSG_DEBUG_0,"lightmap data location %x\n",lightmap_info);
    */

  d->pData = lightmaphandle;
}

///
void OpenGLLightmapCache::Unload(csHighColorCacheData *d)
{
  if (d && d->pData)
  {
    glDeleteTextures(1,(GLuint *)d->pData);
    delete (GLuint *)d->pData;
  }
}
<commit_msg>Yet another mipmap bug fix.  I am learning more about OpenGL mipmaps than I ever wanted to.<commit_after>/*
    Copyright (C) 1998 by Jorrit Tyberghein and Dan Ogles.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "sysdef.h"

#if defined(OS_WIN32)
#include <windows.h>
#endif

#include <GL/gl.h>
//#include <GL/glu.h>

#include "cs3d/opengl/ogl_txtcache.h"
#include "cs3d/opengl/ogl_txtmgr.h"
#include "cs3d/opengl/ogl_hicache.h"
#include "cs3d/opengl/ogl_g3d.h"
#include "isystem.h"
#include "ilghtmap.h"
#include "igraph3d.h"

// need definitions of R24(), G24(), and B24()
#ifndef NORMAL_LIGHT_LEVEL
#define NORMAL_LIGHT_LEVEL 128
#endif

// these are the number of mipmap levels generated by CS
static int const AUTO_MIPMAP_LEVELS = 3;

OpenGLTextureCache::OpenGLTextureCache(int size, int bitdepth)
  : HighColorCache(size,HIGHCOLOR_TEXCACHE,bitdepth)
{
}

OpenGLTextureCache::~OpenGLTextureCache ()
{
  Clear ();
}

void OpenGLTextureCache::Dump()
{
}

void OpenGLTextureCache::Load (csHighColorCacheData *d)
{
  iTextureHandle* txt_handle = (iTextureHandle*)d->pSource;
  csTextureMM* txt_mm = (csTextureMM*)txt_handle->GetPrivateObject ();
  bool const transparent = txt_handle->GetTransparent ();

  CHK (GLuint *texturehandle = new GLuint);

  // bind all 4 mipmap levels if possible
  glGenTextures (1,texturehandle);

  glBindTexture (GL_TEXTURE_2D, *texturehandle);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, rstate_bilinearmap ? GL_LINEAR : GL_NEAREST);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rstate_bilinearmap ? GL_LINEAR : GL_NEAREST);
  if (!txt_mm->for_2d())
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rstate_bilinearmap ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST);

  csTexture* txt_unl = txt_mm->get_texture (0);
  int texture_width = txt_unl->get_width ();
  int texture_height = txt_unl->get_height ();

  CHK (unsigned char * const tempdata = new unsigned char[texture_width*texture_height*4]);

  // generate mipmaps
  int mipmaplevel=0;
  while ( mipmaplevel < 12 )
  {
    // the first X mipmap levels are generated by CS
    if (mipmaplevel < AUTO_MIPMAP_LEVELS)
    {
      txt_unl = txt_mm->get_texture (mipmaplevel);
      ULong const *source = (ULong *)txt_unl->get_bitmap ();

      unsigned char * dest = tempdata;
      for (int count = texture_width * texture_height; count > 0; count--)
      {
        dest[0] = R24(*source);
        dest[1] = G24(*source);
        dest[2] = B24(*source);

        if (transparent && (*source == 0))
          dest[3] = 0;
        else
          dest[3] = 255;

        dest+=4; source++;
      }
    }
    else // manually generate the last mipmap levels by scaling down
    	 // the previous mipmap by 2
    {
      ULong *source = (ULong *)tempdata;
      ULong *dest = (ULong *)tempdata;
      for (int newy=0; newy < texture_height; newy++)
      {
        for (int newx=0; newx < texture_width; newx++)
        {
          *dest++ = *source;
	  source += 2;
        }
	source += texture_height*2;
      }
    }

    // now that the texture has been generated, send it to openGL
    if (transparent)
        glTexImage2D(GL_TEXTURE_2D,mipmaplevel,GL_RGBA,texture_width,
		texture_height,0,GL_RGBA,GL_UNSIGNED_BYTE,
		tempdata);
    else
        glTexImage2D(GL_TEXTURE_2D,mipmaplevel,GL_RGB,texture_width,
		texture_height,0,GL_RGBA,GL_UNSIGNED_BYTE,
		tempdata);

    // only 0 level mipmip for a 2d texture!
    if (txt_mm->for_2d()) break;

    // bugger out if we have just made the smallest mipmap needed
    if ( (texture_height == 1) &&
         (texture_width  == 1) )
    	break;

    // shrink down to the next mipmap level
    if (texture_height > 1) texture_height /= 2;
    if (texture_width > 1) texture_width /= 2;
    mipmaplevel++;
  }

  delete [] tempdata;

  d->pData = texturehandle;
}

///
void OpenGLTextureCache::Unload (csHighColorCacheData *d)
{
   if (d && d->pData)
   {
     glDeleteTextures(1,(GLuint *)d->pData);
     delete (GLuint *)d->pData;
   }
}

OpenGLLightmapCache::OpenGLLightmapCache(int size, int bitdepth)
  : HighColorCache(size,HIGHCOLOR_LITCACHE,bitdepth)
{
}

OpenGLLightmapCache::~OpenGLLightmapCache ()
{
  Clear ();
}

void OpenGLLightmapCache::Dump()
{
}

///
void OpenGLLightmapCache::Load(csHighColorCacheData *d)
{
  iLightMap *lightmap_info = (iLightMap *)d->pSource;

  int lmwidth = lightmap_info->GetWidth ();
  int lmheight = lightmap_info->GetHeight ();
  int lmrealwidth = lightmap_info->GetRealWidth ();
  int lmrealheight = lightmap_info->GetRealHeight ();

//lmheight = floor(pow(2.0, ceil(log(lmheight)/log(2.0)) ) );
//lmwidth = floor(pow(2.0, ceil(log(lmwidth)/log(2.0)) ) );

  unsigned char *red_data = lightmap_info->GetMap (0);
  unsigned char *green_data = lightmap_info->GetMap (1);
  unsigned char *blue_data = lightmap_info->GetMap (2);

  // @@@ Note @@@
  // The lightmap data used by Crystal Space is in another format
  // then the lightmap data needed by OpenGL. Maybe we should consider
  // switching Crystal Space to this format as it is more standard?
  // For the software renderer this would be almost no problem and
  // it would make the hardware renderers faster as the following
  // conversion would not be needed anymore.
  CHK (unsigned char *lm_data = new unsigned char[lmwidth*lmheight*4]);
  unsigned char *walkdata = lm_data;
  unsigned char *last_data = walkdata+(lmwidth*lmheight*4);
  while (walkdata < last_data)
  {
    *walkdata++ = *red_data++;
    *walkdata++ = *green_data++;
    *walkdata++ = *blue_data++;
    *walkdata++ = NORMAL_LIGHT_LEVEL;
  }

  // Because our actual lightmap is a smaller lightmap in a larger
  // po2 texture we can have problems when using GL_LINEAR for
  // the GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER.
  // We still have to copy the last row and column of the lightmap
  // to just outside the original lightmap boundaries.
  // Otherwise we get a random colored border. Because we use
  // GL_REPEAT the problem is even worse. In that case we also have to
  // copy the first row and column to the last row and column in
  // the po2 lightmap texture.
  int i;
  if (lmwidth != lmrealwidth)
  {
    // Point to last real column.
    unsigned char* lastrealcol = lm_data+(lmrealwidth-1)*4;
    // Copy to next column.
    for (i = 0 ; i < lmheight ; i++)
    {
      lastrealcol[4] = lastrealcol[0];
      lastrealcol[5] = lastrealcol[1];
      lastrealcol[6] = lastrealcol[2];
      lastrealcol += lmwidth*4;
    }

    // Point to last physical column.
    unsigned char* lastcol = lm_data+(lmwidth-1)*4;
    unsigned char* firstcol = lm_data;
    // Copy first column to last.
    for (i = 0 ; i < lmheight ; i++)
    {
      lastcol[0] = firstcol[0];
      lastcol[1] = firstcol[1];
      lastcol[2] = firstcol[2];
      lastcol += lmwidth*4;
      firstcol += lmwidth*4;
    }
  }
  if (lmheight != lmrealheight)
  {
    // Point to last real row.
    unsigned char* lastrealrow = lm_data+lmwidth*(lmrealheight-1)*4;
    // Copy to next row.
    for (i = 0 ; i < lmwidth*4 ; i++) lastrealrow[i+lmwidth*4] = lastrealrow[i];

    // Point to last physical row.
    unsigned char* lastrow = lm_data+lmwidth*(lmheight-1)*4;
    unsigned char* firstrow = lm_data;
    // Copy first row to last.
    for (i = 0 ; i < lmwidth*4 ; i++) lastrow[i] = firstrow[i];
  }

  CHK (GLuint *lightmaphandle = new GLuint);
  glGenTextures(1,lightmaphandle);
  glBindTexture(GL_TEXTURE_2D,*lightmaphandle);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,lmwidth,
		lmheight,0,GL_RGBA,GL_UNSIGNED_BYTE,
		lm_data);

  GLenum errtest;
  errtest = glGetError();
  if (errtest != GL_NO_ERROR)
  {
    //SysPrintf (MSG_DEBUG_0,"openGL error string: %s\n",gluErrorString(errtest) );
  }

  delete []lm_data;

/*
    CsPrintf(MSG_DEBUG_0,"caching lightmap in handle %d\n",*lightmaphandle);
    CsPrintf(MSG_DEBUG_0,"size (%d,%d)\n",lmwidth,lmheight);
    CsPrintf(MSG_DEBUG_0,"lightmap data location %x\n",lightmap_info);
    */

  d->pData = lightmaphandle;
}

///
void OpenGLLightmapCache::Unload(csHighColorCacheData *d)
{
  if (d && d->pData)
  {
    glDeleteTextures(1,(GLuint *)d->pData);
    delete (GLuint *)d->pData;
  }
}
<|endoftext|>


<commit_before>#pragma once

namespace thinkyoung {
    namespace client {

#ifndef ALP_TEST_NETWORK
        static const std::vector<std::string> SeedNodes =
        {
			"47.52.62.164:61696",
			"47.90.206.220:61696",
			"47.90.202.190:61696",
			"47.91.21.67:61696",
			"47.91.22.116:61696",
			"47.52.102.41:61696",
			"101.200.32.205:61696",
			"59.110.138.70:61696",
			"47.88.153.65:61696",
			"47.88.228.70:61696"
        };
#else
        static const std::vector<std::string> SeedNodes
        {
        };
#endif

    }
} // thinkyoung::client
<commit_msg>Add new seed node.<commit_after>#pragma once

namespace thinkyoung {
    namespace client {

#ifndef ALP_TEST_NETWORK
        static const std::vector<std::string> SeedNodes =
        {
            "47.52.62.164:61696",
            "47.90.206.220:61696",
            "47.90.202.190:61696",
            "47.91.21.67:61696",
            "47.91.22.116:61696",
            "47.52.102.41:61696",
            "101.200.32.205:61696",
            "59.110.138.70:61696",
            "47.88.153.65:61696",
            "47.88.228.70:61696",
            "node.achain.com:61696"
        };
#else
        static const std::vector<std::string> SeedNodes
        {
        };
#endif

    }
} // thinkyoung::client
<|endoftext|>


<commit_before>/*
 * This material contains unpublished, proprietary software of
 * Entropic Research Laboratory, Inc. Any reproduction, distribution,
 * or publication of this work must be authorized in writing by Entropic
 * Research Laboratory, Inc., and must bear the notice:
 *
 *    "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc.
 *                   All rights reserved"
 *
 * The copyright notice above does not evidence any actual or intended
 * publication of this source code.
 *
 * Written by:  Derek Lin
 * Checked by:
 * Revised by:  David Talkin
 *
 * Brief description:  Estimates F0 using normalized cross correlation and
 *   dynamic programming.
 *
 */

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <limits.h>

#include "f0.h"

int	    debug_level = 0;

// ----------------------------------------
// Externs
extern "C" {
int init_dp_f0(double freq, F0_params *par, long *buffsize, long *sdstep);
int dp_f0(float *fdata, int buff_size, int sdstep, double freq, F0_params *par,
          float **f0p_pt, float **vuvp_pt, float **rms_speech_pt,
          float **acpkp_pt, int *vecsize, int last_time);
}

// ----------------------------------------
// Forward Decl

static int check_f0_params(F0_params *par, double sample_freq);

// ----------------------------------------
// SW: Library interaction
void sw_getf0_output(float *f0p, float *vuvp, float *rms_speech, float *acpkp,
                     int vecsize)
{
}

long sw_getf0_read(float *buffer, long num_records)
{
  return 0;
}

/// Return how many floats we read. Skip back `step` frames.
long sw_getf0_read_overlap(float *buffer, long num_records, long step)
{
  return 0;
}

int main_sw_tmp(int ac, char *av[])
{
  float *fdata;
  int done;
  long buff_size, actsize;
  double sf, output_starts, frame_rate;
  float *f0p, *vuvp, *rms_speech, *acpkp;
  int i, vecsize;
  long sdstep = 0;

  F0_params par;
  par.cand_thresh = 0.3;
  par.lag_weight = 0.3;
  par.freq_weight = 0.02;
  par.trans_cost = 0.005;
  par.trans_amp = 0.5;
  par.trans_spec = 0.5;
  par.voice_bias = 0.0;
  par.double_cost = 0.35;
  par.min_f0 = 50;
  par.max_f0 = 550;
  par.frame_step = 0.01;
  par.wind_dur = 0.0075;
  par.n_cands = 20;
  par.mean_f0 = 200;     /* unused */
  par.mean_f0_weight = 0.0;  /* unused */
  par.conditioning = 0;    /*unused */

#define SW_CUSTOMIZABLE(x) //TODO(sw)
  SW_CUSTOMIZABLE(debug_level);

  SW_CUSTOMIZABLE(par.frame_step);
  SW_CUSTOMIZABLE(par.cand_thresh);
  SW_CUSTOMIZABLE(par.lag_weight);
  SW_CUSTOMIZABLE(par.freq_weight);
  SW_CUSTOMIZABLE(par.trans_cost);
  SW_CUSTOMIZABLE(par.trans_amp);
  SW_CUSTOMIZABLE(par.trans_spec);
  SW_CUSTOMIZABLE(par.voice_bias);
  SW_CUSTOMIZABLE(par.double_cost);
  SW_CUSTOMIZABLE(par.min_f0);
  SW_CUSTOMIZABLE(par.max_f0);
  SW_CUSTOMIZABLE(par.wind_dur);
  SW_CUSTOMIZABLE(par.n_cands);
#undef SW_CUSTOMIZABLE

#define SW_FILE_PARAMS(x, y) //TODO (sw)
  SW_FILE_PARAMS(sf, "sampling frequency");
#undef SW_FILE_PARAMS

  if(check_f0_params(&par, sf)){
    Fprintf(stderr, "invalid/inconsistent parameters -- exiting.\n");
    exit(1);
  }

  /*SW: Removed range restricter, but this may be interesting:
    if (total_samps < ((par->frame_step * 2.0) + par->wind_dur) * sf), then
      input range too small*/

  output_starts = par.wind_dur/2.0;
  /* Average delay due to loc. of ref. window center. */
  frame_rate = 1.0 / par.frame_step;

  /* Initialize variables in get_f0.c; allocate data structures;
   * determine length and overlap of input frames to read.
   */
  if (init_dp_f0(sf, &par, &buff_size, &sdstep)
      || buff_size > INT_MAX || sdstep > INT_MAX)
  {
    Fprintf(stderr, "problem in init_dp_f0().\n");
    exit(1);
  }

  /*SW: pass sdstep to caller so it knows how much we have to buffer. */

  if (debug_level)
    Fprintf(stderr, "init_dp_f0 returned buff_size %ld, sdstep %ld.\n",
	    buff_size, sdstep);

  fdata = static_cast<float*>(malloc(sizeof(float) * buff_size));

  actsize = sw_getf0_read(fdata, buff_size);

  while (1) {

    done = (actsize < buff_size);

    if (dp_f0(fdata, (int) actsize, (int) sdstep, sf, &par,
	      &f0p, &vuvp, &rms_speech, &acpkp, &vecsize, done)) {
      Fprintf(stderr, "problem in dp_f0().\n");
      exit(1);
    }

    sw_getf0_output(f0p, vuvp, rms_speech, acpkp, vecsize);

    if (done)
      break;

    actsize = sw_getf0_read_overlap(fdata, buff_size, sdstep);

  }

  exit(0);
}

/*
 * Some consistency checks on parameter values.
 * Return a positive integer if any errors detected, 0 if none.
 */

static int check_f0_params(F0_params *par, double sample_freq)
{
  int	  error = 0;
  double  dstep;

  if((par->cand_thresh < 0.01) || (par->cand_thresh > 0.99)) {
    Fprintf(stderr,
	    "ERROR: cand_thresh parameter must be between [0.01, 0.99].\n");
    error++;
  }
  if((par->wind_dur > .1) || (par->wind_dur < .0001)) {
    Fprintf(stderr,
	    "ERROR: wind_dur parameter must be between [0.0001, 0.1].\n");
    error++;
  }
  if((par->n_cands > 100) || (par->n_cands < 3)){
    Fprintf(stderr,
	    "ERROR: n_cands parameter must be between [3,100].\n");
    error++;
  }
  if((par->max_f0 <= par->min_f0) || (par->max_f0 >= (sample_freq/2.0)) ||
     (par->min_f0 < (sample_freq/10000.0))){
    Fprintf(stderr,
	    "ERROR: min(max)_f0 parameter inconsistent with sampling frequency.\n");
    error++;
  }
  dstep = ((double)((int)(0.5 + (sample_freq * par->frame_step))))/sample_freq;
  if(dstep != par->frame_step) {
    if(debug_level)
      Fprintf(stderr,
	      "Frame step set to %f to exactly match signal sample rate.\n",
	      dstep);
    par->frame_step = dstep;
  }
  if((par->frame_step > 0.1) || (par->frame_step < (1.0/sample_freq))){
    Fprintf(stderr,
	    "ERROR: frame_step parameter must be between [1/sampling rate, 0.1].\n");
    error++;
  }

  return(error);
}
<commit_msg>Error handling<commit_after>/*
 * This material contains unpublished, proprietary software of
 * Entropic Research Laboratory, Inc. Any reproduction, distribution,
 * or publication of this work must be authorized in writing by Entropic
 * Research Laboratory, Inc., and must bear the notice:
 *
 *    "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc.
 *                   All rights reserved"
 *
 * The copyright notice above does not evidence any actual or intended
 * publication of this source code.
 *
 * Written by:  Derek Lin
 * Checked by:
 * Revised by:  David Talkin
 *
 * Brief description:  Estimates F0 using normalized cross correlation and
 *   dynamic programming.
 *
 */

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <limits.h>
#include <stdexcept>
#include <sstream>
#include <vector>

#include "f0.h"

int	    debug_level = 0;

// ----------------------------------------
// Externs
extern "C" {
int init_dp_f0(double freq, F0_params *par, long *buffsize, long *sdstep);
int dp_f0(float *fdata, int buff_size, int sdstep, double freq, F0_params *par,
          float **f0p_pt, float **vuvp_pt, float **rms_speech_pt,
          float **acpkp_pt, int *vecsize, int last_time);
}

// ----------------------------------------
// SW: Library interaction
void sw_getf0_output(float *f0p, float *vuvp, float *rms_speech, float *acpkp,
                     int vecsize)
{
}

long sw_getf0_read(float *buffer, long num_records)
{
  return 0;
}

/// Return how many floats we read. Skip back `step` frames.
long sw_getf0_read_overlap(float *buffer, long num_records, long step)
{
  return 0;
}

namespace GetF0 {

// EXCEPTIONS

#define CREATE_ERROR(_Name, _Base)                                   \
  class _Name : public _Base {                                       \
  public:                                                            \
    explicit _Name(const std::string &what_arg) : _Base(what_arg) {} \
  };

CREATE_ERROR(RuntimeError, std::runtime_error);
CREATE_ERROR(LogicError, std::logic_error);
CREATE_ERROR(ParameterError, RuntimeError);
CREATE_ERROR(ProcessingError, RuntimeError);
CREATE_ERROR(AssertionError, LogicError);

#undef CREATE_ERROR

#define THROW_ERROR(condition, exception, s) \
  do {                                       \
    if (condition) {                         \
      std::stringstream ss;                  \
      ss << s;                               \
      throw exception(ss.str());             \
    }                                        \
  } while (0);

class GetF0 {
public:
  GetF0();

  int derp();

private:

  static void check_f0_params(F0_params *par, double sample_freq);

};

int GetF0::derp()
{
  float *fdata;
  int done;
  long buff_size, actsize;
  double sf, output_starts, frame_rate;
  float *f0p, *vuvp, *rms_speech, *acpkp;
  int i, vecsize;
  long sdstep = 0;

  F0_params par;
  par.cand_thresh = 0.3;
  par.lag_weight = 0.3;
  par.freq_weight = 0.02;
  par.trans_cost = 0.005;
  par.trans_amp = 0.5;
  par.trans_spec = 0.5;
  par.voice_bias = 0.0;
  par.double_cost = 0.35;
  par.min_f0 = 50;
  par.max_f0 = 550;
  par.frame_step = 0.01;
  par.wind_dur = 0.0075;
  par.n_cands = 20;
  par.mean_f0 = 200;     /* unused */
  par.mean_f0_weight = 0.0;  /* unused */
  par.conditioning = 0;    /*unused */

#define SW_CUSTOMIZABLE(x) //TODO(sw)
  SW_CUSTOMIZABLE(debug_level);

  SW_CUSTOMIZABLE(par.frame_step);
  SW_CUSTOMIZABLE(par.cand_thresh);
  SW_CUSTOMIZABLE(par.lag_weight);
  SW_CUSTOMIZABLE(par.freq_weight);
  SW_CUSTOMIZABLE(par.trans_cost);
  SW_CUSTOMIZABLE(par.trans_amp);
  SW_CUSTOMIZABLE(par.trans_spec);
  SW_CUSTOMIZABLE(par.voice_bias);
  SW_CUSTOMIZABLE(par.double_cost);
  SW_CUSTOMIZABLE(par.min_f0);
  SW_CUSTOMIZABLE(par.max_f0);
  SW_CUSTOMIZABLE(par.wind_dur);
  SW_CUSTOMIZABLE(par.n_cands);
#undef SW_CUSTOMIZABLE

#define SW_FILE_PARAMS(x, y) //TODO (sw)
  SW_FILE_PARAMS(sf, "sampling frequency");
#undef SW_FILE_PARAMS

  check_f0_params(&par, sf);

  /*SW: Removed range restricter, but this may be interesting:
    if (total_samps < ((par->frame_step * 2.0) + par->wind_dur) * sf), then
      input range too small*/

  output_starts = par.wind_dur/2.0;
  /* Average delay due to loc. of ref. window center. */
  frame_rate = 1.0 / par.frame_step;

  /* Initialize variables in get_f0.c; allocate data structures;
   * determine length and overlap of input frames to read.
   */
  if (init_dp_f0(sf, &par, &buff_size, &sdstep)
      || buff_size > INT_MAX || sdstep > INT_MAX)
  {
    Fprintf(stderr, "problem in init_dp_f0().\n");
    exit(1);
  }

  /*SW: pass sdstep to caller so it knows how much we have to buffer. */

  if (debug_level)
    Fprintf(stderr, "init_dp_f0 returned buff_size %ld, sdstep %ld.\n",
	    buff_size, sdstep);

  fdata = static_cast<float*>(malloc(sizeof(float) * buff_size));

  actsize = sw_getf0_read(fdata, buff_size);

  while (1) {

    done = (actsize < buff_size);

    THROW_ERROR(dp_f0(fdata, (int)actsize, (int)sdstep, sf, &par, &f0p, &vuvp,
                      &rms_speech, &acpkp, &vecsize, done),
                ProcessingError, "problem in dp_f0().");

    sw_getf0_output(f0p, vuvp, rms_speech, acpkp, vecsize);

    if (done)
      break;

    actsize = sw_getf0_read_overlap(fdata, buff_size, sdstep);

  }

  exit(0);
}

/*
 * Some consistency checks on parameter values.
 */
void GetF0::check_f0_params(F0_params *par, double sample_freq)
{
  std::vector<std::string> errors;

  if ((par->cand_thresh < 0.01) || (par->cand_thresh > 0.99)) {
    errors.push_back("cand_thresh parameter must be between [0.01, 0.99].");
  }
  if ((par->wind_dur > .1) || (par->wind_dur < .0001)) {
    errors.push_back("wind_dur parameter must be between [0.0001, 0.1].");
  }
  if ((par->n_cands > 100) || (par->n_cands < 3)) {
    errors.push_back("n_cands parameter must be between [3,100].");
  }
  if ((par->max_f0 <= par->min_f0) || (par->max_f0 >= (sample_freq / 2.0)) ||
      (par->min_f0 < (sample_freq / 10000.0))) {
    errors.push_back(
        "min(max)_f0 parameter inconsistent with sampling frequency.");
  }
  double dstep =
      ((double)((int)(0.5 + (sample_freq * par->frame_step)))) / sample_freq;
  if (dstep != par->frame_step) {
    if (debug_level)
      Fprintf(stderr,
              "Frame step set to %f to exactly match signal sample rate.\n",
              dstep);
    par->frame_step = dstep;
  }
  if ((par->frame_step > 0.1) || (par->frame_step < (1.0 / sample_freq))) {
    errors.push_back(
        "frame_step parameter must be between [1/sampling rate, "
        "0.1].");
  }

  if (!errors.empty()) {
    std::stringstream ss;
    bool first = true;
    for (auto &error : errors) {
      if (!first) ss << " ";
      ss << error;
    }

    THROW_ERROR(true, ParameterError, ss.str());
  }
}

} // namespace GetF0
<|endoftext|>


<commit_before>/*******************************
Copyright (c) 2016-2021 Gr�goire Angerand

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********************************/

#include "vk.h"

namespace yave {

const char* vk_result_str(VkResult result) {
#define VK_RESULT_CASE(fmt) case fmt: return #fmt;
    switch(result) {
        VK_RESULT_CASE(VK_SUCCESS)
        VK_RESULT_CASE(VK_NOT_READY)
        VK_RESULT_CASE(VK_TIMEOUT)
        VK_RESULT_CASE(VK_EVENT_SET)
        VK_RESULT_CASE(VK_EVENT_RESET)
        VK_RESULT_CASE(VK_INCOMPLETE)
        VK_RESULT_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
        VK_RESULT_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
        VK_RESULT_CASE(VK_ERROR_INITIALIZATION_FAILED)
        VK_RESULT_CASE(VK_ERROR_DEVICE_LOST)
        VK_RESULT_CASE(VK_ERROR_MEMORY_MAP_FAILED)
        VK_RESULT_CASE(VK_ERROR_LAYER_NOT_PRESENT)
        VK_RESULT_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
        VK_RESULT_CASE(VK_ERROR_FEATURE_NOT_PRESENT)
        VK_RESULT_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
        VK_RESULT_CASE(VK_ERROR_TOO_MANY_OBJECTS)
        VK_RESULT_CASE(VK_ERROR_FORMAT_NOT_SUPPORTED)
        VK_RESULT_CASE(VK_ERROR_FRAGMENTED_POOL)
        VK_RESULT_CASE(VK_ERROR_UNKNOWN)
        VK_RESULT_CASE(VK_ERROR_OUT_OF_POOL_MEMORY)
        VK_RESULT_CASE(VK_ERROR_INVALID_EXTERNAL_HANDLE)
        VK_RESULT_CASE(VK_ERROR_FRAGMENTATION)
        VK_RESULT_CASE(VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS)
        VK_RESULT_CASE(VK_ERROR_SURFACE_LOST_KHR)
        VK_RESULT_CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR)
        VK_RESULT_CASE(VK_SUBOPTIMAL_KHR)
        VK_RESULT_CASE(VK_ERROR_OUT_OF_DATE_KHR)
        VK_RESULT_CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR)
        VK_RESULT_CASE(VK_ERROR_VALIDATION_FAILED_EXT)
        VK_RESULT_CASE(VK_ERROR_INVALID_SHADER_NV)
        VK_RESULT_CASE(VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT)
        VK_RESULT_CASE(VK_ERROR_NOT_PERMITTED_EXT)
        VK_RESULT_CASE(VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
        VK_RESULT_CASE(VK_THREAD_IDLE_KHR)
        VK_RESULT_CASE(VK_THREAD_DONE_KHR)
        VK_RESULT_CASE(VK_OPERATION_DEFERRED_KHR)
        VK_RESULT_CASE(VK_OPERATION_NOT_DEFERRED_KHR)
        VK_RESULT_CASE(VK_PIPELINE_COMPILE_REQUIRED_EXT)

        default:
            break;
    }
    return "Unknown error";
#undef VK_RESULT_CASE
}

}

<commit_msg>Fixed compilation error with some version of the SDK<commit_after>/*******************************
Copyright (c) 2016-2021 Grégoire Angerand

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********************************/

#include "vk.h"

namespace yave {

const char* vk_result_str(VkResult result) {
#define VK_RESULT_CASE(fmt) case fmt: return #fmt;
    switch(result) {
        VK_RESULT_CASE(VK_SUCCESS)
        VK_RESULT_CASE(VK_NOT_READY)
        VK_RESULT_CASE(VK_TIMEOUT)
        VK_RESULT_CASE(VK_EVENT_SET)
        VK_RESULT_CASE(VK_EVENT_RESET)
        VK_RESULT_CASE(VK_INCOMPLETE)
        VK_RESULT_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
        VK_RESULT_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
        VK_RESULT_CASE(VK_ERROR_INITIALIZATION_FAILED)
        VK_RESULT_CASE(VK_ERROR_DEVICE_LOST)
        VK_RESULT_CASE(VK_ERROR_MEMORY_MAP_FAILED)
        VK_RESULT_CASE(VK_ERROR_LAYER_NOT_PRESENT)
        VK_RESULT_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
        VK_RESULT_CASE(VK_ERROR_FEATURE_NOT_PRESENT)
        VK_RESULT_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
        VK_RESULT_CASE(VK_ERROR_TOO_MANY_OBJECTS)
        VK_RESULT_CASE(VK_ERROR_FORMAT_NOT_SUPPORTED)
        VK_RESULT_CASE(VK_ERROR_FRAGMENTED_POOL)
        VK_RESULT_CASE(VK_ERROR_UNKNOWN)

        default:
            break;
    }
    return "Unknown error";
#undef VK_RESULT_CASE
}

}

<|endoftext|>


<commit_before>#include <iostream>   // std::cout

#include "gtest/gtest.h"

#include "simplesorts.h"
#include "printarray.h"
#include "verifyarrays.h"

#define quiet

TEST(SimpleSorts, InsertionSort)
{
    const int N = 5;

    double *to_sort_data = new double[N];
    double *sorted_data  = new double[N];

    to_sort_data[0] = 21.3;
    to_sort_data[1] = 68.2;
    to_sort_data[2] = 1.3;
    to_sort_data[3] = 44.3;
    to_sort_data[4] = 10.3;

    memcpy(sorted_data, to_sort_data, N*sizeof(double));

    InsertionSort(sorted_data, N);

#ifndef quiet
    std::cout << "Arrays" << std::endl;
    std::cout << "Index Original     Sorted " << std::endl;
    for (int i = 0 ; i < N ; i++)
    {
        std::cout << i+1 << "/" << N << "   "
                    << to_sort_data[i] << "        "
                    << sorted_data[i] << std::endl;
    }
#endif // quiet

    for (int i = 1 ; i < N ; i++)
    {
        EXPECT_GE(sorted_data[i], sorted_data[i-1]);
    }

    for (int i = 0 ; i < N ; i++)
    {
        EXPECT_NE(sorted_data[i], 0);
    }
    ASSERT_TRUE(VerifyIfAllNotNaN(sorted_data, N));
    ASSERT_TRUE(VerifyIfOrdered(sorted_data, N));
    ASSERT_TRUE(VerifySortedUnique(to_sort_data, sorted_data, N));

    delete[] to_sort_data;
    delete[] sorted_data;
}

TEST(SimpleSorts, InsertionSortBestCase)
{
    // Best case should be O(N) when array already sorted
    const int N = 1000;

    int *to_sort_data = new int[N];
    int *sorted_data  = new int[N];

    for (int i = 0 ; i < N ; i++)
    {
        to_sort_data[i] = i;
    }

    memcpy(sorted_data, to_sort_data, N*sizeof(int));

    InsertionSort(sorted_data, N);

#ifndef quiet
    std::cout << "Arrays" << std::endl;
    std::cout << "Index Original     Sorted " << std::endl;
    for (int i = 0 ; i < N ; i++)
    {
        std::cout << i+1 << "/" << N << "   "
                    << to_sort_data[i] << "        "
                    << sorted_data[i] << std::endl;
    }
#endif // quiet

    for (int i = 1 ; i < N ; i++)
    {
        EXPECT_GE(sorted_data[i], sorted_data[i-1]);
    }

    ASSERT_TRUE(VerifyIfAllNotNaN(sorted_data, N));
    ASSERT_TRUE(VerifyIfOrdered(sorted_data, N));
    ASSERT_TRUE(VerifySortedUnique(to_sort_data, sorted_data, N));

    delete[] to_sort_data;
    delete[] sorted_data;

}
<commit_msg>Insertion: Best case _is_ O(N).<commit_after>#include <iostream>   // std::cout

#include "gtest/gtest.h"

#include "simplesorts.h"
#include "printarray.h"
#include "verifyarrays.h"

#define quiet

TEST(SimpleSorts, InsertionSort)
{
    const int N = 5;

    double *to_sort_data = new double[N];
    double *sorted_data  = new double[N];

    to_sort_data[0] = 21.3;
    to_sort_data[1] = 68.2;
    to_sort_data[2] = 1.3;
    to_sort_data[3] = 44.3;
    to_sort_data[4] = 10.3;

    memcpy(sorted_data, to_sort_data, N*sizeof(double));

    InsertionSort(sorted_data, N);

#ifndef quiet
    std::cout << "Arrays" << std::endl;
    std::cout << "Index Original     Sorted " << std::endl;
    for (int i = 0 ; i < N ; i++)
    {
        std::cout << i+1 << "/" << N << "   "
                    << to_sort_data[i] << "        "
                    << sorted_data[i] << std::endl;
    }
#endif // quiet

    for (int i = 1 ; i < N ; i++)
    {
        EXPECT_GE(sorted_data[i], sorted_data[i-1]);
    }

    for (int i = 0 ; i < N ; i++)
    {
        EXPECT_NE(sorted_data[i], 0);
    }
    ASSERT_TRUE(VerifyIfAllNotNaN(sorted_data, N));
    ASSERT_TRUE(VerifyIfOrdered(sorted_data, N));
    ASSERT_TRUE(VerifySortedUnique(to_sort_data, sorted_data, N));

    delete[] to_sort_data;
    delete[] sorted_data;
}

TEST(SimpleSorts, InsertionSortBestCase)
{
    // Best case is O(N) when array already sorted
    const int N = 1000;

    int *to_sort_data = new int[N];
    int *sorted_data  = new int[N];

    for (int i = 0 ; i < N ; i++)
    {
        to_sort_data[i] = i;
    }

    memcpy(sorted_data, to_sort_data, N*sizeof(int));

    InsertionSort(sorted_data, N);

#ifndef quiet
    std::cout << "Arrays" << std::endl;
    std::cout << "Index Original     Sorted " << std::endl;
    for (int i = 0 ; i < N ; i++)
    {
        std::cout << i+1 << "/" << N << "   "
                    << to_sort_data[i] << "        "
                    << sorted_data[i] << std::endl;
    }
#endif // quiet

    for (int i = 1 ; i < N ; i++)
    {
        EXPECT_GE(sorted_data[i], sorted_data[i-1]);
    }

    ASSERT_TRUE(VerifyIfAllNotNaN(sorted_data, N));
    ASSERT_TRUE(VerifyIfOrdered(sorted_data, N));
    ASSERT_TRUE(VerifySortedUnique(to_sort_data, sorted_data, N));

    delete[] to_sort_data;
    delete[] sorted_data;

}
<|endoftext|>


<commit_before><commit_msg>Liberalize the size limits in the PNG decoder to match the changes landed upstream to all the WebKit image decoders.  I'm not sure where this is used, hence no test.<commit_after><|endoftext|>


<commit_before>/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */

#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>

using namespace eosio;
using namespace testing;
using namespace chain;

BOOST_AUTO_TEST_SUITE(block_tests)

BOOST_AUTO_TEST_CASE(block_with_invalid_tx_test)
{
   tester main;

   // First we create a valid block with valid transaction
   main.create_account(N(newacc));
   auto b = main.produce_block();

   // Make a copy of the valid block and corrupt the transaction
   auto copy_b = std::make_shared<signed_block>(*b);
   auto signed_tx = copy_b->transactions.back().trx.get<packed_transaction>().get_signed_transaction();
   auto& act = signed_tx.actions.back();
   auto act_data = act.data_as<newaccount>();
   // Make the transaction invalid by having the new account name the same as the creator name
   act_data.name = act_data.creator;
   act.data = fc::raw::pack(act_data);
   // Re-sign the transaction
   signed_tx.signatures.clear();
   signed_tx.sign(main.get_private_key(N(eosio), "active"), main.control->get_chain_id());
   // Replace the valid transaction with the invalid transaction
   auto invalid_packed_tx = packed_transaction(signed_tx);
   copy_b->transactions.back().trx = invalid_packed_tx;

   // Re-sign the block
   auto header_bmroot = digest_type::hash( std::make_pair( copy_b->digest(), main.control->head_block_state()->blockroot_merkle.get_root() ) );
   auto sig_digest = digest_type::hash( std::make_pair(header_bmroot, main.control->head_block_state()->pending_schedule_hash) );
   copy_b->producer_signature = main.get_private_key(N(eosio), "active").sign(sig_digest);

   // Push block with invalid transaction to other chain
   tester validator;
   validator.control->abort_block();
   BOOST_REQUIRE_EXCEPTION(validator.control->push_block( copy_b ), fc::exception ,
   [] (const fc::exception &e)->bool {
      return e.code() == action_validate_exception::code_value ;
   }) ;

}

BOOST_AUTO_TEST_SUITE_END()
<commit_msg>fixed tests in light of new exception<commit_after>/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */

#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>

using namespace eosio;
using namespace testing;
using namespace chain;

BOOST_AUTO_TEST_SUITE(block_tests)

BOOST_AUTO_TEST_CASE(block_with_invalid_tx_test)
{
   tester main;

   // First we create a valid block with valid transaction
   main.create_account(N(newacc));
   auto b = main.produce_block();

   // Make a copy of the valid block and corrupt the transaction
   auto copy_b = std::make_shared<signed_block>(*b);
   auto signed_tx = copy_b->transactions.back().trx.get<packed_transaction>().get_signed_transaction();
   auto& act = signed_tx.actions.back();
   auto act_data = act.data_as<newaccount>();
   // Make the transaction invalid by having the new account name the same as the creator name
   act_data.name = act_data.creator;
   act.data = fc::raw::pack(act_data);
   // Re-sign the transaction
   signed_tx.signatures.clear();
   signed_tx.sign(main.get_private_key(N(eosio), "active"), main.control->get_chain_id());
   // Replace the valid transaction with the invalid transaction
   auto invalid_packed_tx = packed_transaction(signed_tx);
   copy_b->transactions.back().trx = invalid_packed_tx;

   // Re-sign the block
   auto header_bmroot = digest_type::hash( std::make_pair( copy_b->digest(), main.control->head_block_state()->blockroot_merkle.get_root() ) );
   auto sig_digest = digest_type::hash( std::make_pair(header_bmroot, main.control->head_block_state()->pending_schedule_hash) );
   copy_b->producer_signature = main.get_private_key(N(eosio), "active").sign(sig_digest);

   // Push block with invalid transaction to other chain
   tester validator;
   validator.control->abort_block();
   BOOST_REQUIRE_EXCEPTION(validator.control->push_block( copy_b ), fc::exception ,
   [] (const fc::exception &e)->bool {
      return e.code() == account_name_exists_exception::code_value ;
   }) ;

}

BOOST_AUTO_TEST_SUITE_END()
<|endoftext|>


<commit_before>/* -*- Mode: C++; indent-tabs-mode: nil -*- */
#include <Python.h>

#include "callback_stream.h"
#include "exception.h"
#include "pyfile_source.h"
#include "pystream.h"
#include "seq_stream.h"
#include "text_stream.h"

#include <fstream>
#include <memory>

#include <schwa/_base.h>
#include <schwa/tokenizer.h>

#include <boost/exception/exception.hpp>

namespace io = schwa::io;
namespace tok = schwa::tokenizer;

static PyObject *TokenError = 0;

static PyObject *token_ERROR_SKIP = 0;
static PyObject *token_ERROR_CALL = 0;
static PyObject *token_ERROR_THROW = 0;

typedef struct {
  PyObject_HEAD
  tok::Tokenizer tokenizer;
} PyTokenizer;

inline static PyObject *
add_to_module(PyObject *mod, const char *name, PyObject *obj) {
  Py_INCREF(obj);
  PyModule_AddObject(mod, name, obj);
  return obj;
}

inline static PyObject *
add_long_to_module(PyObject *mod, const char *name, long value) {
  return add_to_module(mod, name, PyInt_FromLong(value));
}

static tok::PyStream *
pyobj2dest(PyObject *dest, bool normalise) {
  if ((void *)dest == (void *)&PyString_Type)
    return new tok::PyBytesStream(normalise);
  else if ((void *)dest == (void *)&PyUnicode_Type)
    return new tok::PyUnicodeStream(normalise);
  else if ((void *)dest == (void *)&PyList_Type)
    return new tok::PyListStream();
  else if ((void *)dest == (void *)&PyTuple_Type)
    return new tok::PyTupleStream();
  else if (PyCallable_Check(dest))
    return new tok::PyCallFuncStream(dest);
  else
    return new tok::PyCallObjectStream(dest);
}

PyObject *
PyTokenizer_tokenize(PyTokenizer *self, PyObject *args, PyObject *kwargs) {
  tok::Tokenizer &tok = self->tokenizer;

  PyObject *pysrc = 0;
  PyObject *pydest = (PyObject *)&PyString_Type;
  const char *filename = 0;

  long buffer_size = tok::BUFFER_SIZE;
  int errors = tok::ERROR_SKIP;
  int normalise = 1;
  int use_mmap = 0;

  static const char *kwlist[] = {"source", "dest", "filename", "buffer_size", "errors", "normalise", "mmap", 0};
  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOsliii:tokenize", (char **)kwlist, &pysrc, &pydest, &filename, &buffer_size, &errors, &normalise, &use_mmap))
    return 0;
  if (!pysrc && !filename)
    return PyErr_Format(PyExc_TypeError, "tokenize() requires either a source or filename argument");
  if (pysrc && PyUnicode_Check(pysrc))
    return PyErr_Format(PyExc_TypeError, "tokenize() does not accept unicode objects, use unicode.encode('utf-8')");
  if (buffer_size <= 0)
    return PyErr_Format(PyExc_ValueError, "tokenize() buffer_size must be positive, %ld given", buffer_size);
  if (errors < tok::ERROR_SKIP || errors > tok::ERROR_THROW)
    return PyErr_Format(PyExc_ValueError, "tokenize() unknown bad byte error handler, %d given", errors);

  try {
    std::unique_ptr<tok::PyStream> dest(pyobj2dest(pydest, normalise));
    if (filename) {
      if (use_mmap) {
        try {
          tok.tokenize_mmap(*dest, filename, errors);
        }
        catch (boost::exception &e) {
          return PyErr_Format(PyExc_IOError, "tokenize() could not open file '%s' for reading with mmap", filename);
        }
      }
      else {
        std::ifstream stream(filename);
        if (!stream)
          return PyErr_Format(PyExc_IOError,"tokenize() could not open file '%s' for reading", filename);
        tok.tokenize_stream(*dest, stream, static_cast<tok::offset_type>(buffer_size), errors);
      }
    }
    else if (PyObject_CheckBuffer(pysrc)) {
      Py_buffer buffer;
      if (PyObject_GetBuffer(pysrc, &buffer, PyBUF_SIMPLE) != 0)
        return PyErr_Format(PyExc_ValueError, "tokenize() only supports simple buffer objects");
      tok.tokenize(*dest, (char *)buffer.buf, static_cast<tok::offset_type>(buffer.len), errors);
      PyBuffer_Release(&buffer);
    }
    else if (PyFile_Check(pysrc)) {
      io::PyFileSource src(pysrc);
      tok.tokenize(*dest, src, buffer_size, errors);
    }
    return dest->get();
  }
  catch (tok::TokenError &e) {
    PyErr_SetString(TokenError, e.what());
    return 0;
  }
  catch (tok::PyRaise &e) {
    return 0;
  }
}

static PyMethodDef PyTokenizer_methods[] = {
  {"tokenize", (PyCFunction)PyTokenizer_tokenize, METH_VARARGS | METH_KEYWORDS,
"Identifies paragraph, sentence and token boundaries in (English) text or HTML,\n\
using a rule-based lexer.\n\
\n\
Arguments:\n\
\n\
  source: a string in UTF-8 or a file to tokenize\n\
\n\
  dest: the output method, one of the following Python types:\n\
    - str (default): outputs a UTF-8 string with '\\n\\n', '\\n' and ' ' as\n\
      paragraph, sentence and token delimiters respectively\n\
    - unicode: like str, but with UTF-8 decoded\n\
    - list: outputs a list of paragraphs, each containing a list of sentences,\n\
      each containing a list of (offset, raw_token, [normalised_token]) tuples\n\
    or:\n\
    - any other callable: makes callbacks with arguments\n\
      (emit_type, offset, raw, norm)\n\
    - any other object: calls methods named according to emit_type when present\n\
\n\
    Offset is a byte offset into the input string.\n\
    Emit types are:\n\
      begin_{document,paragraph,sentence,heading,list,item},\n\
      end_{document,paragraph,sentence,heading,list,item},\n\
      token and error\n\
\n\
  filename: the path to a file to use instead of source\n\
\n\
  buffer_size: \n\
\n\
  errors: a method for dealing with bad byte sequences, may be one of:\n\
    - ERROR_SKIP (default): ignores errors\n\
    - ERROR_CALL: emits ('error', offset, bytes)\n\
    - ERROR_THROW: throws a TokenError\n\
\n\
  normalise: a boolean (default True) indicating whether to perform\n\
  normalisation on the output (such as directed quotes, all dashes as --, and *\n\
  to indicate list items). Only applies when dest is str or unicode.\n\
\n\
  mmap: a boolean (default False) indicating whether to process the file\n\
  memory-mapped, when the filename argument is used."
  },
  {0}  /* Sentinel */
};

static PyGetSetDef PyTokenizer_getsets[] = {
  {0}  /* Sentinel */
};

static PyTypeObject PyTokenizerType = {
  PyObject_HEAD_INIT(NULL)
  0,                         /* ob_size */
  "tokens.Tokenizer",        /* tp_name */
  sizeof(PyTokenizer),       /* tp_basicsize */
  0,                         /* tp_itemsize */
  0,                         /* tp_dealloc */
  0,                         /* tp_print */
  0,                         /* tp_getattr */
  0,                         /* tp_setattr */
  0,                         /* tp_compare */
  0,                         /* tp_repr */
  0,                         /* tp_as_number */
  0,                         /* tp_as_sequence */
  0,                         /* tp_as_mapping */
  0,                         /* tp_hash  */
  0,                         /* tp_call */
  0,                         /* tp_str */
  0,                         /* tp_getattro */
  0,                         /* tp_setattro */
  0,                         /* tp_as_buffer */
  Py_TPFLAGS_DEFAULT,        /* tp_flags */
  "Tokenizer object",        /* tp_doc */
  0,                         /* tp_traverse */
  0,                         /* tp_clear */
  0,                         /* tp_richcompare */
  0,                         /* tp_weaklistoffset */
  0,                         /* tp_iter */
  0,                         /* tp_iternext */
  PyTokenizer_methods,       /* tp_methods */
  0,                         /* tp_members */
  PyTokenizer_getsets,       /* tp_getset */
  0,                         /* tp_base */
  0,                         /* tp_dict */
  0,                         /* tp_descr_get */
  0,                         /* tp_descr_set */
  0,                         /* tp_dictoffset */
  (initproc)0,               /* tp_init */
  0,                         /* tp_alloc */
  PyType_GenericNew,         /* tp_new */
};

static PyMethodDef token_functions[] = {
  {0, 0, 0, 0}
};

PyMODINIT_FUNC
inittokenizer(void) {
  PyObject *m = Py_InitModule3("tokenizer", token_functions, "Schwa Lab tokenizer module");
  if (m == 0)
    return;
  if (PyType_Ready(&PyTokenizerType) < 0)
    return;

  add_to_module(m, "Tokenizer", (PyObject *)&PyTokenizerType);

  TokenError = PyErr_NewException((char *)"tokenizer.TokenError", 0, 0);
  add_to_module(m, "TokenError", TokenError);

  token_ERROR_SKIP = add_long_to_module(m, "ERROR_SKIP", tok::ERROR_SKIP);
  token_ERROR_CALL = add_long_to_module(m, "ERROR_CALL", tok::ERROR_CALL);
  token_ERROR_THROW = add_long_to_module(m, "ERROR_THROW", tok::ERROR_THROW);
}
<commit_msg>* Removed last reference to Boost in the tokenizer Python API<commit_after>/* -*- Mode: C++; indent-tabs-mode: nil -*- */
#include <Python.h>

#include "callback_stream.h"
#include "exception.h"
#include "pyfile_source.h"
#include "pystream.h"
#include "seq_stream.h"
#include "text_stream.h"

#include <fstream>
#include <memory>

#include <schwa/_base.h>
#include <schwa/exception.h>
#include <schwa/tokenizer.h>

namespace io = schwa::io;
namespace tok = schwa::tokenizer;

static PyObject *TokenError = 0;

static PyObject *token_ERROR_SKIP = 0;
static PyObject *token_ERROR_CALL = 0;
static PyObject *token_ERROR_THROW = 0;

typedef struct {
  PyObject_HEAD
  tok::Tokenizer tokenizer;
} PyTokenizer;

inline static PyObject *
add_to_module(PyObject *mod, const char *name, PyObject *obj) {
  Py_INCREF(obj);
  PyModule_AddObject(mod, name, obj);
  return obj;
}

inline static PyObject *
add_long_to_module(PyObject *mod, const char *name, long value) {
  return add_to_module(mod, name, PyInt_FromLong(value));
}

static tok::PyStream *
pyobj2dest(PyObject *dest, bool normalise) {
  if ((void *)dest == (void *)&PyString_Type)
    return new tok::PyBytesStream(normalise);
  else if ((void *)dest == (void *)&PyUnicode_Type)
    return new tok::PyUnicodeStream(normalise);
  else if ((void *)dest == (void *)&PyList_Type)
    return new tok::PyListStream();
  else if ((void *)dest == (void *)&PyTuple_Type)
    return new tok::PyTupleStream();
  else if (PyCallable_Check(dest))
    return new tok::PyCallFuncStream(dest);
  else
    return new tok::PyCallObjectStream(dest);
}

PyObject *
PyTokenizer_tokenize(PyTokenizer *self, PyObject *args, PyObject *kwargs) {
  tok::Tokenizer &tok = self->tokenizer;

  PyObject *pysrc = 0;
  PyObject *pydest = (PyObject *)&PyString_Type;
  const char *filename = 0;

  long buffer_size = tok::BUFFER_SIZE;
  int errors = tok::ERROR_SKIP;
  int normalise = 1;
  int use_mmap = 0;

  static const char *kwlist[] = {"source", "dest", "filename", "buffer_size", "errors", "normalise", "mmap", 0};
  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOsliii:tokenize", (char **)kwlist, &pysrc, &pydest, &filename, &buffer_size, &errors, &normalise, &use_mmap))
    return 0;
  if (!pysrc && !filename)
    return PyErr_Format(PyExc_TypeError, "tokenize() requires either a source or filename argument");
  if (pysrc && PyUnicode_Check(pysrc))
    return PyErr_Format(PyExc_TypeError, "tokenize() does not accept unicode objects, use unicode.encode('utf-8')");
  if (buffer_size <= 0)
    return PyErr_Format(PyExc_ValueError, "tokenize() buffer_size must be positive, %ld given", buffer_size);
  if (errors < tok::ERROR_SKIP || errors > tok::ERROR_THROW)
    return PyErr_Format(PyExc_ValueError, "tokenize() unknown bad byte error handler, %d given", errors);

  try {
    std::unique_ptr<tok::PyStream> dest(pyobj2dest(pydest, normalise));
    if (filename) {
      if (use_mmap) {
        tok.tokenize_mmap(*dest, filename, errors);
      }
      else {
        std::ifstream stream(filename);
        if (!stream)
          return PyErr_Format(PyExc_IOError,"tokenize() could not open file '%s' for reading", filename);
        tok.tokenize_stream(*dest, stream, static_cast<tok::offset_type>(buffer_size), errors);
      }
    }
    else if (PyObject_CheckBuffer(pysrc)) {
      Py_buffer buffer;
      if (PyObject_GetBuffer(pysrc, &buffer, PyBUF_SIMPLE) != 0)
        return PyErr_Format(PyExc_ValueError, "tokenize() only supports simple buffer objects");
      tok.tokenize(*dest, (char *)buffer.buf, static_cast<tok::offset_type>(buffer.len), errors);
      PyBuffer_Release(&buffer);
    }
    else if (PyFile_Check(pysrc)) {
      io::PyFileSource src(pysrc);
      tok.tokenize(*dest, src, buffer_size, errors);
    }
    return dest->get();
  }
  catch (tok::TokenError &e) {
    PyErr_SetString(TokenError, e.what());
    return 0;
  }
  catch (tok::PyRaise &e) {
    return 0;
  }
  catch (schwa::IOException &e) {
    PyErr_SetString(PyExc_IOError, e.what());
    return 0;
  }
}

static PyMethodDef PyTokenizer_methods[] = {
  {"tokenize", (PyCFunction)PyTokenizer_tokenize, METH_VARARGS | METH_KEYWORDS,
"Identifies paragraph, sentence and token boundaries in (English) text or HTML,\n\
using a rule-based lexer.\n\
\n\
Arguments:\n\
\n\
  source: a string in UTF-8 or a file to tokenize\n\
\n\
  dest: the output method, one of the following Python types:\n\
    - str (default): outputs a UTF-8 string with '\\n\\n', '\\n' and ' ' as\n\
      paragraph, sentence and token delimiters respectively\n\
    - unicode: like str, but with UTF-8 decoded\n\
    - list: outputs a list of paragraphs, each containing a list of sentences,\n\
      each containing a list of (offset, raw_token, [normalised_token]) tuples\n\
    or:\n\
    - any other callable: makes callbacks with arguments\n\
      (emit_type, offset, raw, norm)\n\
    - any other object: calls methods named according to emit_type when present\n\
\n\
    Offset is a byte offset into the input string.\n\
    Emit types are:\n\
      begin_{document,paragraph,sentence,heading,list,item},\n\
      end_{document,paragraph,sentence,heading,list,item},\n\
      token and error\n\
\n\
  filename: the path to a file to use instead of source\n\
\n\
  buffer_size: \n\
\n\
  errors: a method for dealing with bad byte sequences, may be one of:\n\
    - ERROR_SKIP (default): ignores errors\n\
    - ERROR_CALL: emits ('error', offset, bytes)\n\
    - ERROR_THROW: throws a TokenError\n\
\n\
  normalise: a boolean (default True) indicating whether to perform\n\
  normalisation on the output (such as directed quotes, all dashes as --, and *\n\
  to indicate list items). Only applies when dest is str or unicode.\n\
\n\
  mmap: a boolean (default False) indicating whether to process the file\n\
  memory-mapped, when the filename argument is used."
  },
  {0}  /* Sentinel */
};

static PyGetSetDef PyTokenizer_getsets[] = {
  {0}  /* Sentinel */
};

static PyTypeObject PyTokenizerType = {
  PyObject_HEAD_INIT(NULL)
  0,                         /* ob_size */
  "tokens.Tokenizer",        /* tp_name */
  sizeof(PyTokenizer),       /* tp_basicsize */
  0,                         /* tp_itemsize */
  0,                         /* tp_dealloc */
  0,                         /* tp_print */
  0,                         /* tp_getattr */
  0,                         /* tp_setattr */
  0,                         /* tp_compare */
  0,                         /* tp_repr */
  0,                         /* tp_as_number */
  0,                         /* tp_as_sequence */
  0,                         /* tp_as_mapping */
  0,                         /* tp_hash  */
  0,                         /* tp_call */
  0,                         /* tp_str */
  0,                         /* tp_getattro */
  0,                         /* tp_setattro */
  0,                         /* tp_as_buffer */
  Py_TPFLAGS_DEFAULT,        /* tp_flags */
  "Tokenizer object",        /* tp_doc */
  0,                         /* tp_traverse */
  0,                         /* tp_clear */
  0,                         /* tp_richcompare */
  0,                         /* tp_weaklistoffset */
  0,                         /* tp_iter */
  0,                         /* tp_iternext */
  PyTokenizer_methods,       /* tp_methods */
  0,                         /* tp_members */
  PyTokenizer_getsets,       /* tp_getset */
  0,                         /* tp_base */
  0,                         /* tp_dict */
  0,                         /* tp_descr_get */
  0,                         /* tp_descr_set */
  0,                         /* tp_dictoffset */
  (initproc)0,               /* tp_init */
  0,                         /* tp_alloc */
  PyType_GenericNew,         /* tp_new */
};

static PyMethodDef token_functions[] = {
  {0, 0, 0, 0}
};

PyMODINIT_FUNC
inittokenizer(void) {
  PyObject *m = Py_InitModule3("tokenizer", token_functions, "Schwa Lab tokenizer module");
  if (m == 0)
    return;
  if (PyType_Ready(&PyTokenizerType) < 0)
    return;

  add_to_module(m, "Tokenizer", (PyObject *)&PyTokenizerType);

  TokenError = PyErr_NewException((char *)"tokenizer.TokenError", 0, 0);
  add_to_module(m, "TokenError", TokenError);

  token_ERROR_SKIP = add_long_to_module(m, "ERROR_SKIP", tok::ERROR_SKIP);
  token_ERROR_CALL = add_long_to_module(m, "ERROR_CALL", tok::ERROR_CALL);
  token_ERROR_THROW = add_long_to_module(m, "ERROR_THROW", tok::ERROR_THROW);
}
<|endoftext|>


<commit_before>//
//  main.cpp
//  fontgen - x2d
//
//  Created by Alex Kremer on 03/11/2012.
//  Copyright (c) 2012 godexsoft. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>

#include "ft2build.h"
#include FT_FREETYPE_H

#include "glm.hpp"

namespace po = boost::program_options;
using namespace std;

class generator
{
public:
    generator(const string& fnt, const vector<string>& txt_lst, int sz, int pd)
    : font_file_(fnt)
    , txt_files_(txt_lst)
    , char_size_(sz, sz)
    , padding_(pd * 2.0f)
    {
    }

    ~generator()
    {
        if(font_face_)
        {
            FT_Done_Face(font_face_);
        }

        if(font_library_)
        {
            FT_Done_FreeType(font_library_);
        }
    }

    int run();

private:

    void load_font();
    void process_character(int c);
    void parse_text(const string& file);

    map<int, bool>  gen_;
    string          font_file_;
    vector<string>  txt_files_;

    string          font_name_;
    glm::vec2       char_size_;
    glm::vec2       char_spacing_;
    float           font_scale_;
    float           padding_;

    FT_Library      font_library_;
    FT_Face         font_face_;
};

int generator::run()
{
    cout << "[+] Running generator...\n";

    cout << "    Font size:          " << char_size_.y << "\n";
    cout << "    Padding:            " << padding_ << "\n";
    cout << "    Font file:          " << font_file_ << "\n";

    for(vector<string>::iterator it = txt_files_.begin(); it != txt_files_.end(); ++it)
    {
        cout << "    Text file to parse: " << *it << "\n";
    }

    // open font
    load_font();

    // parse all text files to generate a map with glyphs
    for(vector<string>::iterator it = txt_files_.begin(); it != txt_files_.end(); ++it)
    {
        parse_text(*it);
    }

    return 0;
}

void generator::load_font()
{
    if(!FT_Init_FreeType( &font_library_ ))
    {
        if(!FT_New_Face(font_library_, font_file_.c_str(), 0, &font_face_))
        {
            if(!FT_Select_Charmap(font_face_, ft_encoding_unicode))
            {
                char_spacing_.x = 2.0f * glm::ceil(char_size_.x * (1.0f / 32.0f));
                char_spacing_.y = 2.0f * glm::ceil(char_size_.y * (1.0f / 32.0f));

                cout << "[?] Spacing: x=" << char_spacing_.x << " / y=" << char_spacing_.y << "\n";

                font_scale_ = char_size_.y / (font_face_->bbox.yMax - font_face_->bbox.yMin);

                cout << "[?] Setting pixel sizes: " << char_size_.x - 2 << " x " << (FT_UInt)char_size_.y - 2 << "\n";

                FT_Set_Pixel_Sizes(font_face_, (FT_UInt)char_size_.x - 2, (FT_UInt)char_size_.y - 2);
                cout << "[+] Font loaded.\n";
            }
        }
    }
}

void generator::process_character(int c)
{
    if(!gen_.count(c))
    {
        // TODO: process
        gen_.insert(make_pair(c, true));
    }
}

void generator::parse_text(const string& file)
{
    cout << "[+] Parsing text file: " << file << "...\n";
    ifstream ifs(file.c_str(), ifstream::in);

    if(ifs.is_open())
    {
        while(!ifs.eof())
        {
            process_character(ifs.get());
        }

        cout << "[+] Done.\n";
    }
    else
    {
        cout << "[-] Could'n open text file " << file << ".\n";
    }
}

int main(int argc, const char * argv[])
{
    po::options_description desc("Allowed options");
    desc.add_options()
    ("help,h", "produce help message")
    ("output,o", po::value<std::string>(), "output name")
    ("size,s", po::value<int>()->default_value(16), "height of characters")
    ("padding,p", po::value<int>()->default_value(2), "character padding")
    ("font,f", po::value<string>(), "input font file (.ttf)")
    ("text,t", po::value<vector<string> >()->composing(), "input text file")
    ("monospace,m", po::value<bool>(), "monospaced font")
    ("advance,a", po::value<int>(), "glyph advance value for non-monospace fonts")
    ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    string fnt;
    vector<string> txt_files;
    int size;
    int padding;

    if (vm.count("help"))
    {
        cout << desc << "\n";
        return 1;
    }

    // check font file
    if (vm.count("font"))
    {
        fnt = vm["font"].as<string>();
    }
    else
    {
        cout << "[-] Input font is not passed.\n";
        return 127;
    }

    // set size and padding
    size = vm["size"].as<int>();
    padding = vm["padding"].as<int>();

    // check input text files
    if (vm.count("text"))
    {
        txt_files = vm["text"].as<vector<string> >();
    }
    else
    {
        cout << "[-] Text files not specified. abort.\n";
        return 127;
    }

    // create a generator
    generator gen(fnt, txt_files, size, padding);
    return gen.run();
}
<commit_msg>parsing of ascii characters into gen map<commit_after>//
//  main.cpp
//  fontgen - x2d
//
//  Created by Alex Kremer on 03/11/2012.
//  Copyright (c) 2012 godexsoft. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>

#include "ft2build.h"
#include FT_FREETYPE_H

#include "glm.hpp"

#define CHAR_CR ((uint32_t)'\r')
#define CHAR_LF ((uint32_t)'\n')

namespace po = boost::program_options;
using namespace std;

struct glyph_info
{
    uint32_t index;
    uint32_t code_point;

    glyph_info(uint32_t i, uint32_t cp)
    : index(i)
    , code_point(cp)
    {
    }
};

class generator
{
public:
    generator(const string& fnt, const vector<string>& txt_lst, int sz, int pd)
    : font_file_(fnt)
    , txt_files_(txt_lst)
    , char_size_(sz, sz)
    , padding_(pd * 2.0f)
    {
    }

    ~generator()
    {
        if(font_face_)
        {
            FT_Done_Face(font_face_);
        }

        if(font_library_)
        {
            FT_Done_FreeType(font_library_);
        }
    }

    int run();

private:

    void load_font();
    void process_character(int c);
    void parse_text(const string& file);

    map<int, glyph_info>    gen_;
    string                  font_file_;
    vector<string>          txt_files_;

    string          font_name_;
    glm::vec2       char_size_;
    glm::vec2       char_spacing_;
    float           font_scale_;
    float           padding_;

    FT_Library      font_library_;
    FT_Face         font_face_;
};

int generator::run()
{
    cout << "[+] Running generator...\n";

    cout << "    Font size:          " << char_size_.y << "\n";
    cout << "    Padding:            " << padding_ << "\n";
    cout << "    Font file:          " << font_file_ << "\n";

    for(vector<string>::iterator it = txt_files_.begin(); it != txt_files_.end(); ++it)
    {
        cout << "    Text file to parse: " << *it << "\n";
    }

    // open font
    load_font();

    // parse all text files to generate a map with glyphs
    for(vector<string>::iterator it = txt_files_.begin(); it != txt_files_.end(); ++it)
    {
        parse_text(*it);
    }

    return 0;
}

void generator::load_font()
{
    if(!FT_Init_FreeType( &font_library_ ))
    {
        if(!FT_New_Face(font_library_, font_file_.c_str(), 0, &font_face_))
        {
            if(!FT_Select_Charmap(font_face_, ft_encoding_unicode))
            {
                char_spacing_.x = 2.0f * glm::ceil(char_size_.x * (1.0f / 32.0f));
                char_spacing_.y = 2.0f * glm::ceil(char_size_.y * (1.0f / 32.0f));

                cout << "[?] Spacing: x=" << char_spacing_.x << " / y=" << char_spacing_.y << "\n";

                font_scale_ = char_size_.y / (font_face_->bbox.yMax - font_face_->bbox.yMin);

                cout << "[?] Setting pixel sizes: " << char_size_.x - 2 << " x " << (FT_UInt)char_size_.y - 2 << "\n";

                FT_Set_Pixel_Sizes(font_face_, (FT_UInt)char_size_.x - 2, (FT_UInt)char_size_.y - 2);
                cout << "[+] Font loaded.\n";
            }
        }
    }
}

void generator::process_character(int c)
{
    uint32_t char_code_point = c;

    if(!gen_.count(char_code_point))
    {
        if(char_code_point != CHAR_CR && char_code_point != CHAR_LF)
        {
            uint32_t glyph_index = FT_Get_Char_Index(font_face_, char_code_point);
            if(glyph_index)
            {
                cout << "[debug] Found glyph index " << glyph_index << " for character '" << (char)c << "'\n";

                // add to map
                gen_.insert( make_pair( char_code_point, glyph_info( glyph_index, char_code_point ) ) );
            }
        }
    }
}

void generator::parse_text(const string& file)
{
    cout << "[+] Parsing text file: " << file << "...\n";
    ifstream ifs(file.c_str(), ifstream::in);

    if(ifs.is_open())
    {
        while(!ifs.eof())
        {
            process_character(ifs.get());
        }

        cout << "[+] Done. Added " << gen_.size() << " characters.\n";
    }
    else
    {
        cout << "[-] Could'n open text file " << file << ".\n";
    }
}

int main(int argc, const char * argv[])
{
    po::options_description desc("Allowed options");
    desc.add_options()
    ("help,h", "produce help message")
    ("output,o", po::value<std::string>(), "output name")
    ("size,s", po::value<int>()->default_value(16), "height of characters")
    ("padding,p", po::value<int>()->default_value(2), "character padding")
    ("font,f", po::value<string>(), "input font file (.ttf)")
    ("text,t", po::value<vector<string> >()->composing(), "input text file")
    ("monospace,m", po::value<bool>(), "monospaced font")
    ("advance,a", po::value<int>(), "glyph advance value for non-monospace fonts")
    ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    string fnt;
    vector<string> txt_files;
    int size;
    int padding;

    if (vm.count("help"))
    {
        cout << desc << "\n";
        return 1;
    }

    // check font file
    if (vm.count("font"))
    {
        fnt = vm["font"].as<string>();
    }
    else
    {
        cout << "[-] Input font is not passed.\n";
        return 127;
    }

    // set size and padding
    size = vm["size"].as<int>();
    padding = vm["padding"].as<int>();

    // check input text files
    if (vm.count("text"))
    {
        txt_files = vm["text"].as<vector<string> >();
    }
    else
    {
        cout << "[-] Text files not specified. abort.\n";
        return 127;
    }

    // create a generator
    generator gen(fnt, txt_files, size, padding);
    return gen.run();
}
<|endoftext|>