Skip to content

Instantly share code, notes, and snippets.

@kvpb
Last active February 7, 2024 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvpb/6595153c6c743a53d2cd1604eb395955 to your computer and use it in GitHub Desktop.
Save kvpb/6595153c6c743a53d2cd1604eb395955 to your computer and use it in GitHub Desktop.
Calculate the normal focal length for the given film or image sensor format.
#include <stdint.h> // UInt16_T
#include <stdlib.h> // AToF, MAlloc
#include <math.h> // PowF, SQRT, RoundF
#include <ctype.h> // IsAlpha
#include <string.h> // STRCPY, STRnCMP
#include <stdio.h> // PrintF
float focal_normal_calculate( float h/*eight_sensor*/, float w/*idth_sensor*/ )
{
float d/*iagonal_sensor*/;
d = sqrt( powf( w, 2 ) + powf( h, 2 ) ); // Pythagorean theorem
return d;
}
int main( int c_a, char** v_a )
{
if ( ( c_a < 3 ) || ( ( strncmp( v_a[ 1 ], "--", 2 ) || strncmp( v_a[ 1 ], "-", 1 ) /*|| isalpha( v_a[ 1 ][ 0 ] )*/ ) && ( c_a < 5 ) ) )
return 1;
char* s_w, *s_h;
float w, h, f;
if ( strcmp(v_a[ 1 ], "--c") == 0 || strcmp(v_a[ 1 ], "-c") == 0 || strcmp(v_a[ 1 ], "c") == 0 )
{
s_w = malloc( strlen(v_a[ 4 ]) * sizeof( char ) );
strcpy( s_w, v_a[ 4 ]);
s_h = malloc( strlen(v_a[ 3 ]) * sizeof( char ) );
strcpy( s_h, v_a[ 3 ]);
if ( strcmp(v_a[ 2 ], "inches") == 0 || strcmp(v_a[ 2 ], "in") == 0 )
{
w = 25.4/*( 127 / 5 )*/ * atof( v_a[ 4 ] ); //w /= ( 127 / 5 );
h = 25.4/*( 127 / 5 )*/ * atof( v_a[ 3 ] ); //h /= ( 127 / 5 );
}
if ( strcmp(v_a[ 2 ], "centimeters") == 0 || strcmp(v_a[ 2 ], "cm") == 0 )
{
w = 10 * atof(v_a[ 4 ]); //w *= 10;
h = 10 * atof(v_a[ 3 ]); //h *= 10;
}
}
else
{
w = atof(v_a[ 2 ]);
h = atof(v_a[ 1 ]);
s_w = malloc( strlen(v_a[ 2 ]) * sizeof( char ) );
strcpy(s_w, v_a[ 2 ]);
s_h = malloc( strlen(v_a[ 1 ]) * sizeof( char ) );
strcpy(s_h, v_a[ 1 ]);
}
f = roundf( focal_normal_calculate( h, w ) );
printf("normal lens focal length for %s * %s sensor format in millimeters (mm):\nf_normal_%s*%s\t= %i mm\n", s_h, s_w, s_h, s_w, (uint16_t)f );
return 0;
}
/*// normal.c
//// Calculate the normal focal length for the given film or image sensor format.
////
//// Karl V. P. B. `kvpb` AKA Karl Thomas George West `ktgw`
//// +33 A BB BB BB BB +1 (DDD) DDD-DDDD
//// local-part@domain local-part@domain
//// https://twitter.com/ktgwkvpb
//// https://github.com/kvpb
////
//// Copyright 2024 Karl Vincent Pierre Bertin
////
//// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
////
//// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
////
//// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
////
//// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
////
*/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment