It only works on little endian systems atm, because handling of endianess is a pain in C. The C# and Python encoders handle it fine. The C# encoder currently lives as a part of https://gitea.itycodes.org/itycodes/perlin-noise/src/branch/master/Program.cs#L25-L41master
							parent
							
								
									bfa44a7bf6
								
							
						
					
					
						commit
						821dda8476
					
				@ -0,0 +1,30 @@
 | 
				
			|||||||
 | 
					typedef struct rif {
 | 
				
			||||||
 | 
					  // "lifV0001"
 | 
				
			||||||
 | 
					  // "rifV0001"
 | 
				
			||||||
 | 
					  char     magic_num[8]; // Big Endian. UTF-8 Encoded. 6C 69 66 56 30 30 30 31
 | 
				
			||||||
 | 
					  uint64_t width;        // Big Endian
 | 
				
			||||||
 | 
					  uint8_t  format;
 | 
				
			||||||
 | 
					  uint8_t  data[];
 | 
				
			||||||
 | 
					} rif_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define RIF_MAGIC_LITTLE {'l', 'i', 'f', 'V', '0', '0', '0', '1'}
 | 
				
			||||||
 | 
					#define RIF_MAGIC_BIG    {'r', 'i', 'f', 'V', '0', '0', '0', '1'}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define RIF_FORMAT_R8G8B8   0
 | 
				
			||||||
 | 
					#define RIF_FORMAT_R8G8B8A8 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO detect endianess - this breaks on big endian systems.
 | 
				
			||||||
 | 
					void write_rif_little(char* path, uint32_t width, uint32_t size, uint8_t format, void* data) {
 | 
				
			||||||
 | 
					  // TODO is this better than just hardcoding 17?
 | 
				
			||||||
 | 
					  const uint32_t header_size = sizeof(rif_t) - sizeof(uint8_t*);
 | 
				
			||||||
 | 
					  const uint64_t img_size = header_size + size;
 | 
				
			||||||
 | 
					  rif_t* img = malloc(img_size);
 | 
				
			||||||
 | 
					  char magic[] = RIF_MAGIC_LITTLE;
 | 
				
			||||||
 | 
					  memcpy(&img->magic_num, magic, 8);
 | 
				
			||||||
 | 
					  img->width = width;
 | 
				
			||||||
 | 
					  img->format = format;
 | 
				
			||||||
 | 
					  memcpy(&img->data, data, size);
 | 
				
			||||||
 | 
					  FILE* output_file = fopen(path, "w");
 | 
				
			||||||
 | 
					  fwrite(img, 1, img_size, output_file);
 | 
				
			||||||
 | 
					  free(img);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue