Beispiel #1
0
int main(int argc, char* argv[])
{
  size_t n = 0x200000;
  double rate = 1;
  zfp_field* field;
  uint insize;
  zfp_stream* zfp;
  bitstream* stream;
  void* buffer;
  size_t bytes;
  clock_t c;
  double time;
  uint i;

  switch (argc) {
    case 3:
      sscanf(argv[2], "%zu", &n);
      /* FALLTHROUGH */
    case 2:
      sscanf(argv[1], "%lf", &rate);
      break;
  }

  /* declare array to compress */
  field = zfp_field_3d(NULL, zfp_type_double, 4, 4, 4 * n);
  insize = n * sizeof(block);

  /* allocate storage for compressed bit stream */
  zfp = zfp_stream_open(NULL);
  zfp_stream_set_rate(zfp, rate, zfp_field_type(field), zfp_field_dimensionality(field), 0);
  bytes = zfp_stream_maximum_size(zfp, field);
  buffer = malloc(bytes);
  stream = stream_open(buffer, bytes);
  zfp_stream_set_bit_stream(zfp, stream);
  zfp_field_free(field);

  /* compress */
  c = clock();
  for (i = 0; i < n; i++)
    zfp_encode_block_double_3(zfp, (const double*)block);
  zfp_stream_flush(zfp);
  time = (double)(clock() - c) / CLOCKS_PER_SEC;
  printf("encode in=%u out=%u %.0f MB/s\n", insize, (uint)stream_size(stream), insize / (1024 * 1024 * time));

  /* decompress */
  zfp_stream_rewind(zfp);
  c = clock();
  for (i = 0; i < n; i++) {
    double a[64];
    zfp_decode_block_double_3(zfp, a);
  }
  time = (double)(clock() - c) / CLOCKS_PER_SEC;
  printf("decode in=%u out=%u %.0f MB/s\n", (uint)stream_size(stream), insize, insize / (1024 * 1024 * time));

  zfp_stream_close(zfp);
  stream_close(stream);
  free(buffer);

  return 0;
}
Beispiel #2
0
unsigned int stream_available_read( stream_t* stream )
{
	FOUNDATION_ASSERT( stream );
	if( stream->vtable->available_read )
		return (unsigned int)stream->vtable->available_read( stream );
	return (unsigned int)( stream_size( stream ) - stream_tell( stream ) );
}
Beispiel #3
0
void stream_remove(lua_Stream *self, size_t pos, size_t size)
{
	assert(pos + size <= stream_size(self));
	buffer_remove(&self->buf, pos, size);
	if (self->pos >= pos + size) self->pos -= size;
	else if(self->pos >= pos) self->pos = pos;
}
Beispiel #4
0
static int d81_image_init(const struct ImageModule *mod, imgtool_stream *f, imgtool_image **outimg)
{
	d64_image *image;

	d64_open_helper();
	image=*(d64_image**)outimg=(d64_image *) malloc(sizeof(d64_image));
	if (!image) return IMGTOOLERR_OUTOFMEMORY;

	memset(image, 0, sizeof(d64_image));
	image->base.module = mod;
	image->size=stream_size(f);
	image->get_offset=d81_tracksector2offset;
	image->alloc_sector=d81_alloc_sector;
	image->free_sector=d81_free_sector;
	image->directory.track=40;image->directory.sector=0;
	image->d81=1;
	image->tracks=80;
	image->bam_bytes_dir_track=6;
	image->file_handle=f;

	image->realimage = (unsigned char *) malloc(image->size);
	image->data = image->realimage;
	if ( (!image->data)
		 ||(stream_read(f, image->data, image->size)!=image->size) ) {
		free(image);
		*outimg=NULL;
		return IMGTOOLERR_OUTOFMEMORY;
	}

	return 0;
}
Beispiel #5
0
static int lynx_image_init(const struct ImageModule *mod, imgtool_stream *f, imgtool_image **outimg)
{
	lynx_image *image;
	int rc;

	image=*(lynx_image**)outimg=(lynx_image *) malloc(sizeof(lynx_image));
	if (!image) return IMGTOOLERR_OUTOFMEMORY;

	memset(image, 0, sizeof(lynx_image));
	image->base.module = mod;
	image->size=stream_size(f);
	image->file_handle=f;

	image->data = (unsigned char *) malloc(image->size);
	if ( (!image->data)
		 ||(stream_read(f, image->data, image->size)!=image->size) ) {
		free(image);
		*outimg=NULL;
		return IMGTOOLERR_OUTOFMEMORY;
	}
	if ( (rc=lynx_read_image(image)) ) {
		if (image->entries) free(image->entries);
		free(image);
		*outimg=NULL;
		return rc;
	}

	return 0;
}
Beispiel #6
0
static int rom16_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef,
							   const ResolvedOption *options_)
{
	rom16_image *image=(rom16_image*)img;
	int size;
	int pos;
	int i;

	size=stream_size(sourcef);
	if (size*2!=image->size) return IMGTOOLERR_READERROR;
	if (mame_stricmp(fname, "even")==0) {
		pos=0;
	} else if (mame_stricmp(fname,"odd")==0) {
		pos=1;
	} else {
		return IMGTOOLERR_MODULENOTFOUND;
	}

	for (i=0; i<size; i++) {
		if (stream_read(sourcef, image->data+pos+i*2, 1)!=1)
			return IMGTOOLERR_READERROR;
	}
	image->modified=1;

	return 0;
}
Beispiel #7
0
static int ti85b_file_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg)
{
	ti85b_file *file;

	file=*(ti85b_file**)outimg=(ti85b_file *) malloc(sizeof(ti85b_file));
	if (!file) return IMGTOOLERR_OUTOFMEMORY;

	file->base.module = mod;
	file->size=stream_size(f);
	file->file_handle=f;

	file->data = (unsigned char *) malloc(file->size);
	if (!file->data) return IMGTOOLERR_OUTOFMEMORY;

	if (stream_read(f, file->data, file->size)!=file->size)
	{
		free(file);
		*outimg=NULL;
		return IMGTOOLERR_READERROR;
	}

	if ( strncmp((char *) file->data, (char *) ti85_file_signature, 11))
	{
		free(file);
		return IMGTOOLERR_CORRUPTIMAGE;
	}

	if (file->data[0x3b]!=0x1d)
	{
		free(file);
		return IMGTOOLERR_CORRUPTIMAGE;
	}

	return 0;
}
Beispiel #8
0
static int vmsx_gm2_image_init(const struct ImageModule *mod, imgtool_stream *f, imgtool_image **outimg)
{
    GM2_IMAGE *image;

    image = (GM2_IMAGE*)malloc (sizeof (GM2_IMAGE) );
    if (!image) return IMGTOOLERR_OUTOFMEMORY;

    *outimg = (imgtool_image*)image;

    memset(image, 0, sizeof(GM2_IMAGE));
    image->base.module = mod;
    image->size=stream_size(f);
    image->file_handle=f;

    if (image->size != 0x4000)
    {
        free (image);
        return IMGTOOLERR_READERROR;
    }

    image->data = (unsigned char *) malloc(image->size);
    if ( (!image->data)
            ||(stream_read(f, image->data, image->size)!=image->size) )
    {
        free(image);
        *outimg=NULL;
        return IMGTOOLERR_OUTOFMEMORY;
    }

    return 0;
}
Beispiel #9
0
int stream_seek(imgtool_stream *s, INT64 pos, int where)
{
    UINT64 size;

    size = stream_size(s);

    switch(where)
    {
    case SEEK_CUR:
        pos += s->position;
        break;
    case SEEK_END:
        pos += size;
        break;
    }

    if (pos < 0)
        s->position = 0;
    else
        s->position = MIN(size, pos);

    if (s->position < pos)
        stream_fill(s, '\0', pos - s->position);

    return 0;
}
Beispiel #10
0
char *stream_getline(imgtool_stream *source, UINT16 max_len)
{
	UINT16 pos = 0;
	char data;
	char *line = (char*)malloc(max_len);
	memset(line, 0, max_len);

	while (pos < max_len && stream_size(source) > stream_tell(source))
	{
		stream_read(source, &data, 1);

		switch(data)
		{
			case '\r':
				stream_read(source, &data, 1);
				if (data != '\n')
					stream_seek(source, -1, SEEK_CUR);
			case '\n':
				return line;
			default:
				line[pos++] = data;
				break;
		}
	}

	if (pos)
		return line;

	free(line);
	return NULL;
}
Beispiel #11
0
int stream_crc(imgtool_stream *s, unsigned long *result)
{
    size_t sz;
    void *ptr;

    switch(s->imgtype)
    {
    case IMG_MEM:
        *result = crc32(0, (unsigned char *) s->u.buffer, (size_t) s->filesize);
        break;

    default:
        sz = stream_size(s);
        ptr = malloc(sz);
        if (!ptr)
            return IMGTOOLERR_OUTOFMEMORY;
        stream_seek(s, 0, SEEK_SET);
        if (stream_read(s, ptr, sz) != sz)
        {
            free(ptr);
            return IMGTOOLERR_READERROR;
        }
        *result = crc32(0, (const Bytef*)ptr, sz);
        free(ptr);
        break;
    }
    return 0;
}
Beispiel #12
0
static imgtoolerr_t os9_diskimage_writefile(imgtool_partition *partition, const char *path, const char *fork, imgtool_stream *sourcef, option_resolution *opts)
{
	imgtoolerr_t err;
	imgtool_image *image = imgtool_partition_image(partition);
	struct os9_fileinfo file_info;
	size_t write_size;
	void *buf = NULL;
	int i = -1;
	UINT32 lsn = 0;
	UINT32 count = 0;
	UINT32 sz;
	const os9_diskinfo *disk_info;

	disk_info = os9_get_diskinfo(image);

	buf = malloc(disk_info->sector_size);
	if (!buf)
	{
		err = IMGTOOLERR_OUTOFMEMORY;
		goto done;
	}

	err = os9_lookup_path(image, path, CREATE_FILE, &file_info, NULL, NULL, NULL);
	if (err)
		goto done;

	sz = (UINT32) stream_size(sourcef);

	err = os9_set_file_size(image, &file_info, sz);
	if (err)
		goto done;

	while(sz > 0)
	{
		write_size = (size_t) MIN(sz, (UINT64) disk_info->sector_size);

		stream_read(sourcef, buf, write_size);

		while(count == 0)
		{
			i++;
			lsn = file_info.sector_map[i].lsn;
			count = file_info.sector_map[i].count;
		}

		err = os9_write_lsn(image, lsn, 0, buf, write_size);
		if (err)
			goto done;

		lsn++;
		count--;
		sz -= write_size;
	}

done:
	if (buf)
		free(buf);
	return err;
}
Beispiel #13
0
void update_opk_head(imgtool_stream *stream)
{
	UINT16 size = stream_size(stream) - 6;

	stream_seek(stream, 4, SEEK_SET);
	stream_putc(stream, (size>>8) & 0xff);
	stream_putc(stream, size & 0xff);
}
Beispiel #14
0
UINT32 update_pack_index(psion_pack *pack)
{
	UINT8 data, type;
	UINT16 size;
	UINT16 index = 0;

	memset(pack->pack_index, 0, sizeof(psion_file) * MAXFILES);

	// start at the first record
	stream_seek(pack->stream, 0x10, SEEK_SET);

	do
	{
		stream_read(pack->stream, &data, 1);

		if(data == 0xff)
		{
			pack->eop = stream_tell(pack->stream) - 1;
			return TRUE;
		}
		else if (data == 0x02)
		{
			// long record without name are ignored
			stream_read(pack->stream, &data, 1);
			size = get_long_rec_size(pack->stream);
			stream_seek(pack->stream, size, SEEK_CUR);
		}
		else
		{
			stream_read(pack->stream, &type, 1);

			// deleted record are not listed
			if (type < 0x90 && (type & 0x80))
			{
				pack->pack_index[index].type = type;
				stream_read(pack->stream, &pack->pack_index[index].filename, 8);
				stream_read(pack->stream, &pack->pack_index[index].id, 1);
				pack->pack_index[index].name_rec = stream_tell(pack->stream) - 11;

				//check for data record
				stream_read(pack->stream, &data, 1);
				if (data == 0x02)
					pack->pack_index[index].data_rec = stream_tell(pack->stream) - 1;

				stream_seek(pack->stream, -1, SEEK_CUR);

				index++;
			}
			else
				stream_seek(pack->stream, data, SEEK_CUR);
		}

	} while (stream_size(pack->stream) > stream_tell(pack->stream));

	// corrupted image
	return FALSE;
}
Beispiel #15
0
static void node_checkfile(struct imgtooltest_state *state, xml_data_node *node)
{
	imgtoolerr_t err;
	const char *filename;
	const char *fork;
	filter_getinfoproc filter;
	imgtool_stream *stream = NULL;
	UINT64 stream_sz;
	const void *stream_ptr;
	mess_pile pile;

	if (!state->m_partition)
	{
		state->m_failed = 1;
		report_message(MSG_FAILURE, "Partition not loaded");
		goto done;
	}

	get_file_params(node, &filename, &fork, &filter);
	if (!filename)
		goto done;

	stream = stream_open_mem(NULL, 0);
	if (!stream)
	{
		state->m_failed = 1;
		error_outofmemory();
		goto done;
	}

	err = imgtool_partition_read_file(state->m_partition, filename, fork, stream, filter);
	if (err)
	{
		state->m_failed = 1;
		report_imgtoolerr(err);
		goto done;
	}

	pile_init(&pile);
	messtest_get_data(node, &pile);

	stream_ptr = stream_getptr(stream);
	stream_sz = stream_size(stream);

	if ((pile_size(&pile) != stream_sz) || (memcmp(stream_ptr, pile_getptr(&pile), pile_size(&pile))))
	{
		report_message(MSG_FAILURE, "Failed file verification");
		goto done;
	}

	pile_delete(&pile);

done:
	if (stream != NULL)
		stream_close(stream);
}
Beispiel #16
0
static int cococas_initalt(imgtool_stream *instream, imgtool_stream **outstream, int *basepos,
	int *length, int *channels, int *frequency, int *resolution)
{
	int caslength;
	int numsamples;
	int samplepos;
	UINT8 *bytes = NULL;
	INT16 *buffer = NULL;
	INT16 *newbuffer;

	caslength = stream_size(instream);
	bytes = malloc(caslength);
	if (!bytes)
		goto outofmemory;
	stream_read(instream, bytes, caslength);

	numsamples = COCO_WAVESAMPLES_HEADER + COCO_WAVESAMPLES_TRAILER + (caslength * 8*8);

	buffer = malloc(numsamples * sizeof(INT16));
	if (!buffer)
		goto outofmemory;

	coco_wave_size = caslength;

	samplepos = 0;
	samplepos += coco_cassette_fill_wave(&buffer[samplepos], COCO_WAVESAMPLES_HEADER, CODE_HEADER);
	samplepos += coco_cassette_fill_wave(&buffer[samplepos], caslength, bytes);
	samplepos += coco_cassette_fill_wave(&buffer[samplepos], COCO_WAVESAMPLES_TRAILER, CODE_TRAILER);

	free(bytes);
	bytes = NULL;

	newbuffer = realloc(buffer, samplepos * sizeof(INT16));
	if (!newbuffer)
		goto outofmemory;
	buffer = newbuffer;

	*outstream = stream_open_mem(buffer, samplepos * sizeof(INT16));
	if (!(*outstream))
		goto outofmemory;

	*basepos = 0;
	*length = samplepos * 2;
	*channels = 1;
	*frequency = 4800;
	*resolution = 16;
	return 0;

outofmemory:
	if (bytes)
		free(bytes);
	if (buffer)
		free(buffer);
	return IMGTOOLERR_OUTOFMEMORY;
}
Beispiel #17
0
 bool FileHandle::at_end_of_stream()
 {
     uint32_t size = 0u;
     auto size_result = stream_size(size);
     if (size_result != SUCCESS)
     {
         return false;
     }
     return read_position() >= static_cast<int32_t>(size);
 
 }
Beispiel #18
0
static int ti85_file_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef, const ResolvedOption *options_)
{
	ti85_file *file=(ti85_file*)img;
	int ind;

	unsigned char name_size = strlen (fname);
	UINT16 head_size = 0x04 + name_size;
	UINT16 data_size = stream_size(sourcef);
	unsigned char type = options_[TI85_OPTION_FTYPE].i;
	unsigned int offset = file->size-2;

	if (!(file->data=realloc(file->data, file->size+data_size+head_size+4)) )
		return IMGTOOLERR_OUTOFMEMORY;

	file->size+=data_size+head_size+4;

	if (!(file->entries=realloc(file->entries, sizeof(ti85_entry)*(file->number_of_entries+1))) )
		return IMGTOOLERR_OUTOFMEMORY;
	file->number_of_entries++;

	ind = file->number_of_entries-1;

	file->entries[ind].head_size = head_size;
	file->entries[ind].data_size = data_size;
	file->entries[ind].type = type;
	file->entries[ind].name_size = name_size;
	file->entries[ind].offset = offset;

	HEADER(file)->data_size[0] = (file->size-0x36)&0x00ff;
	HEADER(file)->data_size[1] = ((file->size-0x36)&0xff00)>>8;

	file->data[offset] = head_size&0x00ff;
	file->data[offset+0x01] = (head_size&0xff00)>>8;
	file->data[offset+0x02] = data_size&0x00ff;
	file->data[offset+0x03] = (data_size&0xff00)>>8;
	file->data[offset+0x04] = type;
	file->data[offset+0x05] = name_size;

	strncpy ((char*) file->data+offset+0x06, fname, name_size);

	file->data[offset+head_size+0x02] = data_size%0x00ff;
	file->data[offset+head_size+0x03] = (data_size%0xff00)>>8;

	if (stream_read(sourcef, file->data+offset+head_size+0x04, data_size)!=data_size)
		return IMGTOOLERR_READERROR;
	
	file->data[file->size-2] = ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE)&0x00ff;
	file->data[file->size-1] = (ti85_calculate_checksum(file->data+TI85_HEADER_SIZE, file->size-2-TI85_HEADER_SIZE)&0xff00)>>8;

	file->modified=1;

	return 0;
}
Beispiel #19
0
UINT16 put_ob3(imgtool_stream *instream, imgtool_stream *outstream)
{
	UINT16 size = stream_size(instream) - 6;
	dynamic_buffer buffer(size);

	stream_seek(instream, 6, SEEK_SET);
	stream_read(instream, &buffer[0], size);

	stream_write(outstream, &buffer[0], size);

	// end of pack
	stream_fill(outstream, 0xff, 2);

	return size;
}
Beispiel #20
0
static int cmd_writesector(const struct command *c, int argc, char *argv[])
{
	imgtoolerr_t err;
	imgtool_image *img;
	imgtool_stream *stream = NULL;
	void *buffer = NULL;
	UINT32 size, track, head, sector;

	/* attempt to open image */
	err = imgtool_image_open_byname(argv[0], argv[1], OSD_FOPEN_RW, &img);
	if (err)
		goto done;

	track = atoi(argv[2]);
	head = atoi(argv[3]);
	sector = atoi(argv[4]);

	stream = stream_open(argv[5], OSD_FOPEN_READ);
	if (!stream)
	{
		err = (imgtoolerr_t)(IMGTOOLERR_FILENOTFOUND | IMGTOOLERR_SRC_NATIVEFILE);
		goto done;
	}

	size = (UINT32) stream_size(stream);

	buffer = malloc(size);
	if (!buffer)
	{
		err = (imgtoolerr_t)(IMGTOOLERR_OUTOFMEMORY);
		goto done;
	}

	stream_read(stream, buffer, size);

	err = imgtool_image_write_sector(img, track, head, sector, buffer, size);
	if (err)
		goto done;

done:
	if (buffer)
		free(buffer);
	if (stream)
		stream_close(stream);
	if (err)
		reporterror(err, c, argv[0], argv[1], NULL, NULL, 0);
	return err ? -1 : 0;
}
Beispiel #21
0
static int ti85_file_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg)
{
	ti85_file *file;

	file=*(ti85_file**)outimg=(ti85_file *) malloc(sizeof(ti85_file));
	if (!file) return IMGTOOLERR_OUTOFMEMORY;

	file->base.module = mod;
	file->size=stream_size(f);
	file->file_handle=f;

	file->data = (unsigned char *) malloc(file->size);
	if (!file->data) return IMGTOOLERR_OUTOFMEMORY;

	if (stream_read(f, file->data, file->size)!=file->size)
	{
		free(file);
		*outimg=NULL;
		return IMGTOOLERR_READERROR;
	}

	if ( strncmp((char *) file->data, (char *) ti85_file_signature, 11) &&
             strncmp((char *) file->data, (char *) ti86_file_signature, 11) )
	{
		free(file);
		return IMGTOOLERR_CORRUPTIMAGE;
	}


	if (file->data[0x3b]==0x1d)
	{
		free(file);
		return IMGTOOLERR_CORRUPTIMAGE;
	}

	file->number_of_entries = ti85_variables_count(file->data, file->size);


	file->entries = (ti85_entry*) malloc (file->number_of_entries*sizeof(ti85_entry));
	if (!file->entries) return IMGTOOLERR_OUTOFMEMORY;


	ti85_variables_read(file->data, file->size, file->entries);

	return 0;
}
Beispiel #22
0
static int d64_image_init(const struct ImageModule *mod, imgtool_stream *f, imgtool_image **outimg)
{
	d64_image *image;

	d64_open_helper();
	image=*(d64_image**)outimg=(d64_image *) malloc(sizeof(d64_image));
	if (!image) return IMGTOOLERR_OUTOFMEMORY;

	memset(image, 0, sizeof(d64_image));
	image->base.module = mod;
	image->size=stream_size(f);
	image->file_handle=f;
	image->get_offset=d64_tracksector2offset;
	image->alloc_sector=d64_alloc_sector;
	image->free_sector=d64_free_sector;
	image->directory.track=18;image->directory.sector=0;
	image->bam_bytes_dir_track=3;
	image->d64=1;

	switch (image->size) {
	case 174848: image->tracks=35;break;
	case 175531: image->tracks=35;image->crc=1;break;
	case 196608: image->tracks=40;break;
	case 197376: image->tracks=40;image->crc=1;break;
	default:
		free(image);
		*outimg=NULL;
		return IMGTOOLERR_CORRUPTIMAGE;
	}

	image->realimage = (unsigned char *) malloc(image->size);
	image->data = image->realimage;
	if ( (!image->data)
		 ||(stream_read(f, image->data, image->size)!=image->size) ) {
		free(image);
		*outimg=NULL;
		return IMGTOOLERR_OUTOFMEMORY;
	}

	return 0;
}
Beispiel #23
0
static int cmd_writesector(const struct command *c, int argc, char *argv[])
{
	imgtoolerr_t err;
	imgtool_image *img;
	imgtool_stream *stream = nullptr;
	dynamic_buffer buffer;
	UINT32 size, track, head, sector;

	/* attempt to open image */
	err = imgtool_image_open_byname(argv[0], argv[1], OSD_FOPEN_RW, &img);
	if (err)
		goto done;

	track = atoi(argv[2]);
	head = atoi(argv[3]);
	sector = atoi(argv[4]);

	stream = stream_open(argv[5], OSD_FOPEN_READ);
	if (!stream)
	{
		err = (imgtoolerr_t)(IMGTOOLERR_FILENOTFOUND | IMGTOOLERR_SRC_NATIVEFILE);
		goto done;
	}

	size = (UINT32) stream_size(stream);

	buffer.resize(size);

	stream_read(stream, &buffer[0], size);

	err = imgtool_image_write_sector(img, track, head, sector, &buffer[0], size);
	if (err)
		goto done;

done:
	if (stream)
		stream_close(stream);
	if (err)
		reporterror(err, c, argv[0], argv[1], nullptr, nullptr, nullptr);
	return err ? -1 : 0;
}
Beispiel #24
0
font_t font_create(const char* name)
{
	//todo: make this a task

	FOUNDATION_ASSERT(fs_is_file(name));
	stream_t* ttf_stream = fs_open_file(name, STREAM_IN|STREAM_BINARY);
	FOUNDATION_ASSERT(ttf_stream);
	const uint64_t ttf_stream_size = stream_size(ttf_stream);
	unsigned char* ttf_buffer = (unsigned char*) memory_allocate(ttf_stream_size, 4, MEMORY_TEMPORARY);
	unsigned char* ttf_bitmap = (unsigned char*) memory_allocate(MINT_FONT_BITMAPSIZE*MINT_FONT_BITMAPSIZE, 4, MEMORY_PERSISTENT);

	const uint64_t read = stream_read(ttf_stream, ttf_buffer, ttf_stream_size);
	FOUNDATION_ASSERT(read == ttf_stream_size);

	font_data_t data;
	stbtt_BakeFontBitmap(
		ttf_buffer,0, 32.0, 
		ttf_bitmap, 
		MINT_FONT_BITMAPSIZE, 
		MINT_FONT_BITMAPSIZE, 
		32,96, 
		data.cdata
	);

	TextureCreationInfo info;
	info.type = Texture2D;
	info.format = FormatR8;
	info.width = MINT_FONT_BITMAPSIZE;
	info.height = MINT_FONT_BITMAPSIZE;

	data.texture = texture_create(ttf_bitmap, info);

	array_push_memcpy(s_fontCtx.fonts, &data);

	memory_deallocate(ttf_bitmap);
	memory_deallocate(ttf_buffer);
	stream_deallocate(ttf_stream);

	return array_size(s_fontCtx.fonts)-1;
}
Beispiel #25
0
static int vmsx_tap_image_init(const struct ImageModule *mod, imgtool_stream *f, imgtool_image **outimg)
{
	TAP_IMAGE *image;
	int rc;

	image = (TAP_IMAGE*)malloc (sizeof (TAP_IMAGE) );
	if (!image) return IMGTOOLERR_OUTOFMEMORY;

	*outimg = (imgtool_image*)image;

	memset(image, 0, sizeof(TAP_IMAGE));
	image->base.module = mod;
	image->size=stream_size(f);
	image->file_handle=f;

	image->data = (UINT8*) malloc(image->size);
	if (!image->data)
	{
		free(image);
		*outimg=NULL;
		return IMGTOOLERR_OUTOFMEMORY;
	}

	if (stream_read(f, image->data, image->size)!=image->size) 
	{
		free(image);
		*outimg=NULL;
		return IMGTOOLERR_READERROR;
	}

	if ( (rc=vmsx_tap_read_image(image)) ) 	
	{
		if (image->entries) free(image->entries);
		free(image);
		*outimg=NULL;
		return rc;
	}

	return 0;
}
Beispiel #26
0
static int crt_image_writefile(imgtool_image *img, const char *fname, imgtool_stream *sourcef,
							   const ResolvedOption *_options)
{
	crt_image *image=(crt_image*)img;
	int size;
	int pos;

	size=stream_size(sourcef);
	if (!(pos=crt_image_findfile(image, fname)) ) {
		// appending
		pos=image->size;
		if (!(image->data=realloc(image->data, image->size+size+sizeof(crt_packet))) )
			return IMGTOOLERR_OUTOFMEMORY;
		image->size+=size+sizeof(crt_packet);
	} else {
		int oldsize=GET_ULONG(PACKET(image,pos)->packet_length);
		// overwritting
		if (!(image->data=realloc(image->data, image->size+size+sizeof(crt_packet)-oldsize) ) )
			return IMGTOOLERR_OUTOFMEMORY;
		if (image->size-pos-oldsize!=0) {
			memmove(image->data+pos+size+sizeof(crt_packet), image->data+pos+oldsize,
					image->size-pos-oldsize);
		}
		image->size+=size+sizeof(crt_packet)-oldsize;
	}
	if (stream_read(sourcef, image->data+pos+sizeof(crt_packet), size)!=size) {
		return IMGTOOLERR_READERROR;
	}
	memset(image->data+pos, 0, sizeof(crt_packet));
	memcpy(PACKET(image,pos)->id,"CHIP",4);
	SET_ULONG( PACKET(image, pos)->packet_length, size+sizeof(crt_packet));
	SET_UWORD(PACKET(image, pos)->chip_type, _options[C64CRT_FILEOPTION_FTYPE].i);
	SET_UWORD( PACKET(image, pos)->address, _options[C64CRT_FILEOPTION_FADDR].i);
	SET_UWORD( PACKET(image, pos)->bank, _options[C64CRT_FILEOPTION_FBANK].i);
	SET_UWORD( PACKET(image, pos)->length, size);

	image->modified=1;

	return 0;
}
Beispiel #27
0
static int crt_image_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg)
{
	crt_image *image;

	image=*(crt_image**)outimg=(crt_image *) malloc(sizeof(crt_image));
	if (!image) return IMGTOOLERR_OUTOFMEMORY;

	memset(image, 0, sizeof(crt_image));
	image->base.module = mod;
	image->size=stream_size(f);
	image->file_handle=f;

	image->data = (unsigned char *) malloc(image->size);
	if ( (!image->data)
		 ||(stream_read(f, image->data, image->size)!=image->size) ) {
		free(image);
		*outimg=NULL;
		return IMGTOOLERR_OUTOFMEMORY;
	}

	return 0;
}
Beispiel #28
0
Datei: io.c Projekt: asqz/runner
void* stream_read_file(const char* fname, long* psize)
{
   stream_t* stream = NULL;
   if (io_open_reader(io_get_default(), fname, &stream) != 0)
   {
      return NULL;
   }

   long size = stream_size(stream);
   if (size < 0)
   {
      return NULL;
   }

   void* buffer = malloc(size + 1);
   memset(buffer, 0, size + 1);

   (*psize) = stream_read(stream, buffer, size);

   stream_close(stream);

   return buffer;
}
Beispiel #29
0
int seek_next_record(imgtool_stream *stream, UINT8 id)
{
	UINT8 data, rec_id;
	UINT16 size;

	do
	{
		stream_read(stream, &data, 1);

		if(data == 0xff)
			break;

		if (data == 2)
		{
			stream_read(stream, &rec_id, 1);
			size = get_long_rec_size(stream);

		}
		else
		{
			stream_read(stream, &rec_id, 1);
			if (id == rec_id)
			{
				stream_seek(stream, -2, SEEK_CUR);
				return TRUE;
			}
			size = data;
		}

		// next record
		stream_seek(stream, size, SEEK_CUR);

	} while (stream_size(stream) > stream_tell(stream));

	return FALSE;
}
Beispiel #30
0
UINT64 stream_transfer_all(imgtool_stream *dest, imgtool_stream *source)
{
    return stream_transfer(dest, source, stream_size(source));
}