freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [ft] Standalone rasterizer


From: Erik Möller
Subject: RE: [ft] Standalone rasterizer
Date: Wed, 22 Jul 2009 12:01:12 +0200

Ah well.. I might give it a shot a rainy day then. I get that it's prob not 
used much standalone but still would be very handy to have...for me :)

Is there an "official" way of including the memory definitions needed to 
compile standalone?

-Erik

-----Original Message-----
From: Werner LEMBERG [mailto:address@hidden 
Sent: den 22 juli 2009 11:41
To: address@hidden
Cc: address@hidden
Subject: Re: [ft] Standalone rasterizer


> I’m trying to use your rasterizer standalone to render out an outline 
> but I keep getting squashed results back and I can’t seem to get my 
> head around why.
> 
> By visual inspection it seems to be 1/8 the width it should be, see 
> the attached image (it’s only 11k so hope you let it slip).

Here's my version which seems to work.

> and support for the direct rendering flag is at the top of my list to 
> Santa this year ;)

Uhmm, don't expect too much here.  But maybe you could contribute this?  :-)


    Werner


======================================================================


#include "ftraster.c"
#include "malloc.h"
#include <fstream>

//
// Define an acorn shape to test with
//
struct Vec2
{
  Vec2(float a, float b) : x(a), y(b) { }

  float x, y;
};

static Vec2 k_shape[] =
{ Vec2(-3, -18), Vec2(0, -12), Vec2(6, -10), Vec2(12, -6), Vec2(12, -4),
  Vec2(11, -4), Vec2(10, -5), Vec2(10, 1), Vec2(9, 6), Vec2(7, 10),
  Vec2(5, 12), Vec2(4, 15),Vec2(3, 14), Vec2(1, 13), Vec2(-1, 13),
  Vec2(-5, 11), Vec2(-8, 8), Vec2(-11, 2), Vec2(-11, -2), Vec2(-14, 0),
  Vec2(-14, -2), Vec2(-11, -7), Vec2(-9, -9), Vec2(-8, -9), Vec2(-5, -12),
  Vec2(-5, -14), Vec2(-7, -15), Vec2(-8, -14), Vec2(-9, -15), Vec2(-9, -17),
  Vec2(-7, -17), Vec2(-6, -18)
};

//
// Some freetype definitions ripped to get this to work. Maybe there's some // 
way to include these instead?
//

typedef struct FT_MemoryRec_* FT_Memory; typedef void* 
(*FT_Alloc_Func)(FT_Memory memory,
                               long size); typedef void 
(*FT_Free_Func)(FT_Memory memory,
                             void* block); typedef void* 
(*FT_Realloc_Func)(FT_Memory memory,
                                 long cur_size,
                                 long new_size,
                                 void* block); struct FT_MemoryRec_ {
  void* user;
  FT_Alloc_Func alloc;
  FT_Free_Func free;
  FT_Realloc_Func realloc;
};

void* MY_Alloc_Func(FT_Memory memory,
                    long size)
{
  return malloc(size);
}

void MY_Free_Func(FT_Memory memory,
                  void *block)
{
  free(block);
}

void* MY_Realloc_Func(FT_Memory memory,
                      long cur_size,
                      long new_size,
                      void* block)
{
  return realloc(block, new_size);
}


FT_Memory mem;


//
// Render a shape and dump it out as a raw image // int main() {
  // Set up the memory management to use malloc and free
  mem = new FT_MemoryRec_;
  mem->alloc = MY_Alloc_Func;
  mem->free = MY_Free_Func;
  mem->realloc = MY_Realloc_Func;

  // Build an outline manually
  FT_Outline_ outline;
  outline.n_contours = 1;
  outline.n_points = sizeof (k_shape) / sizeof (Vec2);
  outline.points = new FT_Vector[outline.n_points];
  for (unsigned int i = 0; i < sizeof (k_shape) / sizeof (Vec2); ++i)
  {
    FT_Vector v;
    // offset it to fit in the image and scale it up 10 times
    v.x = (20 + k_shape[i].x) * 10 * 64;
    v.y = (20 + k_shape[i].y) * 10 * 64;
    outline.points[i] = v;
  }
  outline.tags = new char[outline.n_points];
  for (int i = 0; i < outline.n_points; ++i)
    outline.tags[i] = 1;
  outline.contours = new short[outline.n_contours];
  outline.contours[0] = outline.n_points - 1;
  outline.flags = 0;

  const int width = 400;
  const int rows = 400;
  const int pitch = ((width + 15) >> 4) << 1;

  // Set up a bitmap
  FT_Bitmap bmp;
  bmp.buffer = new unsigned char[width * pitch];
  memset(bmp.buffer, 0, width * pitch);
  bmp.width = width;
  bmp.rows = rows;
  bmp.pitch = pitch;
  bmp.pixel_mode = FT_PIXEL_MODE_MONO;

  // Set up the raster params (these seem to be the only two checked).
  FT_Raster_Params params;
  memset(&params, 0, sizeof (params));
  params.source = &outline;
  params.target = &bmp;

  // Allocate a chunk of mem for the render pool.
  const int kRenderPoolSize = 1024 * 1024;
  unsigned char *renderPool = new unsigned char[kRenderPoolSize];

  // Initialize the rasterer and get it to render into the bitmap.
  FT_Raster raster;
  ft_standard_raster.raster_new(mem, &raster);
  ft_standard_raster.raster_reset(raster, renderPool, kRenderPoolSize);
  ft_standard_raster.raster_render(raster, &params);

  // Dump out the raw image data (in PBM format).
  std::ofstream out("out.pbm", std::ios::binary);
  out << "P4 " << width << " " << rows << "\n";
  out.write((const char *)bmp.buffer, width * pitch);

  return 0;
}
 

__________ Information from ESET NOD32 Antivirus, version of virus signature 
database 4266 (20090722) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]