Beispiel #1
0
int sendFILE(int hsock){
	char buffer[1024];
	int buffer_len = 1024;
	int bytecount;
	int c;
	memset(buffer, '\0', 1024);
	char filename[32];
	printf("Enter File name:");
	gets(filename);
	if ((bytecount = send(hsock, filename, Buff_SIZE, 0)) == SOCKET_ERROR){
		fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
		return 0;
	}
	printf("Sent bytes %d\n", bytecount);
	FILE *file = fopen(filename, "r");
	fseek(file, 0, 2);
	int len = ftell(file);
	char length[10];
	itoa(len, length, 10);
	memset(buffer, '\0', Buff_SIZE);
	if ((bytecount = recv(hsock, buffer, Buff_SIZE, 0)) == SOCKET_ERROR){
		fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
		return 0;
	}
	if ((bytecount = send(hsock, length, Buff_SIZE, 0)) == SOCKET_ERROR){
		fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
		return 0;
	}
	fseek(file, 0, 0);
	int count = -1;
	char buff[Buff_SIZE];
	while (count != 0)
	{
		memset(buffer, '\0', Buff_SIZE);
		if ((bytecount = recv(hsock, buffer, Buff_SIZE, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			return 0;
		}
		count = loadBuff(file, buff);
		if ((bytecount = send(hsock, buff, Buff_SIZE, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
			return 0;
		}
		printf("Sent bytes %d\n", bytecount);
	}
	if ((bytecount = send(hsock, "0", Buff_SIZE, 0)) == SOCKET_ERROR){
		fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
		return 0;
	}
	printf("Sent bytes %d\n", bytecount);
	memset(buffer, '\0', buffer_len);
	if ((bytecount = recv(hsock, buffer, Buff_SIZE, 0)) == SOCKET_ERROR){
		fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
		return 0;
	}
	return 1;
}
	uint8_t* Image::xyz::load(SystemString const& filename, int& width, int& height, int& channels, bool trans)
	{
		static uint const  SIGN_SIZE = 4;
		static uint8_t const* SIGN = reinterpret_cast< uint8_t const* >("XYZ1");
		static uint const BUFF_SIZE = 1024;
		static uint const COLOR_NUM = 256, COLOR_SIZE = 3, PALETTE_SIZE = COLOR_SIZE * COLOR_NUM;

		uint8_t sign[SIGN_SIZE];
		#pragma pack(push, 1) // no alignment
			struct Palette { uint8_t r, g, b; };
		#pragma pack(pop)
	// Open File
		std::FILE* fp = std::fopen(filename.c_str(), "rb");
		if(fp == NULL) return NULL;
	// read header
		if(
			( std::fread(sign, 1, sizeof(sign), fp) != sizeof(sign) ) ||
			( std::memcmp(sign, SIGN, SIGN_SIZE) != 0 )
		) {
			std::fclose(fp);
			return NULL;
		}

		width  = std::fgetc(fp) | (std::fgetc(fp) << CHAR_BIT); // read uint16_t little endian
		height = std::fgetc(fp) | (std::fgetc(fp) << CHAR_BIT);
		channels = trans ? 4 : 3;
	// init zlib
		::z_stream z;
		z.zalloc   = Z_NULL;
		z.zfree    = Z_NULL;
		z.opaque   = Z_NULL;
		z.next_in  = Z_NULL;
		z.avail_in = 0;
		if(::inflateInit(&z) != Z_OK) {
			fclose(fp);
			return NULL;
		}
	// decode palette and image data
		uint8_t  inbuff[BUFF_SIZE];
		boost::scoped_array< uint8_t > loadBuff( new uint8_t[PALETTE_SIZE + width*height] );
		int status = Z_OK;
		z.next_out = reinterpret_cast< ::Bytef* >( loadBuff.get() );
		z.avail_out = PALETTE_SIZE + width*height;
		while(true) {
			if(z.avail_in == 0) {
				z.next_in  = reinterpret_cast< ::Bytef* >(inbuff);
				z.avail_in = fread(inbuff, 1, BUFF_SIZE, fp);
			}
			status = ::inflate(&z, Z_NO_FLUSH);
			if( (z.avail_out == 0) && (status == Z_STREAM_END) ) {
				std::fclose(fp);
				break;
			}

			if( (status != Z_OK) || (status == Z_STREAM_END) ) {
				std::fclose(fp);
				return NULL;
			}
		}
	// convert
		Palette* palette = reinterpret_cast< Palette* >( loadBuff.get() );
		uint8_t* data = loadBuff.get() + PALETTE_SIZE;
		uint8_t* ret = new uint8_t[channels * width * height];

		for(int y = 0; y < height; y++) {
			for(int x = 0; x < width; x++) {
				uint8_t index = data[(x + y*width)];
				ret[channels * (x + y * width) + 0] = palette[index].r;
				ret[channels * (x + y * width) + 1] = palette[index].g;
				ret[channels * (x + y * width) + 2] = palette[index].b;
				if(trans) ret[channels * (x + y * width) + 3] = (index == TRANS_COLOR) ? 0x00 : 0xff;
			}
		}

		return ret;
	} // uint8_t* Image::xyz::load