Skip to content

Instantly share code, notes, and snippets.

@John-Colvin
Created May 31, 2024 18:29
Show Gist options
  • Save John-Colvin/70ebced669fecd1a8e44676ebf7bc9fa to your computer and use it in GitHub Desktop.
Save John-Colvin/70ebced669fecd1a8e44676ebf7bc9fa to your computer and use it in GitHub Desktop.
Ply file colors to spherical
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
float toSH(uint8_t b) {
double C0 = 0.28209479177387814;
return ((b / 255.0) - 0.5) / C0;
}
void convert_and_write(FILE *input, FILE *output) {
uint8_t buffer[373];
float converted[3];
fread(buffer, 1, 373, input);
fwrite(buffer, 1, 59, output);
fwrite(
"property float x\n"
"property float y\n"
"property float z\n"
"property float nx\n"
"property float ny\n"
"property float nz\n"
"property float f_dc_0\n"
"property float f_dc_1\n"
"property float f_dc_2\n"
"property float opacity\n"
"property float scale_0\n"
"property float scale_1\n"
"property float scale_2\n"
"property float rot_0\n"
"property float rot_1\n"
"property float rot_2\n"
"property float rot_3\n"
"end_header\n", 1, 358, output);
while (fread(buffer, 1, 59, input) == 59) {
// Convert the three uint8s at the end of the buffer to floats
converted[0] = toSH(buffer[24]);
converted[1] = toSH(buffer[25]);
converted[2] = toSH(buffer[26]);
// Write the first 56 bytes unchanged
fwrite(buffer, 1, 24, output);
// Write the three floats
fwrite(converted, sizeof(float), 3, output);
// Write the rest of the buffer
fwrite(buffer + 27, 1, 32, output);
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
return 1;
}
FILE *input = fopen(argv[1], "rb");
if (input == NULL) {
perror("Error opening input file");
return 1;
}
FILE *output = fopen(argv[2], "wb");
if (output == NULL) {
perror("Error opening output file");
fclose(input);
return 1;
}
convert_and_write(input, output);
fclose(input);
fclose(output);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment