mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-09-06 03:18:13 +00:00
Allow specifying only some SDL_CameraSpec fields when opening a camera
This allows setting the size without format, or FPS without size, etc.
This commit is contained in:
@@ -948,34 +948,38 @@ static void ChooseBestCameraSpec(SDL_CameraDevice *device, const SDL_CameraSpec
|
|||||||
int wantw = spec->width;
|
int wantw = spec->width;
|
||||||
int wanth = spec->height;
|
int wanth = spec->height;
|
||||||
|
|
||||||
// Find the sizes with the closest aspect ratio and then find the best fit of those.
|
if (wantw > 0 && wanth > 0) {
|
||||||
const float wantaspect = ((float)wantw) / ((float) wanth);
|
// Find the sizes with the closest aspect ratio and then find the best fit of those.
|
||||||
const float epsilon = 1e-6f;
|
const float wantaspect = ((float)wantw) / ((float)wanth);
|
||||||
float closestaspect = -9999999.0f;
|
const float epsilon = 1e-6f;
|
||||||
float closestdiff = 999999.0f;
|
float closestaspect = -9999999.0f;
|
||||||
int closestdiffw = 9999999;
|
float closestdiff = 999999.0f;
|
||||||
|
int closestdiffw = 9999999;
|
||||||
|
|
||||||
for (int i = 0; i < num_specs; i++) {
|
for (int i = 0; i < num_specs; i++) {
|
||||||
const SDL_CameraSpec *thisspec = &device->all_specs[i];
|
const SDL_CameraSpec *thisspec = &device->all_specs[i];
|
||||||
const int thisw = thisspec->width;
|
const int thisw = thisspec->width;
|
||||||
const int thish = thisspec->height;
|
const int thish = thisspec->height;
|
||||||
const float thisaspect = ((float)thisw) / ((float) thish);
|
const float thisaspect = ((float)thisw) / ((float)thish);
|
||||||
const float aspectdiff = SDL_fabs(wantaspect - thisaspect);
|
const float aspectdiff = SDL_fabs(wantaspect - thisaspect);
|
||||||
const float diff = SDL_fabs(closestaspect - thisaspect);
|
const float diff = SDL_fabs(closestaspect - thisaspect);
|
||||||
const int diffw = SDL_abs(thisw - wantw);
|
const int diffw = SDL_abs(thisw - wantw);
|
||||||
if (diff < epsilon) { // matches current closestaspect? See if resolution is closer in size.
|
if (diff < epsilon) { // matches current closestaspect? See if resolution is closer in size.
|
||||||
if (diffw < closestdiffw) {
|
if (diffw < closestdiffw) {
|
||||||
|
closestdiffw = diffw;
|
||||||
|
closest->width = thisw;
|
||||||
|
closest->height = thish;
|
||||||
|
}
|
||||||
|
} else if (aspectdiff < closestdiff) { // this is a closer aspect ratio? Take it, reset resolution checks.
|
||||||
|
closestdiff = aspectdiff;
|
||||||
|
closestaspect = thisaspect;
|
||||||
closestdiffw = diffw;
|
closestdiffw = diffw;
|
||||||
closest->width = thisw;
|
closest->width = thisw;
|
||||||
closest->height = thish;
|
closest->height = thish;
|
||||||
}
|
}
|
||||||
} else if (aspectdiff < closestdiff) { // this is a closer aspect ratio? Take it, reset resolution checks.
|
|
||||||
closestdiff = aspectdiff;
|
|
||||||
closestaspect = thisaspect;
|
|
||||||
closestdiffw = diffw;
|
|
||||||
closest->width = thisw;
|
|
||||||
closest->height = thish;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
SDL_copyp(closest, &device->all_specs[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_assert(closest->width > 0);
|
SDL_assert(closest->width > 0);
|
||||||
@@ -1030,16 +1034,6 @@ static void ChooseBestCameraSpec(SDL_CameraDevice *device, const SDL_CameraSpec
|
|||||||
|
|
||||||
SDL_Camera *SDL_OpenCameraDevice(SDL_CameraDeviceID instance_id, const SDL_CameraSpec *spec)
|
SDL_Camera *SDL_OpenCameraDevice(SDL_CameraDeviceID instance_id, const SDL_CameraSpec *spec)
|
||||||
{
|
{
|
||||||
if (spec) {
|
|
||||||
if ((spec->width <= 0) || (spec->height <= 0)) {
|
|
||||||
SDL_SetError("Requested spec frame size is invalid");
|
|
||||||
return NULL;
|
|
||||||
} else if (spec->format == SDL_PIXELFORMAT_UNKNOWN) {
|
|
||||||
SDL_SetError("Requested spec format is invalid");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_CameraDevice *device = ObtainPhysicalCameraDevice(instance_id);
|
SDL_CameraDevice *device = ObtainPhysicalCameraDevice(instance_id);
|
||||||
if (!device) {
|
if (!device) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -1073,10 +1067,21 @@ SDL_Camera *SDL_OpenCameraDevice(SDL_CameraDeviceID instance_id, const SDL_Camer
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!spec) {
|
if (spec) {
|
||||||
SDL_copyp(&device->spec, &closest);
|
|
||||||
} else {
|
|
||||||
SDL_copyp(&device->spec, spec);
|
SDL_copyp(&device->spec, spec);
|
||||||
|
if (spec->width <= 0 || spec->height <= 0) {
|
||||||
|
device->spec.width = closest.width;
|
||||||
|
device->spec.height = closest.height;
|
||||||
|
}
|
||||||
|
if (spec->format == SDL_PIXELFORMAT_UNKNOWN) {
|
||||||
|
device->spec.format = closest.format;
|
||||||
|
}
|
||||||
|
if (spec->interval_denominator == 0) {
|
||||||
|
device->spec.interval_numerator = closest.interval_numerator;
|
||||||
|
device->spec.interval_denominator = closest.interval_denominator;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SDL_copyp(&device->spec, &closest);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_copyp(&device->actual_spec, &closest);
|
SDL_copyp(&device->actual_spec, &closest);
|
||||||
|
Reference in New Issue
Block a user